Principle of TWO-DIMENSIONAL code
This paper briefly introduces the composition of two-dimensional code, aiming to understand the composition and principle, the complex coding process may make the article difficult to understand, so it is not discussed.
An overview of the
-
A total of 40 versions are available. The official Version is Version (version1-version40).
-
Version 1 is a 21 x 21 matrix and Version 2 is a 25 x 25 matrix. Each additional Version increases the size by 4. The formula is :(v-1) * 4 + 21 (V is the Version number)
-
The highest Version is 40, (40-1) * 4 + 21 = 177, so the highest is 177 x 177
Positioning design
Position Detection Pattern
-
Position Detection Pattern (3 places) is a positioning Pattern that marks the size of the TWO-DIMENSIONAL code.
-
Separators for Postion Detection Patterns.
-
The reason that three instead of four means that three can identify a rectangle.
Alignment Patterns
- Only qr codes above Version2 (including Version2) require this feature, again for positioning purposes.
- The locations of alignments can be listed in the Table defined in THE QR Code in Table E.1:
For example, Version8 has 6 Alignment Patterns in the table at positions (6, 24, 42), as shown in the figure:
Timing Patterns
-
Timing Patterns (2 lines) are also used for positioning.
-
The reason is that the TWO-DIMENSIONAL code has 40 sizes, the size is too large after the need to have a standard line, otherwise when scanning may sweep crooked.
-
It’s the same location, so you can link it directly
Functional data
Format Information
- Format Information(3) exists in all sizes and occupies 15 bits, which is used to store some formatted data.
There are five bits: two bits are used to indicate the Error Correction Level (see the following table for Error Correction dimensions), three bits are used to indicate what Mask is used (indicating encoding mode, which will be discussed later), and the other ten bits are used for Error Correction: Calculate by BCH Code
Error correction dimension
Error Correction Level | ||
---|---|---|
L levels | 7% of the codes can be corrected | 01 |
M | Fifteen percent of the code can be corrected | 00 |
Q level | Twenty-five percent of the codes can be corrected | 11 |
H level | 30% of the codes can be corrected | 10 |
Version Information
- Version Information(2 locations) If the Version is larger than or equal to Version 7, two 3 x 6 areas are reserved for storing Version Information.
There are 18 bits in total. 6 bits are the version number and 12bits are the error correction code, for example, Version7:
Data code and error correction code
-
In addition to the above places, the remaining places store the Data Code and Error Correction Code.
-
Here’s a simple concrete example of how data is encoded:
Basic data encoding
We encode the string “HELLO WORLD” : we find the index of these letters in the character index table:
According to the form we can get [17, 14, 21, 21, 24, 36, 32, 24, 27, 21, 13], and then two groups [(17, 14), (21, 21), (24, 36), (32, 24), (27, 21), (13)]
(17, 14) = 17 * 45 + 14 = 779, and finally get 01100001011 after all conversion: 01100001011 01111000110 10001011100 10110111000 10011010100 001101
Convert the number of characters 11 to binary: 000001011(Version 1 -h is 9 bits)
Alphanumeric Mode: 0010
Mode Name | Mode Indicator |
---|---|
Numeric Mode | 0001 |
Alphanumeric Mode | 0010 |
Byte Mode | 0100 |
Kanji Mode | 1000 |
ECI Mode | 0111 |
coding | Number of characters | HELLO WORLD encoding | The end of the |
---|---|---|---|
0010 | 000001011 | 01100001011 01111000110 10001011100 10110111000 10011010100 001101 | 0000 |
Rearrange by 8bits. If all codes do not add up to 8 multiples, you need to add zeros at the end
00100000 01011011 00001011 01111000 11010001 01110010 11011100 01001101 01000011 01000000
If we don’t reach our maximum bits limit at the end, add some complement codes (magic number 11101100 00010001)
We use Version 1’s Q correction level to demonstrate that it requires a maximum of 104 bits, while we only have 80 bits above. It also requires 24 bits, i.e., 3 Padding Bytes
00100000 01011011 00001011 01111000 11010001 01110010 11011100 01001101 01000011 01000000 11101100 00010001 11101100
Error correction codes are added according to the error correction level
Due to the complicated logic, there is no detailed explanation here. If you are interested, please refer to the official documentation
Fill in the final code
-
The final code is filled as follows, starting from the bottom right corner, we fill in the bits along the red line, 1 is black, 0 is white.
-
If any of the above non-data areas are encountered, bypass or skip
- This way, our image is filled in, but sometimes there may be large areas of white space or black blocks, which makes scanning difficult
We also have to do Masking,
Masking operation
- XOR with the graph generated above. This operation can only be applied to the area where the data code and error correction code are placed
- The id of its Mask is as follows :(I and j correspond to x and y in the figure above respectively)
- Here are some of the images behind the Mask. We can see that the data XOR by some masks becomes more fragmented.
Other extensions
Two-dimensional code with LOGO
-
We often see a QR code with a company or product logo in the middle
-
Two-dimensional code is drawn according to the specification of a certain rule of the small box, if you want to add a logo in the middle of it certainly does not meet the specification of two-dimensional code
-
So how do we see the logo added?
- Actually very simple, is in the original two-dimensional code to cover a logo
- So the question comes again, so it blocks the TWO-DIMENSIONAL code, it can also scan successfully?
- There’s no problem. You read it right. Mainly because two-dimensional code has error correction characteristics
- Error correction degree of application when creating two-dimensional code (refer to error correction dimension) : L (7%), M (15%), H (25%), V (30%)
Reference documentation
Explore in depth the principles of QR code and its application