|
| 1 | +// Title: Benchmark decoding time for Swiss QR Code images at various resolutions |
| 2 | +// Description: Demonstrates how to generate Swiss QR Code barcodes of different sizes and measure the time required to decode them using Aspose.BarCode's BarCodeReader. |
| 3 | +// Category-Description: This example belongs to the Aspose.BarCode barcode generation and recognition category, focusing on complex barcode types such as Swiss QR Code. It showcases the use of ComplexBarcodeGenerator for creating QR bills and BarCodeReader for decoding, a common task for developers building payment processing or QR‑code scanning solutions. The snippet helps compare performance across image resolutions. |
| 4 | +// Prompt: Benchmark the time required to decode Swiss QR Code images of varying resolutions using BarCodeReader. |
| 5 | +// Tags: swiss qr code, barcode generation, barcode decoding, performance benchmark, aspnet.barcode, complexbarcodegenerator, barcodereader |
| 6 | + |
1 | 7 | using System; |
2 | 8 | using System.Diagnostics; |
3 | 9 | using System.IO; |
|
6 | 12 | using Aspose.BarCode.BarCodeRecognition; |
7 | 13 |
|
8 | 14 | /// <summary> |
9 | | -/// Demonstrates benchmarking of QR code decoding at various image resolutions |
10 | | -/// using Aspose.BarCode library. Generates a Swiss QR bill barcode, decodes it, |
11 | | -/// and reports the decoding time for each DPI setting. |
| 15 | +/// Generates Swiss QR Code images at different resolutions and benchmarks the decoding time using BarCodeReader. |
12 | 16 | /// </summary> |
13 | 17 | class Program |
14 | 18 | { |
15 | 19 | /// <summary> |
16 | | - /// Entry point of the application. Generates a Swiss QR barcode at multiple |
17 | | - /// resolutions, decodes it, and prints timing and content information. |
| 20 | + /// Entry point of the example. Iterates over predefined resolutions, creates a Swiss QR Code for each, |
| 21 | + /// decodes it, and prints the elapsed time. |
18 | 22 | /// </summary> |
19 | 23 | static void Main() |
20 | 24 | { |
21 | | - // Define different image resolutions (dots per inch) to benchmark. |
22 | | - int[] resolutions = { 72, 150, 300, 600 }; |
| 25 | + // Define a set of image resolutions to test (width x height in points) |
| 26 | + var resolutions = new (int width, int height)[] |
| 27 | + { |
| 28 | + (100, 100), |
| 29 | + (200, 200), |
| 30 | + (400, 400) |
| 31 | + }; |
| 32 | + |
| 33 | + // Process each resolution |
| 34 | + foreach (var res in resolutions) |
| 35 | + { |
| 36 | + // Generate a Swiss QR Code image at the specified resolution |
| 37 | + byte[] imageData = GenerateSwissQrImage(res.width, res.height); |
| 38 | + |
| 39 | + // Decode the image and measure the time taken |
| 40 | + double elapsedMs = DecodeImageAndMeasure(imageData); |
| 41 | + |
| 42 | + // Output the benchmark result |
| 43 | + Console.WriteLine($"Resolution: {res.width}x{res.height} points - Decode time: {elapsedMs:F2} ms"); |
| 44 | + } |
| 45 | + } |
23 | 46 |
|
24 | | - // Prepare sample Swiss QR bill data (must be valid for generation). |
| 47 | + /// <summary> |
| 48 | + /// Generates a Swiss QR Code image with the given width and height (points) and returns the PNG bytes. |
| 49 | + /// </summary> |
| 50 | + /// <param name="width">Image width in points.</param> |
| 51 | + /// <param name="height">Image height in points.</param> |
| 52 | + /// <returns>Byte array containing the PNG image.</returns> |
| 53 | + static byte[] GenerateSwissQrImage(int width, int height) |
| 54 | + { |
| 55 | + // Prepare Swiss QR Code codetext with required fields |
25 | 56 | var swissQr = new SwissQRCodetext(); |
26 | 57 | swissQr.Bill.Creditor.Name = "John Doe"; |
27 | 58 | swissQr.Bill.Creditor.CountryCode = "CH"; |
28 | 59 | swissQr.Bill.Account = "CH9300762011623852957"; |
29 | 60 | swissQr.Bill.Amount = 199.95m; |
30 | 61 | swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0; |
31 | 62 |
|
32 | | - // Iterate over each resolution, generate, decode, and report results. |
33 | | - foreach (int dpi in resolutions) |
| 63 | + // Create the generator for the complex barcode |
| 64 | + using (var generator = new ComplexBarcodeGenerator(swissQr)) |
34 | 65 | { |
35 | | - // Generate Swiss QR barcode image at the specified resolution. |
36 | | - using (var generator = new ComplexBarcodeGenerator(swissQr)) |
37 | | - { |
38 | | - generator.Parameters.Resolution = (float)dpi; |
39 | | - |
40 | | - // Store the generated image in a memory stream. |
41 | | - using (var ms = new MemoryStream()) |
42 | | - { |
43 | | - generator.Save(ms, BarCodeImageFormat.Png); |
44 | | - ms.Position = 0; // Reset stream position for reading. |
| 66 | + // Set image size in points |
| 67 | + generator.Parameters.ImageWidth.Point = (float)width; |
| 68 | + generator.Parameters.ImageHeight.Point = (float)height; |
45 | 69 |
|
46 | | - // Start timing the decoding process. |
47 | | - var stopwatch = Stopwatch.StartNew(); |
48 | | - |
49 | | - // Decode the barcode from the memory stream. |
50 | | - using (var reader = new BarCodeReader(ms, DecodeType.QR)) |
51 | | - { |
52 | | - var results = reader.ReadBarCodes(); |
53 | | - |
54 | | - // Stop timing after decoding completes. |
55 | | - stopwatch.Stop(); |
| 70 | + // Save to a memory stream in PNG format and return the byte array |
| 71 | + using (var ms = new MemoryStream()) |
| 72 | + { |
| 73 | + generator.Save(ms, BarCodeImageFormat.Png); |
| 74 | + return ms.ToArray(); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
56 | 78 |
|
57 | | - // Output resolution and decoding duration. |
58 | | - Console.WriteLine($"Resolution: {dpi} DPI"); |
59 | | - Console.WriteLine($"Decoding time: {stopwatch.ElapsedMilliseconds} ms"); |
| 79 | + /// <summary> |
| 80 | + /// Decodes the provided image bytes and returns the elapsed time in milliseconds. |
| 81 | + /// </summary> |
| 82 | + /// <param name="imageBytes">Byte array containing the barcode image.</param> |
| 83 | + /// <returns>Decoding duration in milliseconds.</returns> |
| 84 | + static double DecodeImageAndMeasure(byte[] imageBytes) |
| 85 | + { |
| 86 | + using (var ms = new MemoryStream(imageBytes)) |
| 87 | + { |
| 88 | + // Initialize the reader for all supported barcode types |
| 89 | + using (var reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes)) |
| 90 | + { |
| 91 | + // Start timing |
| 92 | + var stopwatch = Stopwatch.StartNew(); |
60 | 93 |
|
61 | | - // Iterate over all detected barcodes (should be one in this case). |
62 | | - foreach (var result in results) |
63 | | - { |
64 | | - Console.WriteLine($" Detected type: {result.CodeTypeName}"); |
65 | | - Console.WriteLine($" CodeText: {result.CodeText}"); |
| 94 | + // Perform the decoding operation |
| 95 | + var results = reader.ReadBarCodes(); |
66 | 96 |
|
67 | | - // Decode the complex codetext to verify Swiss QR content. |
68 | | - var decoded = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText); |
69 | | - if (decoded != null) |
70 | | - { |
71 | | - Console.WriteLine($" Decoded Bill Amount: {decoded.Bill.Amount}"); |
72 | | - } |
73 | | - } |
| 97 | + // Stop timing |
| 98 | + stopwatch.Stop(); |
74 | 99 |
|
75 | | - Console.WriteLine(); // Blank line for readability between resolutions. |
76 | | - } |
| 100 | + // Optionally output decoded text (if any) |
| 101 | + foreach (var result in results) |
| 102 | + { |
| 103 | + Console.WriteLine($"Decoded Text: {result.CodeText}"); |
77 | 104 | } |
| 105 | + |
| 106 | + // Return elapsed time in milliseconds |
| 107 | + return stopwatch.Elapsed.TotalMilliseconds; |
78 | 108 | } |
79 | 109 | } |
80 | 110 | } |
|
0 commit comments