Read the QR code
According to the standard, data with RS-codes before recording in the picture is "mixed". Special masks are used for this purpose. There are 8 algorithms, among which the best one is selected. The selection criteria are based on a system of penalties, which can also be read in the specification. The "mixed" data is recorded in a special sequence in a template picture, where technical information for the decoding devices is added. Based on the described algorithm, we can distinguish the scheme for extracting data from the QR code: Here, the green felt-tip underlines the points that you will need to implement when you directly read the code. The remaining items can be omitted because the person is reading.
Step 0. QR code
Looking at the pictures, you can see a few distinct areas.These areas are used to detect QR code.
These data are not of interest from the point of view of recorded information, but they need to be deleted or simply remember their location so that they do not interfere. The rest of the code field already contains useful information.
It can be divided into two parts: system information and data. There is also information about the version of the code.
The maximum amount of data that can be written to the code depends on the version of the code. With the upgrade version - special blocks are added, for example as here: On them you can find out and understand what version of QR is in front of you.
Cods of high versions are usually also impractical to read manually. The location of the system information is shown in the picture: The system information is duplicated, which allows to significantly reduce the probability of errors when detecting code and reading.
System information is 15 bits of data, among which the first 5 is useful information, and the remaining 10 are BCH (15,5) code that allows you to correct errors in system data. To the class of BCH codes include and RS codes.
Note ATTENTION that in the figure, two 15-bit strips do not intersect.
Step 1. Reading 5 bits of system information
As already mentioned, only the first 5 bits are of interest. Of which 2 bits indicate the level of error correction, and the remaining 3 bits indicate which mask of the available 8 is applied to the data. In the QR codes considered, the system information contains:Step 2. Mask for system information
In addition to the already announced schemes of protection of system information, in addition, a static mask is applied, which is applied to any system information.It looks like: 101010000010010.
Since only the first 5 bits are of interest, the mask can be shortened and easily remembered: 10101 (ten-one-one).
After applying the "exclusive or" operation (xor), we get the information. Possible error correction levels:
L | 01 |
M | 00 |
Q | 11 |
H | 10 |
000 | (i + j) mod 2=0 |
001 | i mod 2=0 |
010 | j mod 3=0 |
011 | (i + j) mod 3=0 |
100 | ((i div 2) + (j div 3)) mod 2=0 |
101 | (i j) mod 2 + (i j) mod 3=0 |
110 | ((i j) mod 2 + (i j) mod 3) mod 2=0 |
111 | ((i+j) mod 2 + (i j) mod 3) mod 2=0 |
Step 3. Reading the data header
To understand what data to deal with, you must first read the 4-bit header, which contains information about the mode. Specificity of data reading is shown in the picture: List of possible modes:ECI | 0111 |
Numerical | 0001 |
Alphanumeric | 0010 |
8-bit (byte) | 0100 |
Kanji | 1000 |
Structured addition | 0011 |
FNC1 | 0101 (1st position) 1001 (2nd position) |
Step 4. Applying the mask to the header
After extracting the 4 bits describing the mode, you need to apply a mask to them.In our case two masks are used for the two codes. The mask is determined by the expression given in the table above. If this expression reduces to TRUE (true) for a bit with the coordinates (i, j), then the bit is inverted, otherwise everything remains unchanged. The origin in the upper left corner is (0,0). Looking at the expressions, you can see in them patterns. For the considered QR codes, the masks will look like this: We get the following modes:
Step 5. Reading Data
After receiving the data on the mode, you can start reading the information. It must be stipulated that it is most interesting to read numerical and alphanumeric data, since they are easily interpreted. But also do not be afraid of 8-bit. It can also be easily interpreted information. For example, many online QR text generators are encoded in this mode, using ASCII. Another reason why you should initially read the mode is that the number of data packets depends on it. Which also depends on the version of the code. For versions from the first to the ninth blocks length for more readable modes:Numerical | 10 bits/4 bits |
Alphanumeric | 9 bit |
8-bit (byte) | 8 bits |