Skip to content

Commit c4e3999

Browse files
Merge pull request #83 from aspose-barcode/agent/swiss-qr-code/2026-07-14-145030
feat: Add Aspose.BarCode .NET C# Examples — Swiss Qr Code
2 parents 6fdc36b + 70cfa00 commit c4e3999

28 files changed

Lines changed: 1450 additions & 1323 deletions

File tree

swiss-qr-code/access-creditor-name-iban-amount-and-currency-properties-from-decoded-swissqrcodetext-instance.cs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,83 @@
1+
// Title: Decode Swiss QR Bill and extract creditor details
2+
// Description: Demonstrates generating a Swiss QR code, decoding it, and accessing creditor name, IBAN, amount, and currency from the decoded SwissQRCodetext.
3+
// Category-Description: This example belongs to the Aspose.BarCode Swiss QR Bill processing category. It showcases the use of BarcodeGenerator, BarCodeReader, and ComplexCodetextReader to create, read, and parse Swiss QR codes. Developers working with financial QR codes can learn how to encode bill data, generate PNG images, and retrieve structured payment information programmatically.
4+
// Prompt: Access creditor name, IBAN, amount, and currency properties from the decoded SwissQRCodetext instance.
5+
// Tags: swissqr, qr, barcode generation, barcode recognition, png, aspose.barcode, financial, payment
6+
17
using System;
28
using System.IO;
39
using Aspose.BarCode.Generation;
410
using Aspose.BarCode.BarCodeRecognition;
511
using Aspose.BarCode.ComplexBarcode;
6-
using Aspose.BarCode;
712

