Generating QR Code in Java
The QR code is shortened for Quick Response code. It is an alternative to bar code or we can say that it is a two-dimensional bar-code. It contains the matrix of small squares in which information is stored. It works the same as a bar-code. It can be read by an imaging device. Nowadays, it's being used a lot in every field such as GEO location, on products, vehicle tracking, etc. In this section, we will learn how to generate and read QR code in Java by using the ZXing library.
The grid arrangement of the black squares on a white background that can be read by an imaging device is called a QR code. The advantage of a QR code is that it can be scanned by smartphones. We need not to carry any handheld device for scanning. It is used to store information such as text, webpage, URL, etc. It uses the following four standardized encoding modes:
These standard encodings modes store data effectively. The Quick Response system became popular outside the automotive industry due to its fast readability and greater storage capacity compared to standard UPC barcodes.
How does QR code work?
QR code is made up of modules (black and white squares) that contain the encoded data. Modules are arranged in the row and columns that are called Data Matrix. The Placing of data bits starts from the bottom right corner and moves in an upward direction and changes the position while reach at the top.
We scan the QR code through an imaging device such as a camera. After scanning, it processes the QR code using the Reed-Solomon Error Correction Code until the image interpreted. After that, the data is extracted from the patterns that are stored in horizontal and vertical components of the code. The following figure describes the structure of the QR code and its elements.
QR Code Components
There are the two primary components of QR code i.e. Function Patterns and Encoding Regions. These components further divided into small components. The following figure depicts the components of QR code.
The following table describes each component with their work.
Error Correction Levels
A QR code can be scanned even if it is damaged somewhat (up to 30%). The Reed-Solomon Correction algorithm makes it possible. It is to be noted that if we add error correction, it increases the number of the data block in the QR code. So, we need to adjust the level of error correction according to requirements. There are four levels of error correction:
The following QR code represents the 7% error correction.
How much data can be stored in the QR code?
The data storage capacity of a QR code depends on the following three:
The version denotes the dimensions of the symbol (i.e. 4 × version number + 17 dots on each side).
Note: As the version number varies, the number of rows and columns also varies. Therefore, stores the higher information. Also, notable that the QR code contains an equal number of rows and columns. The number of rows and columns depends on the version number.
Generating QR Code
To generate a QR code in Java, we need to use a third-party library named ZXing (Zebra Crossing). It is a popular API that allows us to process with QR code. With the help of the library, we can easily generate and read the QR code. Before moving towards the Java program, we need to add the ZXing library to the project. We can download it from the official site.
After downloading, add it to the classpath. Or add the following dependency in pom.xml file.
Let's create a Java program that generates a QR code.
When we run the above Java program, it generates the following QR code in the specified directory.
Reading QR Code
Let's read the QR code through a Java program that we have generated above.
We see that we get the same data that we have stored in the QR code in the previous program.