QR code encoding principle

QR (Quick-Response) code is a widely used two-dimensional code with fast decoding speed.

This article refers to the address: http://

It can store multipurpose types

8.jpg

As shown above, the basic structure of a qrcode, where:

Position detection pattern, position detection pattern separator, positioning pattern: used to locate the QR code. For each QR code, the position is fixed, but the size specifications will be different;

Correction pattern: the specification is determined, and the number and position of the correction pattern are determined;

Format information: indicates the error correction level of the modified QR code, which is divided into L, M, Q, and H;

Version information: the specification of the QR code. The QR code symbol has a matrix of 40 specifications (usually black and white), from 21x21 (version 1) to 177x177 (version 40). Each version symbol is more than the previous version. Add 4 modules.

Data and error correction codewords: Actually stored QR code information, and error correction codewords (used to correct errors caused by QR code corruption).

Brief coding process:

1. Data analysis: Determine the type of the encoded character and convert it into symbol characters according to the corresponding character set. Select the error correction level. Under the condition of certain specifications, the higher the error correction level, the smaller the capacity of the real data.

2. Data coding: Convert data characters into bit streams, one codeword every 8 bits, and form a sequence of code words of one data as a whole. In fact, knowing the data code word sequence knows the data content of the QR code.

Detailed description of the two-dimensional code encoding principle

Data can be encoded in a pattern for more efficient decoding, such as encoding data (version 1-H),

1) Grouping: 012 345 67

2) Convert to binary: 012 → 0000001100

345→0101011001

67 →1000011

3) Convert to sequence

4) Convert the number of characters into binary: 8→0000001000

5) Join mode indicator (number above) 0001 0101011001 1000011

For letters, Chinese, Japanese, etc., only the grouping methods, modes, and the like are different. The basic method is consistent

3. Error correction coding: block the above codeword sequence as needed, and generate an error correction codeword according to the error correction level and the block codeword, and add the error correction codeword to the data codeword sequence to become A new sequence.

10.jpg

In the case where the two-dimensional code specification and the error correction level are determined, the total number of codewords and the number of error correction code words that can be accommodated therein are also determined, for example, version 10, when the error correction level is H, a total of 346 can be accommodated. Codeword, of which 224 error correction codewords.

That is to say, about 1/3 of the code words in the two-dimensional code area are redundant. For these 224 error correction codewords, it can correct 112 alternative errors (such as black and white upside down) or 224 read errors (unreadable or undecodeable).

The error correction capacity is: 112/346 = 32.4%

4. Construct the final data information: in the condition that the specification is determined, put the sequence generated above into the block in order.

The data is divided into blocks according to regulations, and then each block is calculated to obtain a corresponding error correction codeword block, and the error correction codeword block is sequentially formed into a sequence, which is added to the original data codeword sequence.

Such as: D1, D12, D23, D35, D2, D13, D24, D36, ... D11, D22, D33, D45, D34, D46, E1, E23, E45, E67, E2, E24, E46, E68,. ..

5. Construct Matrix: Place the probing pattern, separator, positioning graph, correction graph, and codeword module into the matrix.

11.jpg

Fill the complete sequence above into the area of ​​the QR code matrix of the corresponding specification

6. Masking: The masking pattern is used for the coding region of the symbol so that the dark and light (black and white) regions in the two-dimensional code pattern can be optimally distributed.

An algorithm, not studied, interested students can continue.

7. Format and version information: Generate format and version information into the corresponding area.

Versions 7-40 contain version information, and all versions without version information are 0. The two locations on the QR code contain version information, which are redundant.

The version information has a total of 18 bits, 6×3 matrix, of which the data is 6 bits, such as version number 8, the data bit information is 001000, and the last 12 bits are error correction bits.

At this point, the encoding process of the QR code is basically completed. Let's practice it below. Of course, you don't have to write the above algorithm yourself. You can use the three-party package zxing.

coding:
Public static void encode(String content, String format, String filePath) {
Try {
Hashtable hints = new Hashtable();//Set the encoding type
Hints.put(EncodeHintType.CHARACTER_SET, DEFAULT_ENCODING);
//Code BitMatrix bitMatrix = new QRCodeWriter().encode(content,
BarcodeFormat.QR_CODE, DEFAULT_IMAGE_WIDTH,
DEFAULT_IMAGE_HEIGHT, hints);
/ / Output to a file, can also be output to the stream
File file = new File(filePath);
MatrixToImageWriter.writeToFile(bitMatrix, format, file);

} catch (IOException e) {
e.printStackTrace();
} catch (WriterException e1) {
e1.printStackTrace();
}
}

decoding:

BufferedImage image = ImageIO.read(file);//Read file
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
Source));
//decoding
Result result = new MultiFormatReader().decode(bitmap);
String resultStr = result.getText();
System.out.println(resultStr);

Silicone Cable

Silicone Insulated Wire,Silicone Cable,Silicone Rubber Cable,Silicone Rubber Wire

Dong guan Sum Wai Electronic Co,. Ltd. , https://www.sw-cables.com