Generate and Read Swiss QR Code in C#
Overview
The Swiss QR Code symbology has been developed in Switzerland to automate digital payments. At present, all payment receipts and bills in Switzerland are intended to have a Swiss QR Code barcode that encrypts payment details. To distinguish from basic QR Code, Swiss QR Code labels have the Swiss cross sign in the center.
Specifically, the Swiss QR Code standard is used to work with QR bills in electronic payments. A Swiss QR Code barcode contains all necessary payment information required to launch payments or to process a QR invoice. Aspose.BarCode for .NET includes SwissQRBill and SwissQRCodetext classes that provide various properties to work with Swiss QR codes.
The general rules for creating Swiss QR Code barcodes and corresponding payment documents are defined in a specific standard called “Swiss Implementation Guidelines for the QR-bill” that relies on the ISO 20022 standard.
Generate Swiss QR Code
To generate a Swiss QR Code barcode using Aspose.BarCode for .NET, it is necessary to create an instance of ComplexBarcodeGenerator and specify the information to be encoded into SwissQRCodetext.
The code sample given below demonstrates how to generate a Swiss QR Code barcode.
//create Swiss QR Bill
SwissQRCodetext swissQRCode = new SwissQRCodetext();
swissQRCode.Bill.Version = SwissQRBill.QrBillStandardVersion.V2_0;
swissQRCode.Bill.Account = "CH4431999123000889012";
swissQRCode.Bill.Amount = 1000.25m;
swissQRCode.Bill.Currency = "CHF";
swissQRCode.Bill.Reference = "210000000003139471430009017";
swissQRCode.Bill.Creditor = new Address
{
Name = "Muster & Söhne",
Street = "Musterstrasse",
HouseNo = "12b",
PostalCode = "8200",
Town = "Zürich",
CountryCode = "CH"
};
swissQRCode.Bill.Debtor = new Address
{
Name = "Muster AG",
Street = "Musterstrasse",
HouseNo = "1",
PostalCode = "3030",
Town = "Bern",
CountryCode = "CH"
};
//encode Swiss QR Bill
ComplexBarcodeGenerator generator = new ComplexBarcodeGenerator(swissQRCode);
generator.Parameters.Barcode.XDimension.Pixels = 4;
generator.Parameters.Barcode.QR.QrEncodeMode = QREncodeMode.ECIEncoding;
generator.Parameters.Barcode.QR.QrECIEncoding = ECIEncodings.UTF8;
generator.Save($"{path}SwissQRBill.png");
Read Swiss QR Code
Aspose.BarCode for .NET includes class ComplexCodetextReader that is used to decode barcode input text according to the specified complex barcode type, in this case, Swiss QR Code. To read Swiss QR Code barcodes, first, it is necessary to create an instance of class BarCodeReader and set it to the value DecodeType.QR; then, the obtained barcode contents need to be parsed in class ComplexCodetextReader by calling the TryDecodeSwissQR method that returns an instance of SwissQRCodetext with the decoded barcode information. The code snippet below illustrates how to implement Swiss QR code recognition.
Console.OutputEncoding = Encoding.Unicode;
//recognize Swiss QR Code
BarCodeReader reader = new BarCodeReader($"{path}SwissQRBill.png", DecodeType.QR);
foreach (BarCodeResult result in reader.ReadBarCodes())
{
SwissQRCodetext swissResult = ComplexCodetextReader.TryDecodeSwissQR(result.CodeText);
if (null == swissResult) continue;
Console.WriteLine($"Version:{swissResult.Bill.Version}");
Console.WriteLine($"Account:{swissResult.Bill.Account}");
Console.WriteLine($"Amount:{swissResult.Bill.Amount}");
Console.WriteLine($"Currency:{swissResult.Bill.Currency}");
Console.WriteLine($"Reference:{swissResult.Bill.Reference}");
Console.WriteLine($"Creditor:{swissResult.Bill.Creditor.Name}");
Console.WriteLine($"Debtor:{swissResult.Bill.Debtor.Name}");
}