Detailed QR code (a)

About two-dimensional code, I checked the information, and now basically in Japan with QR codes, PDF417 and Han letter code basically do not see everyday. The reason is that all the parties concerned, is indeed the most outstanding QR code. So I'm going to write an article on QR code article, if it is writing a book, written in such detail may be inconvenient, but if the article is online, you can free play. Finish this article, then taking part regularization, and integration with other content form a chapter of the book. To facilitate future classes to explain convenient, and fast painting, I also did a "QR assistant program" to help draw the graphic portions of the QR code, and finally evolved into a two-dimensional drawing program code. Originally did not intend to write their own programs to draw two-dimensional code, there are many online development kit ready. But on the one hand to make themselves more familiar two-dimensional code, on the other hand, with the realization of a simple two-dimensional drawing program for students to reference code, can effectively reduce the student's learning curve.
 
QR code is a two-dimensional code invented in Japan, is currently the most widely used encoding. In China, this encoding almost everywhere, micro-channel payment, Alipay, bike sharing, a variety of advertising interest is scanned and so on.
 
1. QR Code features
1. Storage capacity
The traditional bar code information can only handle about 20, compared with, QR codes can process the information several times a hundred times. Further, the QR code may also support all types of data (eg: numbers, letters, Japanese alphabet, Chinese characters, binary, control codes, etc.). A QR code can handle a huge amount of information up to 7089 digits.

2. Small footprint
QR code using the longitudinal and transverse directions of data processing, the same amount of information, the QR code occupies less space. FIG GS1-128 are the coding and encoding GS1 QR code pattern `(02) 66901234000049 (17) 050101 (37) 10 (10) ABC` is. I would like to draw graphics on the screen, and then use the scan code scanning gun, then reducing the image until the scan code gun not recognized so far. Finally, scaling, etc., to obtain the following comparative drawing area.

 
3. Strong error correction capability
It includes a QR code "error correction", even if some encoded dirty or damaged, the data can be restored. Data recovery codeword units (units of the internal data is, in case of QR code, each eight-bit codeword represents 1), the error correction up to about 30% (depending on the extent of damage and dirty, there the situation can not be restored).

4 can be read from any direction
QR code can be read quickly from 360 ° in either direction. QR code pattern positioned at three, can help affect the QR code from the background pattern, to achieve fast and stable reading. Conventional laser scan code gun scan the bar code, can only use a scanning direction, but if the use of a CCD scanner, can scan the bar code from either direction.
 
2. QR code structure
A QR code can be divided into two parts: the graphics functions and the coding region.

Functional patterns and correction patterns play a role in targeting, specific data area for recording data, error correction information and version information.

2.1 QR Code symbol version and specifications
QR code symbol total of 40 kinds of specifications, respectively, version 1, version 2 ...... 40 version. Specifications Version 1 was 21 × 21 block module, specification version 2 is 25 × 25 block module, and so on, each of the symbols than the previous version for each version of a module 4 side increases, until version 40 (size 177 × module 177 module).
 

Seen from the figure, a graphic version of 21 × 21 is divided into small blocks, modules refers to these small squares, QR code is the minimum unit of the drawing, when drawing the QR code, the data block will be present as a dark filler, The final composition of the QR code graphic. The figure is a version of the module of FIG. 40, the small blocks consisting of 177 × 177.


 2.2 Viewfinder graphics
Viewfinder same pattern comprising three position detection patterns, each two-dimensional code is located in the top left, top right, bottom left, each by a position detection pattern composed of 7 × 7 modules as shown below:

 
Possibility of symbol encountered elsewhere similar pattern is extremely small, it is possible to quickly identify possible symbol in the QR code field of view. Composition viewfinder three identification pattern position detection patterns can be unambiguously determined position and orientation of the symbol in the field of view. The figure is the version 1 and version 6 of the finder pattern, seen from FIG higher version number, the smaller the proportion of the finder pattern in the entire pattern.


Position detection pattern separators 2.3
To easily identify the position detection patterns, each with a position detection pattern between the coding region and a width of a separator module, as shown in FIG yellow region. This whole area should be blank, can not fill in the data.


