Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,68 +1,83 @@
// Title: Decode Swiss QR Bill and extract creditor details
// Description: Demonstrates generating a Swiss QR code, decoding it, and accessing creditor name, IBAN, amount, and currency from the decoded SwissQRCodetext.
// 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.
// Prompt: Access creditor name, IBAN, amount, and currency properties from the decoded SwissQRCodetext instance.
// Tags: swissqr, qr, barcode generation, barcode recognition, png, aspose.barcode, financial, payment

using System;
using System.IO;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.BarCode.ComplexBarcode;
using Aspose.BarCode;

/// <summary>
/// Demonstrates creation, encoding, and decoding of a Swiss QR bill using Aspose.BarCode.
/// Example program that creates a Swiss QR code, decodes it, and extracts key payment fields.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application.
/// Generates a Swiss QR code, saves it to a memory stream, then reads and decodes it.
/// Entry point. Generates a Swiss QR barcode, reads it back, and prints creditor details.
/// </summary>
static void Main()
{
// ------------------------------------------------------------
// 1. Build the Swiss QR bill codetext with required fields.
// 1. Build the Swiss QR bill data model with required fields.
// ------------------------------------------------------------
var swissQr = new SwissQRCodetext();
swissQr.Bill.Creditor.Name = "John Doe"; // Creditor's name
swissQr.Bill.Creditor.CountryCode = "CH"; // Creditor's country (Switzerland)
swissQr.Bill.Account = "CH9300762011623852957"; // IBAN account number
swissQr.Bill.Amount = 199.95m; // Payment amount
swissQr.Bill.Currency = "CHF"; // Currency (mandatory)
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0; // QR bill version
swissQr.Bill.Creditor.Name = "John Doe";
swissQr.Bill.Creditor.CountryCode = "CH";
swissQr.Bill.Account = "CH9300762011623852957";
swissQr.Bill.Amount = 199.95m;
swissQr.Bill.Currency = "CHF";
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;

// ------------------------------------------------------------
// 2. Construct the encoded text that will be embedded in the QR code.
// ------------------------------------------------------------
string encodedText = swissQr.GetConstructedCodetext();

// ------------------------------------------------------------
// 2. Encode the codetext into a QR barcode image stored in memory.
// 3. Generate a QR barcode image (PNG) containing the Swiss QR text.
// ------------------------------------------------------------
using (var ms = new MemoryStream())
using (var generator = new BarcodeGenerator(EncodeTypes.QR, encodedText))
{
// Generate the QR code and write it as PNG into the memory stream.
using (var generator = new ComplexBarcodeGenerator(swissQr))
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
}

// Reset stream position to the beginning for reading.
ms.Position = 0;
ms.Position = 0; // Reset stream position for reading.

// ------------------------------------------------------------
// 3. Decode the QR barcode from the memory stream.
// ------------------------------------------------------------
using (var reader = new BarCodeReader(ms, DecodeType.QR))
{
// Iterate over all detected barcodes (should be only one).
foreach (var result in reader.ReadBarCodes())
// ------------------------------------------------------------
// 4. Read and decode the barcode image from the memory stream.
// ------------------------------------------------------------
using (var reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes))
{
// Attempt to parse the complex Swiss QR codetext.
var decoded = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
if (decoded != null)
{
// Output decoded bill details to the console.
Console.WriteLine("Creditor Name: " + decoded.Bill.Creditor.Name);
Console.WriteLine("IBAN: " + decoded.Bill.Account);
Console.WriteLine("Amount: " + decoded.Bill.Amount);
Console.WriteLine("Currency: " + decoded.Bill.Currency);
}
else
var results = reader.ReadBarCodes();

// ------------------------------------------------------------
// 5. Iterate over decoded results and extract Swiss QR bill fields.
// ------------------------------------------------------------
foreach (var result in results)
{
// Inform the user if decoding failed.
Console.WriteLine("Failed to decode SwissQR codetext.");
// Attempt to parse the raw code text as a Swiss QR bill.
var decodedSwiss = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
if (decodedSwiss != null)
{
// Access required properties from the decoded object.
string creditorName = decodedSwiss.Bill.Creditor.Name;
string iban = decodedSwiss.Bill.Account;
decimal amount = decodedSwiss.Bill.Amount;
string currency = decodedSwiss.Bill.Currency;

// Output the extracted values.
Console.WriteLine($"Creditor Name: {creditorName}");
Console.WriteLine($"IBAN: {iban}");
Console.WriteLine($"Amount: {amount}");
Console.WriteLine($"Currency: {currency}");
}
else
{
Console.WriteLine("Failed to decode Swiss QR codetext.");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Title: Benchmark decoding time for Swiss QR Code images at various resolutions
// 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.
// 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.
// Prompt: Benchmark the time required to decode Swiss QR Code images of varying resolutions using BarCodeReader.
// Tags: swiss qr code, barcode generation, barcode decoding, performance benchmark, aspnet.barcode, complexbarcodegenerator, barcodereader

using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -6,75 +12,99 @@
using Aspose.BarCode.BarCodeRecognition;

/// <summary>
/// Demonstrates benchmarking of QR code decoding at various image resolutions
/// using Aspose.BarCode library. Generates a Swiss QR bill barcode, decodes it,
/// and reports the decoding time for each DPI setting.
/// Generates Swiss QR Code images at different resolutions and benchmarks the decoding time using BarCodeReader.
/// </summary>
class Program
{
/// <summary>
/// Entry point of the application. Generates a Swiss QR barcode at multiple
/// resolutions, decodes it, and prints timing and content information.
/// Entry point of the example. Iterates over predefined resolutions, creates a Swiss QR Code for each,
/// decodes it, and prints the elapsed time.
/// </summary>
static void Main()
{
// Define different image resolutions (dots per inch) to benchmark.
int[] resolutions = { 72, 150, 300, 600 };
// Define a set of image resolutions to test (width x height in points)
var resolutions = new (int width, int height)[]
{
(100, 100),
(200, 200),
(400, 400)
};

// Process each resolution
foreach (var res in resolutions)
{
// Generate a Swiss QR Code image at the specified resolution
byte[] imageData = GenerateSwissQrImage(res.width, res.height);

// Decode the image and measure the time taken
double elapsedMs = DecodeImageAndMeasure(imageData);

// Output the benchmark result
Console.WriteLine($"Resolution: {res.width}x{res.height} points - Decode time: {elapsedMs:F2} ms");
}
}

// Prepare sample Swiss QR bill data (must be valid for generation).
/// <summary>
/// Generates a Swiss QR Code image with the given width and height (points) and returns the PNG bytes.
/// </summary>
/// <param name="width">Image width in points.</param>
/// <param name="height">Image height in points.</param>
/// <returns>Byte array containing the PNG image.</returns>
static byte[] GenerateSwissQrImage(int width, int height)
{
// Prepare Swiss QR Code codetext with required fields
var swissQr = new SwissQRCodetext();
swissQr.Bill.Creditor.Name = "John Doe";
swissQr.Bill.Creditor.CountryCode = "CH";
swissQr.Bill.Account = "CH9300762011623852957";
swissQr.Bill.Amount = 199.95m;
swissQr.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;

// Iterate over each resolution, generate, decode, and report results.
foreach (int dpi in resolutions)
// Create the generator for the complex barcode
using (var generator = new ComplexBarcodeGenerator(swissQr))
{
// Generate Swiss QR barcode image at the specified resolution.
using (var generator = new ComplexBarcodeGenerator(swissQr))
{
generator.Parameters.Resolution = (float)dpi;

// Store the generated image in a memory stream.
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
ms.Position = 0; // Reset stream position for reading.
// Set image size in points
generator.Parameters.ImageWidth.Point = (float)width;
generator.Parameters.ImageHeight.Point = (float)height;

// Start timing the decoding process.
var stopwatch = Stopwatch.StartNew();

// Decode the barcode from the memory stream.
using (var reader = new BarCodeReader(ms, DecodeType.QR))
{
var results = reader.ReadBarCodes();

// Stop timing after decoding completes.
stopwatch.Stop();
// Save to a memory stream in PNG format and return the byte array
using (var ms = new MemoryStream())
{
generator.Save(ms, BarCodeImageFormat.Png);
return ms.ToArray();
}
}
}

// Output resolution and decoding duration.
Console.WriteLine($"Resolution: {dpi} DPI");
Console.WriteLine($"Decoding time: {stopwatch.ElapsedMilliseconds} ms");
/// <summary>
/// Decodes the provided image bytes and returns the elapsed time in milliseconds.
/// </summary>
/// <param name="imageBytes">Byte array containing the barcode image.</param>
/// <returns>Decoding duration in milliseconds.</returns>
static double DecodeImageAndMeasure(byte[] imageBytes)
{
using (var ms = new MemoryStream(imageBytes))
{
// Initialize the reader for all supported barcode types
using (var reader = new BarCodeReader(ms, DecodeType.AllSupportedTypes))
{
// Start timing
var stopwatch = Stopwatch.StartNew();

// Iterate over all detected barcodes (should be one in this case).
foreach (var result in results)
{
Console.WriteLine($" Detected type: {result.CodeTypeName}");
Console.WriteLine($" CodeText: {result.CodeText}");
// Perform the decoding operation
var results = reader.ReadBarCodes();

// Decode the complex codetext to verify Swiss QR content.
var decoded = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
if (decoded != null)
{
Console.WriteLine($" Decoded Bill Amount: {decoded.Bill.Amount}");
}
}
// Stop timing
stopwatch.Stop();

Console.WriteLine(); // Blank line for readability between resolutions.
}
// Optionally output decoded text (if any)
foreach (var result in results)
{
Console.WriteLine($"Decoded Text: {result.CodeText}");
}

// Return elapsed time in milliseconds
return stopwatch.Elapsed.TotalMilliseconds;
}
}
}
Expand Down
Loading
Loading