813
/// <summary>
9-
/// Demonstrates creation, encoding, and decoding of a Swiss QR bill using Aspose.BarCode.
14+
/// Example program that creates a Swiss QR code, decodes it, and extracts key payment fields.
1015
/// </summary>
1116
class Program
1217
{
1318
/// <summary>
14-
/// Entry point of the application.
15-
/// Generates a Swiss QR code, saves it to a memory stream, then reads and decodes it.
19+
/// Entry point. Generates a Swiss QR barcode, reads it back, and prints creditor details.
1620
/// </summary>
1721
static void Main()
1822
{
1923
// ------------------------------------------------------------
20-
// 1. Build the Swiss QR bill codetext with required fields.
24+
// 1. Build the Swiss QR bill data model with required fields.
2125
// ------------------------------------------------------------
2226
var swissQr = new SwissQRCodetext();
23-
swissQr.Bill.Creditor.Name = "John Doe"; // Creditor's name
24-
swissQr.Bill.Creditor.CountryCode = "CH"; // Creditor's country (Switzerland)
25-
swissQr.Bill.Account = "CH9300762011623852957"; // IBAN account number
26-
swissQr.Bill.Amount = 199.95m; // Payment amount
27-
swissQr.Bill.Currency = "CHF"; // Currency (mandatory)
28-
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0; // QR bill version
27+
swissQr.Bill.Creditor.Name = "John Doe";
28+
swissQr.Bill.Creditor.CountryCode = "CH";
29+
swissQr.Bill.Account = "CH9300762011623852957";
30+
swissQr.Bill.Amount = 199.95m;
31+
swissQr.Bill.Currency = "CHF";
32+
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
33+
34+
// ------------------------------------------------------------
35+
// 2. Construct the encoded text that will be embedded in the QR code.
36+
// ------------------------------------------------------------
37+
string encodedText = swissQr.GetConstructedCodetext();
2938

3039
// ------------------------------------------------------------
31-
// 2. Encode the codetext into a QR barcode image stored in memory.
40+
// 3. Generate a QR barcode image (PNG) containing the Swiss QR text.
3241
// ------------------------------------------------------------
33-
using (var ms = new MemoryStream())
42+
using (var generator = new BarcodeGenerator(EncodeTypes.QR, encodedText))
3443
{
35-
// Generate the QR code and write it as PNG into the memory stream.
36-
using (var generator = new ComplexBarcodeGenerator(swissQr))
44+
using (var ms = new MemoryStream())
3745
{
3846
generator.Save(ms, BarCodeImageFormat.Png);
39-
}
40-
41-
// Reset stream position to the beginning for reading.
42-
ms.Position = 0;
47+
ms.Position = 0; // Reset stream position for reading.
4348

44-
// ------------------------------------------------------------
45-
// 3. Decode the QR barcode from the memory stream.
46-
// ------------------------------------------------------------
47-
using (var reader = new BarCodeReader(ms, DecodeType.QR))
48-
{
49-
// Iterate over all detected barcodes (should be only one).
50-
foreach (var result in reader.ReadBarCodes())
49+
// ------------------------------------------------------------
50+
// 4. Read and decode the barcode image from the memory stream.
51+
// ------------------------------------------------------------
52+
using (var reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes))
5153
{
52-
// Attempt to parse the complex Swiss QR codetext.
53-
var decoded = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
54-
if (decoded != null)
55-
{
56-
// Output decoded bill details to the console.
57-
Console.WriteLine("Creditor Name: " + decoded.Bill.Creditor.Name);
58-
Console.WriteLine("IBAN: " + decoded.Bill.Account);
59-
Console.WriteLine("Amount: " + decoded.Bill.Amount);
60-
Console.WriteLine("Currency: " + decoded.Bill.Currency);
61-
}
62-
else
54+
var results = reader.ReadBarCodes();
55+
56+
// ------------------------------------------------------------
57+
// 5. Iterate over decoded results and extract Swiss QR bill fields.
58+
// ------------------------------------------------------------
59+
foreach (var result in results)
6360
{
64-
// Inform the user if decoding failed.
65-
Console.WriteLine("Failed to decode SwissQR codetext.");
61+
// Attempt to parse the raw code text as a Swiss QR bill.
62+
var decodedSwiss = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
63+
if (decodedSwiss != null)
64+
{
65+
// Access required properties from the decoded object.
66+
string creditorName = decodedSwiss.Bill.Creditor.Name;
67+
string iban = decodedSwiss.Bill.Account;
68+
decimal amount = decodedSwiss.Bill.Amount;
69+
string currency = decodedSwiss.Bill.Currency;
70+
71+
// Output the extracted values.
72+
Console.WriteLine($"Creditor Name: {creditorName}");
73+
Console.WriteLine($"IBAN: {iban}");
74+
Console.WriteLine($"Amount: {amount}");
75+
Console.WriteLine($"Currency: {currency}");
76+
}
77+
else
78+
{
79+
Console.WriteLine("Failed to decode Swiss QR codetext.");
80+
}
6681
}
6782
}
6883
}

swiss-qr-code/benchmark-time-required-to-decode-swiss-qr-code-images-of-varying-resolutions-using-barcodereader.cs

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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+
17
using System;
28
using System.Diagnostics;
39
using System.IO;
@@ -6,75 +12,99 @@
612
using Aspose.BarCode.BarCodeRecognition;
713

814
/// <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.
1216
/// </summary>
1317
class Program
1418
{
1519
/// <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.
1822
/// </summary>
1923
static void Main()
2024
{
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+
}
2346

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
2556
var swissQr = new SwissQRCodetext();
2657
swissQr.Bill.Creditor.Name = "John Doe";
2758
swissQr.Bill.Creditor.CountryCode = "CH";
2859
swissQr.Bill.Account = "CH9300762011623852957";
2960
swissQr.Bill.Amount = 199.95m;
3061
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
3162

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))
3465
{
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;
4569

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+
}
5678

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();
6093

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();
6696

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();
7499

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}");
77104
}
105+
106+
// Return elapsed time in milliseconds
107+
return stopwatch.Elapsed.TotalMilliseconds;
78108
}
79109
}
80110
}

0 commit comments

Comments
 (0)