2.4 Positioning graphics
They are respectively horizontal and vertical positioning of a graphics module and a width of one row, the alternating dark and light modules, which start and end modules are dark. Are respectively horizontal and vertical finder pattern in row and column 6 6 (row, column starts counting from 0), and to avoid the position detection patterns. Their purpose is to determine the density and the version of symbol decision module to provide the coordinates of the reference position. The figure is drawn after the version 1 and version finder pattern 6 pattern.


2.5 correction pattern
The correction pattern as a fixed reference pattern, the image in a certain degree of damage, the decoding software can synchronize the image coordinates of the image through which the module. Each correction pattern of 5 × 5 modules. As shown below:


Correction pattern depending on the number of symbols and the version number may be, without correction pattern version 1, version 2 and above contain correction pattern. Correction pattern depending on the number of the QR code version number given. The following table lists the top 14 versions of the data, complete the data please see Appendix 18284-2000 National Standard GB / T E.



Here to talk about how to read the center of the module row / column coordinates. For example, in Release 7, 6, 22, and values ​​are given in Table 38. The correction pattern center point of row and column coordinates (6,22) (22,6) (22,22) (22.38) (38,22) (38, 38). Since the coordinate (6,6) (6, 38) (38,6) coordinate positions are occupied position detection pattern, therefore, the coordinate position of the correction pattern is not placed. It means the center line of the module / column coordinates of each value separately with their own values ​​and other values ​​consisting of the coordinates, and then remove the upper left, upper right, lower left coordinate, namely the outcome.

FIG version the correction pattern 1 and version 7:


The following figure is a graphical version of the corrected version 15 and 40:


There is no fixed formula central module row / column coordinate values, but the correction pattern observed above 4, can be found, the QR code designers in the choice of coordinate values, such that the first row and first column of correction pattern integrated into just finder pattern, It does not make the finder pattern destruction.

3. Data coding
Various functional patterns for image recognition has been placed, the next step is to encode the data. Standard QR code may encode four types: numeric, alphanumeric, 8-bit bytes and Kanji, which is a Japanese Kanji characters in Japanese. Our National Standard GB / T 18284-2000 range represents an increase of QR code. The following table is a data type and the corresponding mode indicator may be represented by a QR code.
 
mode indicator    mode indicator
ECI 0111   Nichibun  1000 
digital 0001    Chinese 1101
letter and number 0010   FNC1 0101 (first position)
8 bytes 0100    FNC1 1001 (second position)
Table 1: mode indicator

ECI is something I would like to get to know, but unfortunately almost no information on the Internet, AIM ECI specification of foreign materials, for a fee to download. No way, and so we talk about it later. AIM is now only know one company, ECI is the norm, with this norm, with this thing, you can use two-dimensional code to load all kinds of different character sets, but also allows the receiving system before you know what data is used to make specific expansion or decryption process.

FNC1 believe've read the previous book, "bar code technology," the article in the GS1-128 barcode should be very familiar with this type of coding is designed to prepare for the GS1 standards. Herein only for explanation numeric, alphanumeric, 8-bit bytes and FNC1 mode, the example program implemented only four encoding.

3.1 Digital mode
Digital mode only decimal numbers from 0 to 9 encoding, generally having a density of 10 indicates three characters.

3.1.1 is converted to a digital bit stream
The numbers to be represented from the left of each group is divided into three, then convert each 10-bit binary data (binary 10 may be represented by the maximum number is 1023). If the last set of numbers only two, then the 7-bit binary representation (7 maximum number that can be represented in binary as 127) of these two numbers. If only one last set of numbers, use the 4-bit binary digit represents the last (four maximum number that can be represented as a binary 15). Binary data concatenated and prefixed with the mode indicator and character count indicator.

[Example 1] **: ** `Digital 01234567`:
1 is divided into three group: 01234567
2. Convert each binary:
    012 -> 0000001100
    345 -> 0101011001
     67 -> 1000011
3. The connector as a binary sequence: 0000001100 0101011001 1000011

3.1.2 The character count indicator to binary
Character count indicator indicates the number of characters loaded, with different lengths of the QR code version, and the mode. Table 2 lists the edition mode and character count indicator corresponding to the length.
 
version Digital mode Alphanumeric mode  8-bit byte mode  Chinese characters mode
1~9 10 9 8 8
10~26 12 11 16 10
27~40  14 13 16 12
Table 2: Character Count Indicator bits

8 digits for data, check table columns digital mode, if used to indicate the version 1, the median is 10. finally:
8 -> 0000001000

