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.
QR Code
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:
- Numeric
- Alphanumeric
- Byte/binary
- Kanji (the Japanese script)
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.
Components | Parts of Components | Description |
---|---|---|
Quite Zone | It is the boundary of the QR code. The white space around the QR code represents the quiet zone. | |
Function Patterns | Positioning Patterns | It is also known as finder patterns or position markers. It finds the edges of the QR code. |
Timing Patterns | It helps the scanner to identify the position of rows and columns. The sixth row and column in the QR code are dedicated to the timing pattern. | |
Separator | It separates data from the finder pattern. | |
Alignment Patterns | It helps the scanner to identify the reference point for alignment. | |
Encoding Regions | Format Information | It helps the scanner to identify the types of content such as text, URL, etc. It contains data in two parts i.e. Level of Error Correction and masks pattern. |
Version Information | It helps the scanner to identify the version number of the QR code. | |
Data Modules | It helps the scanner to decode the data entered by the creator. |
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:
Error Correction Levels and their Capability | |
---|---|
Level | Correction in % |
Low (L) | 7% |
Medium (M) | 15% |
Quartile (Q) | 25% |
High (H) | 30% |
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:
- Datatype (mode or input character set)
- Version (1 to 40 different sizes of QR)
- Error correction
The version denotes the dimensions of the symbol (i.e. 4 × version number + 17 dots on each side).
Maximum Character Storage Capacity | |||
---|---|---|---|
Encoding mode | Max. characters | Bits/char | Possible characters and default encoding |
Numeric only | 7,089 | 31⁄3 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
Alphanumeric | 4,296 | 51⁄2 | 0-9, A-Z (upper-case only), space, $, %, *, +, -, ., /, : |
Binary/byte | 2,953 | 8 | ISO 8859-1 |
Kanji/kana | 1,817 | 13 | Shift JIS X 0208 |
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.
GenerateQrCode.java
Output:
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.
ReadQrCode.java
Output:
We see that we get the same data that we have stored in the QR code in the previous program.