In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to generate the QR code, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
QR code, also known as QR Code,QR full name Quick Response, is a very popular coding method on mobile devices in recent years. It can store more information and express more data types than traditional Bar Code bar codes, such as characters, numbers, Japanese, Chinese and so on. In the past two days, I have learned the relevant details of the generation of QR code pictures. I feel that this thing is a cryptographic algorithm. I will write this article here to reveal it. It is for studious people to learn together.
For more information about QR Code Specification, see this PDF: http://raidenii.net/files/datasheets/misc/qr_code.pdf
Basic knowledge
First of all, let's say that the QR code has a total of 40 sizes. It's officially called version Version. Version 1 is a matrix of 21 x 21, Version 2 is a matrix of 25 x 25, and Version 3 is the size of 29. Each additional version increases the size of 4. The formula is: (Vmur1) * 4x21 (V is the version number) up to Version 40, (40-1) * 4x21 = 177, so the highest is 177x177square.
Let's look at an example of a QR code:
Positioning pattern
Position Detection Pattern is a positioning pattern that marks the rectangular size of a QR code. These three positioning patterns have white edges called Separators for Postion Detection Patterns. The reason why three instead of four means three can identify a rectangle.
Timing Patterns is also used for positioning. The reason is that the QR code has 40 sizes, and a standard line is needed when the size is too large, otherwise the scan may be crooked.
Alignment Patterns only QR codes above Version2 (including Version2) need this thing, which is also used for positioning.
Functional data
Format Information exists in all sizes and is used to store some formatted data.
If the Version Information is above > = Version 7, you need to reserve two 3 x 6 areas to store some version information.
Data code and error correction code
Except for those places mentioned above, Data Code data codes and Error Correction Code error correction codes are stored in the remaining places.
Data coding
Let's talk about data coding first. QR codes support the following encodings:
Numeric mode numeric coding, from 0 to 9. If the number of digits to be encoded is not a multiple of 3, then the remaining 1 or 2 digits will be converted to 4 or 7bits, and every other 3 digits will be compiled into 10, 12, and 14 bits, depending on the size of the QR code (there is a table below Table 3 to illustrate this)
Alphanumeric mode character encoding. Includes 0-9, uppercase A to Z (no lowercase), and the symbol $% * + -. /: include spaces. These characters are mapped into a character index table. As shown below: (where SP is a space, Char is a character, and Value is its index value) the coding process is to group characters in pairs, then convert them to 45 bits in the following table, and then convert them to 11bits binary, and if there is a single one in the end, convert them to 6bits binary. The encoding mode and the number of characters need to be compiled into 9, 11 or 13 binaries according to different Version sizes (such as Table 3 in the following table)
Byte mode, byte encoding, can be 0-255ISO-8859-1 characters. Some scanners of QR codes can automatically detect whether they are UTF-8 codes.
Kanji mode this is a Japanese code, but also a double-byte code. Similarly, it can also be used for Chinese coding. The coding of Japanese and Chinese characters will subtract a value. For example, characters in 0X8140 to 0X9FFC will be subtracted by 8140, characters in 0XE040 to 0XEBBF will be subtracted from 0XC140, then the first two bits will be multiplied by 0XC0, then the last two bits will be added, and finally converted to 13bit coding. The following illustration is shown:
Extended Channel Interpretation (ECI) mode is mainly used for special character sets. Not all scanners support this encoding.
Structured Append mode is used for mixed coding, that is, this QR code contains a variety of coding formats.
The coding method of FNC1 mode is mainly for some special industries or industries. Like a GS1 bar code or something.
For simplicity, the latter three will not be discussed in this article.
In the following two tables
Table 2 is the "number" of each encoding format, which is to be written in Format Information. Note: Chinese is 1101
Table 3 represents different versions of the QR code, for numeric, character, byte and Kanji modes, for the number of binary bits of a single code. (there are various coding specifications in the specification of the QR code, which will be mentioned later.)
Let's look at a few examples.
Example 1: digital coding
In the case of Version 1 size and error correction level H, the code is 01234567.
Divide the above figures into three groups: 012 345 67
Convert them to binary: 012 to 0000001100; 345 to 0101011001; 67 to 1000011.
String these three binaries together: 0000001100 0101011001 1000011
Convert the number of numbers to binary (version 1Mel H is 10 bits): the binary of 8 numbers is 0000001000
Add the numeric coded flag 0001 and the coding of step 4 to the front: 0001 0000001000 0000001100 0101011001 1000011
Example 2: character encoding
In the case of Version 1 size and error correction level H, code: AC-42
1. Find the index of the five words AC-42 from the character index table (10meme 12pint 41pyrm 2)
two。 Pairwise grouping: (10, 12) (41, 4) (2)
3. Convert each group to the binary of 11bits:
(10pr 12) 10x 45m 12 equals 462 to 00111001110
(41 4) 41 "45" 4 equals 1849 to 11100111001
(2) equals 2 to 000010
4. Connect these binaries: 0011100111011100111001 000010
5. Convert the number of characters to binary (Version 1mer H is 9 bits): 5 characters, 5 to 000000101
6. Add the code mark 0010 and the number of step 5 to the head: 0010 000000101 00111001110111001001000010
Terminator and complement
If we have a HELLO WORLD string to encode, according to example 2 above, we can get the following encoding
Code number of characters HELLO WORLD code 00100000010110101010111100011000110001011100 10110111000 10011010100 001101
We also need to add a Terminator:
Number of encoded characters Encoding end of HELLO WORLD 00100000010110101011111100011000101011100 10110111000 10011010100 0011010000
Rearrange by 8bits
If all the codes do not add up to 8 multiples, we need to add enough zeros to the end, for example, there are 78 bits above, so we need to add 2 zeros, and then group them according to 8 bits:
00100000 01011011 00001011 01111000 11010001 01110010 11011100 01001101 01000011 01000000
Complement code (Padding Bytes)
Finally, if we haven't reached our maximum number of bits limits, we need to add some Padding Bytes codes, Padding Bytes is to repeat the following two bytes:11101100 00010001 (these two binaries are converted to decimals 236and 17, I don't know why, only that's what it says on Spec) the maximum bit limit for each error correction level of each Version. You can refer to the Table- 7 table on pages 28 to 32 of QR Code Spec.
Suppose we need to encode the Q error correction level of Version 1, which requires a maximum of 104 bits, while we only have 80 bits above, so we need 24 more bits, that is, we need 3 Padding Bytes, so we add three, so we get the following code:
00100000 01011011 00001011 01111000 11010001 01110010 11011100 01001101 01000011 01000000 11101100 00010001 11101100
Error correcting code
Above we talked about some error correction levels, Error Correction Code Level, there are four levels of error correction in the QR code, which is why the QR code can be scanned if it is incomplete, that is why someone adds an icon to the center of the QR code.
Error correction capacity L level 7% code can be corrected M level 15% code can be corrected Q level 25% code can be corrected H level 30% code can be corrected
So, how does QR add error correction codes to data codes? First of all, we need to group the data codes, that is, into different Block, and then correct each Block. For how to group, we can look at the definition table of Table-13 to Table-22 on pages 33 to 44 of QR Code Spec. Note the last two columns:
Number of Error Code Correction Blocks: how many blocks need to be divided.
Error Correction Code Per Blocks: the number of code in each block, the so-called number of code, that is, the number of 8bits bytes.
For example: the above Version 5 + Q error correction level: requires 4 Blocks (2 Blocks in a group, a total of two groups), 15 bits data in each of the first two Blocks + 9 bits error correction codes (Note: the codewords in the table is the byte of a 8bits) (renote: the formula of (c, k, r) in the last example is: C = k + 2 * r, because the footnote explains that the capacity of the error correction code is less than half of the error correction code.
The following figure shows an example of 5Muq (I used decimal because binary makes the table too large)
The error correction code of chunk data for each block is 1167 85 70 134 87 38 85 194 50 6 18 6 103 38213 199 11 45 115 247 241 223 229 248 154 117 111 86 161 111 392246 246 66 7 118 242 7 38 86 22 199 146 687 204 96 60 202 182 157 124 124 27 209 17 163 120 13321182 247 119 50 118 118 87 38 82 6 7148 116 212 76 133 75 238 195 195 230 189 189 10 108 192 141270 247 118 86 194 151 50 16 236 17 236 17 236 17 236 17 236 17 236235 159 5 173 24 147 59 33 106 40 255 172 82 2 131 32 178 236
Note: the error correction code of two-dimensional code is mainly realized by Reed-Solomon error correction (Reed-Solomon error correction algorithm). For this algorithm, it is very complex for me, there are a lot of mathematical calculations, such as: polynomial division, mapping the number of 1-255 to the nth power of 2 (0).
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.