#### was added and character count indicator mode indicator
A look-up table, digital mode indicator 0001. [Mode indicator Character Count Indicator + +] in the digital bit stream data is finally connected to the bit stream:

0001 0000001000 0000001100 0101011001 1000011

Then we do a deeper understanding of example:

[Example 2] **: ** `Digital 0123456789012345`:
1 is divided into three group: 0,123,456,789,012,345
2. Convert each binary:
    012 -> 0000001100
    345 -> 0101011001
    678 -> 1010100110
    901 -> 1110000101
    234 -> 0011101010
      5 -> 0101
3. The connector as a binary sequence:
    0000001100 0101011001 1010100110 1110000101 0011101010 0101

4. The number of digits is 16, represented using version 1, it is 10 bits. finally:
16 -> 0000010000

The mode indicator 0001 digitally final bit stream:
    0001 0000010000 0000001100 0101011001 1010100110 1110000101 0011101010 0101

3.2 alphanumeric mode
Mode alphanumeric character set of 45 characters are encoded, namely: 10 numbers from 0 to 9, 26 capital letters A ~ Z, and nine symbols SP, $,%, *, +, -,, /.. Typically, the input character is represented by two 11. The following table is 45 characters and their coded:
character value   character value   character value   character value   character value
0 0   9 9   I 18   R 27   SP 36
1 1   A 10   J 19   S 28   $ 37
2 2   B 11   K 20   T 29   % 38
3 3   C 12   L 21   The 30   * 39
4 4   D 13   M 22   V 31   + 40
5 5   E 14   N 23   W 32   - 41
6 6   F 15   THE 24   X 33   . 42
7 7   G 16   P 25   Y 34   / 43
8 8   H 17   Q 26   WITH 35   : 44

To represent a full 45 characters, it is necessary to six bit binary number ($ 64 = $ 6 ^ 2), then each of the characters need to take two 12-bit space. QR code using another encoding method, the two characters representing compressed using a 11-bit space.

将输入的数字分为两个字符一组,将左边的字符的值乘以 45 与第二个字符的值相加,将所得的结果转换为 11 位二进制数。最大字符组为"::"(44×45+44=2024,而2 11=2048)。如果输入的数据的字符不是 2 的倍数,将最后一个字符编码为 6 位二进制数。将所得的二进制数连接起来并在前面加上模式指示符和字符计数指示符,得到最终编码。

**【例 3】:** 数据`AC-42`,QR 码版本号 1:
1. 根据表 5 查出字符的值:AC-42 -> (10,12,41,4,2)
2. 将结果分为 2 个一组:(10,12)(41,4)(2)
3. 将每组数据转换为 11 位二进制数:
    (10,12) -> 10×45+12=462 -> 00111001110
    (41,4) -> 41×45+4=1849 -> 11100111001
    (2) -> 2 -> 000010
4. 二进制数据顺次连接得到数据位流: 00111001110 11100111001 000010
5. 字符数为 5,查表2,使用 9 位二进制数表示。将字符计数指示符转换为二进制,
    5 -> 000000101
6. 查表 1,模式指示符为 0010。以【模式指示符 + 字符计数指示符 + 数字位流】的方式连接数据最终位流为:
    0010 000000101 00111001110 11100111001 000010

