Generate Swiss QR Code using Java

Generate Swiss QR Code using Java

A Swiss QR code is also known as the Swiss QR Bill. It was developed to automate digital payments. We can easily encode all the necessary payment information required to trigger payments in the Swiss QR Code. The Swiss QR code contains a Swiss cross in the middle. In this article, we will learn how to generate a Swiss QR Code using Java. Furthermore, we will also learn how to decode and read payment information from a Swiss QR code programmatically.

The following topics shall be covered in this article:

Java API to Generate and Read Swiss QR Code

For generating and reading Swiss QR codes, we will be using the Aspose.BarCode for Java API. It provides SwissQRBill and SwissQRCodetext classes offering various properties to work with Swiss QR codes. The SwissQRBill class enables getting or setting billing and payment data, such as account information, amount, currency, and creditor details. The SwissQRCodetext class allows encoding and decoding of the SwissQRBill information embedded in the Swiss QR code.

The API provides the ComplexBarcodeGenerator class for generating images of complex barcodes, such as Swiss QR codes. The BarCodeReader class of the API allows reading the barcodes of the specified DecodeType. The BarCodeResult class of the API stores the recognized barcode data, such as barcode type and code text. Moreover, it is a feature-rich API that lets you generate, scan, and read a wide range of barcode symbologies.

Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-barcode</artifactId>
    <version>22.5</version>
</dependency>

Generate Swiss QR Code in Java

We can generate a Swiss QR code by following the steps given below:

  • Create an instance of the SwissQRCodetext class.
  • Set various SwissQRBill properties such as Version, Account, Amount, Currency, Reference, etc.
  • Initialize the Address class objects and set Creditor and Debtor details.
  • After that, create an instance of the ComplexBarcodeGenerator class with the SwissQRCodetext object.
  • Finally, call the save() method to save the image at the given file path.

The following code sample shows how to generate a Swiss QR code using Java.

Generate Swiss QR Code in Java.

Customize Swiss QR Code in Java

We can customize the appearance of a Swiss QR code by following the steps given below:

  • Create an instance of the SwissQRCodetext class.
  • Set various SwissQRBill properties such as Version, Account, Amount, Currency, Reference, etc.
  • Initialize the Address class objects and set Creditor and Debtor details.
  • Create an instance of the ComplexBarcodeGenerator class with the SwissQRCodetext object.
  • After that, set various barcode parameters such as image width, encoding, back color, resolution, border, etc.
  • Finally, call the save() method to generate the code. It takes the image file path and BarCodeImageFormat as arguments.

The following code sample shows how to customize a Swiss QR code using Java.

Customize Swiss QR Code in Java
Customize Swiss QR Code in Java

Read Swiss QR Code in Java

We can read barcodes from an image by following the steps given below:

  • Firstly, load an image using the BarCodeReader class.
  • Get recognition results in BarCodeResult class object using the readBarCodes() method.
  • Loop through the results and decode the Swiss QR code using the tryDecodeSwissQR() method.
  • Finally, read the SwissQRCodetext and show the data.

The following code sample shows how to read a Swiss QR code from an image using Java.

Version: V2_0
Account: CH4431999123000889012
Amount: 1000.25
Currency: CHF
Reference: 210000000003139471430009017
Creditor Name: Muster & Söhne
Creditor Street: Musterstrasse
Creditor House: 12b
Creditor Postal Code: 8200
Creditor Town: Zürich
Creditor Country: CH
Debtor Name: Muster AG
Debtor Street: Musterstrasse
Debtor House: 1
Debtor Postal Code: 3030
Debtor Town: Bern
Debtor Country: CH

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • generate Swiss QR Bill image programmatically;
  • set QR code image size and resolution;
  • set the border and back color of the QR code;
  • detect a Swiss QR code from an image;
  • read payment details from the Swiss QR Code in Java.

Besides, you can learn more about Aspose.BarCode for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also