3.3  8 位字节模式
在 8 位字节模式中,一个 8 位码字直接表示一个输入数据字符的 ASCII 字符值(ASCII 译码表请查[这里](http://iotxfd.cn/demo/ascii.html))。即密度为每个字符 8 位。将二进制数据连接起来并在前面加上模式指示符和字符计数指示符,得到最终编码。

**【例 4】:** 数据`Ab>Cd`,QR 码版本号 1:
1. 在 ASCII表中查出各字符的值,并转换为 8 位二进制:
    A -> 65 -> 01000001
    b -> 98 -> 01100010
    `>` -> 62 -> 00111110
    C -> 67 -> 01000011
    d -> 100 -> 01100100
2.  二进制数据顺次连接得到数据位流:01000001 01100010 00111110 01000011 01100100
3. 字符数为 5,查表2,使用 8 位二进制数表示。将字符计数指示符转换为二进制,
    5 -> 00000101
4. 查表 1,模式指示符为 0100。以【模式指示符 + 字符计数指示符 + 数字位流】的方式连接数据最终位流为:
    0100 00000101 01000001 01100010 00111110 01000011 01100100

3.4 混合模式
之前在学习 Code 128 编码时大家应该还记得,为了达到符号长度最小的目的,Code 128 条码支持混合编码的方式。QR 码也支持以混合方式进行编码。其基本结构为【模式指示符 + 字符计数指示符 + 数据】,其后紧跟下一段的指示符开始另一段。下图为有 n 段数据的结构。


由于 GS1 标准仅支持 ASCII 码,这里只针对数字模式、字母数字模式、8 位字节模式的混合编码进行讨论。以下是针对上述模式的编码规则,完整编码规则请参考 GB/T 18284-2000 的附录 H。

3.4.1 位流长度最优化的编码规则
下列是形成最短位流的算法的基础。在方括号中的字符数如[5,7,9]分别用于版本 1~9,10~26 和版本 27~40。

1. 选择初始模式
    * 如果初始输入数据是在 8 位字节的二进制字符的专有子集中,选择 8 位字节模式;
    * 如果初始输入数据是在字母数字字符集的专有子集中,并且如果字符个数少于[6,7,8],其后紧跟 8 位字节专有子集中的数据,那么选择 8 位字节模式,否则选择字母数据模式;
    * 如果初始数据是数字,并且如果数字个数少于[4,4,5],其后紧跟 8 位字节字符集专有子集中的数据,那么选择 8 位字节模式,否则如果少于[6,7,8]后随字母数字字符集的专有子集中的数据,那么选择字母数字模式,否则选择数字模式。
2. 在 8 位字节模式中
    * 如果有不少于[6,8,9]的数字字符序列出现在来自 8 位字节二进制字符集的专有子集的多个数据前,那么转至数字模式;
    * 如果有字母数字字符集的专有子集的不少于[11,15,16]的字符序列出现在来自 8 位字节二进制字符集的专有子集的数据前,那么转至字母数字模式。
3. 在字母数字模式中
    * 如果有 8 位字节字符集的专有子集的一个或多个字符出现,转至 8 位字节模式;
    * 如果有不少于[13,15,17]的数字字符数据序列在来自字母数字字符集的专有子集的数据前出现,转至数字模式。
4. 在数字模式中
    * 如果有一个或多个来自 8 位字节的专有子集中的字符出现,转至 8 位字节模式;
    * 如果有一个或多个字母数字的专有子集中的字符出现,转至字母数字模式。

> 编者注:上述规则摘抄至 GB/T 18284-2000,最后一句是有问题或者不完善的。如果在数字模式中出现字符`Aabcd`,因为`A`属于字母数字字符集,而`abcd`只属于 8 位字节字符集。此时按最后一句话,需先转换为字母数字模式,加入字符`A`,然后马上又转换为 8 位字节模式。很显然,这样做浪费了空间。由于字母数字字符集是 8 位字节字符集的子集,正确的做法应当是转换为 8 位字节模式,然后加入字符`Aabcd`。

**【例 5】:** 数据`123456ABC123`,QR 码版本号 1:
1. 根据编码规则,首先应选择数字模式作为初始模式:0001
2. 加入字符计数指示符:6 -> 0000000110
3. 加入 123 编码:123 -> 0001111011
4. 加入 456 编码:456 -> 0111001000
5. 根据编码规则,转换为字母数字模式:0010
6. 加入字符计数指示符:6 -> 000000110
7. 加入 AB 编码:(10,11) -> 10×45+11=461 -> 00111001101
8. 加入 C1 编码:(12,1) -> 12×45+1=541 -> 01000011101
9. 加入 23 编码:(2,3) -> 2×45+3=93 -> 00001011101

将以上生成的二进制数据从上至下连接,生成最终位流。

3.5 FNC1 模式
FNC1 模式有两种模式指示符,用于标识按特定的行业或应用格式化信息的符号。FNC1 在第一位置时支持 GS1 标准,FNC1 在第二位置时支持按 AIM 认可的特定行业或者特定应用规范格式化信息。由于 AIM 查不到资料,这里只介绍 FNC1 第一位置模式,即模式指示符为:0101。

在 GS1-128 中,FNC1 用于数据段分隔,而 QR 码中没有专用的 FNC1 字符,在字母数字模式中用 % 字符代替,如果在数据中也有 % 符号出现,则 应按 %% 进行编码。在 8 位字节模式中用字符 GS(ASCII 值 29)代替。

> 编者注:个人感觉,这个设计方式相比 GS1-128 来说,并不太合理。即使数据只是纯数字,但只要在其中需要分段,安置 FNC1 ,就必须转为字母数字模式,如果后面还有足够多的数字,还得再次转回来。这样做的成本太高了。数字模式,还有部分未使用的编码,应当有自已的 FNC1,即使这个编码占据 10 个位,还是合算的。

**【例 6】:** 数据`(02)66901234000049(17)050101(37)10(10)ABC`,QR 码版本号 1:
首先分析数据中的 AI:(02)表示物流单元内贸易项目,长度固定;(17)表示有效期,长度固定;(37)表示物流单元内贸易项目数量,长度不固定,需在此 AI 最后加入 FNC1;(10)表示批号,长度不固定,它为最后一个 AI,无需加入 FNC1 标志。最终编码变为:

`(02)66901234000049(17)050101(37)10<FNC1>(10)ABC`

编码过程:

1. 将 FNC1 第一位置作为初始模式:0101
2. 根据位流长度最优化的编码规则,加入数字模式指示符:0001
3. 加入字符计数指示符:28 -> 0000011100
4. 加入 026 编码:026 -> 0000011010
5. 加入 690 编码:690 -> 1010110010
6. 加入 123 编码:123 -> 0001111011
7. 加入 400 编码:400 -> 0110010000
8. 加入 004 编码:004 -> 0000000100
9. 加入 917 编码:917 -> 1110010101
10. 加入 050 编码:050 -> 0000110010
11. 加入 101 编码:101 -> 0001100101
12. 加入 371 编码:371 -> 0101110011
13. 加入 0 编码:0 -> 0000
14. 根据编码规则,转换为字母数字模式:0010
15. 加入字符计数指示符:6 -> 000000110
16. 加入 %1 编码:(38,1) -> 38×45+1=1711 -> 11010101111
17. 加入 0A 编码:(0,10) -> 0×45+10=10 -> 00000001010
18. 加入 BC 编码:(11,12) -> 11×45+12=507 -> 00111111011

将以上生成的二进制数据从上至下连接,生成最终位流。

3.6  终止符
符号的数据结尾由紧跟在最后一个模式段后面的终止符序列 0000 表示,当数据位流数量正好填满符号容量时,它可以省略,或者当符号所余的容量不足 4 位时它可以截短。

3.7  位流到码字的转换
每个模式段的位流需要按顺序连接在一起,最后添加终止符,除非数据位流正好填满符号容量。所得的数据位流将被分为一个个码字;所有码字长度都是 8 位,如果位流长度最后一个码字不足 8 位,则用二进制值为 0 的填充位填充至 8 位,填充位应加在数据位流最后 1 位(最低位)的后面。说白了就是将位流进行 8 位对齐。

我们以【例 1】最终生成的数据位流为例说明上述过程。

1. 【例 1】最终生成的数据位流:000000110001010110011000011
2. 加入终止符 0000,(现在符号容量足够,不用考虑容量不足的情况):
    0000001100010101100110000110000
3. 将数据位流划分为码字,这里在每 8 个位后加一个逗号表示:
    00000011,00010101,10011000,0110000
4. 我们发现最后一个码字只有 7 位数字,不足 8 位,补 1 个零,最终数据变为:
    00000011,00010101,10011000,01100000

最终【例 1】生成的数据使用了 4 个码字。

接下来就要查表了,下表列出了各个版本 QR 码的码字容量,这里只列出一小部分,完整表格请参考 GB/T 18284-2000。


注意表格中的红框部分,由于只使用了 4 个码字,我们完全可以选择最高纠错等级 H。所以最终选择为:版本 1 的 H 纠错等级 QR 码。在此等级中可放置 9 个码字,还剩余 5 个码字是不能放空的,需要交替填充码字 11101100 和 00010001,直至填满整个 9 个码字。

5. 将空位填满至 9 个码字,最终数据变为:
    00000011,00010101,10011000,01100000,11101100,00010001,11101100,00010001,11101100

好!上半场结束,图多,还是分两篇写吧。下半场内容就比较抽象了。

Guess you like

Origin www.cnblogs.com/abatei/p/11655984.html