Http://blog.csdn.net/young0325/article/details/8254521 code:
A two-dimensional code (2-dimen1_bar Code) is a plane with a specific geometric image according to a certain law (two-dimensional direction)
Distribution of black and white images to record data symbol information.
In many types of two-dimensional bar codes, the commonly used code systems include data matrix, Maxi code, Aztec, QR code, QR code, vericode 417, ultracode, Code 49, and code 16k.
1. Stacked/row-arranged two-dimensional barcode, such,Code 16 K, Code 49, ipv417 (for example), etc.
2. Matrix QR codes are the most popular QR codes.
The name of the QR code is relative to the one-dimensional code. For example, the previous barcode is a one-dimensional code ",
It has the following advantages: the two-dimensional code stores a larger amount of data, can contain numbers, characters, Chinese text and other mixed content; has a certain degree of fault tolerance (can be normally read after some damage ); high space utilization.
QR code principles:
Quick-response code is a widely used QR code with fast decoding speed.
It can store multiple types
For example, the basic structure of a qrcode, where:
Location Detection Image, Location Detection Image separator, and positioning image: used to locate the QR code. For each QR code, the location is fixed, but the size and specifications may vary;
Calibration image: the specification is determined, and the quantity and position of the correction image are also determined;
Format information: indicates the error correction level for modifying the QR code, which can be L, M, Q, and H;
Version information: the type of the QR code. There are 40 types of matrix (generally black and white), from 21x21 (version 1 ), to 177X177 (version 40), each version adds four modules to each side of the previous version.
Data and Error Correction Code: the information of the actually saved QR code, and the error code (used to correct the error caused by damage to the QR code ).
Brief Encoding Process:
1. data analysis: determines the encoding character type and converts it to a symbolic character based on the corresponding character set. If an error correction level is selected, the higher the error correction level, the smaller the actual data capacity.
2. Data Encoding: converts a data character into a bitstream. Each 8-bit code word represents a code word sequence of data. In fact, the data content of the QR code is known as the data code sequence.
Data can be encoded in one mode for more efficient decoding. For example, for data: 01234567 encoding (version 1-h ),
1) group: 012 345 67
2) convert to binary:
012 → 0000001100
345 → 0101011001
67 → 1000011
3) Conversion sequence: 0000001100 0101011001 1000011
4) convert the number of characters to binary: 8 to 0000001000
5) Add the mode indicator (number) 000:0001 0000001000 0000001100 0101011001 1000011
For letters, Chinese characters, and Japanese, only the group mode and mode are different. The basic methods are the same.
3. error Correction Code: blocks the above code sequence as needed, generates Error Correction codes based on the error correction level and the code segment, and adds the error correction code to the data code sequence, become a new sequence.
When the two-dimensional code specifications and error correction levels are determined, the total number of characters that can be accommodated by the two-dimensional code and the number of characters in the Error Correction Code are also determined. For example, in version 10, when the error correction level is H, A total of 346 code words are supported, including 224 error correction code words.
That is to say, about 1/3 of the Code in the QR code area is redundant. For these 224 error codes, it can correct 112 replacement errors (such as black and white inversion) or 224 data read errors (unreadable or undecoded ),
The error correction capacity is 112/346 = 32.4%.
4. Construct the final data information: place the sequence generated above in order, as shown in a chunk, when the specification is determined.
Data is segmented according to regulations, and each block is calculated to obtain the corresponding block of the Error Correction Code. The block of the Error Correction Code form a sequence in order and is added to the back of the original data code sequence.
For example, D1, D12, d23, D35, D2, D13, d24, d36 ,... d11, D22, d33, d45, d34, d46, E1, E23, e45, e67, E2, e24, e46, E68 ,...
Constructor: puts the probe image, separator, positioning image, correction image, and code module into the matrix.
Fill in the complete sequence above to the area of the QR code matrix of the corresponding specification.
6. Masking: the masking image is used in the Encoding Area of the symbol, so that the dark and light (black and white) areas in the QR code image can be evenly distributed.
7. Format and version information: Enter the format and version information in the corresponding region.
Both 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 is redundant.
The version information is 18 bits in total, with a 6x3 matrix. The data in six bits is, for example, version 8, the data bit information is 001000, And the next 12 bits are error correction bits.
Practice QR code encoding and decoding: (call the API for decoding and encoding in the third-party package zxing)
Encoding Process:
[Java]View plaincopy
- Public static void encode (string content, string format, string filepath ){
- Try {
- Hashtable hints = new hashtable (); // you can specify the encoding type.
- Hints. Put (encodehinttype. character_set, default_encoding); // code
- Bitmatrix = new qrcodewriter (). encode (content,
- Barcodeformat. qr_code, default_image_width,
- Default_image_height, hints );
- // Output to a file or stream
- File file = new file (filepath );
- Matrixtoimagewriter. writetofile (bitmatrix, format, file );
- } Catch (ioexception e ){
- E. printstacktrace ();
- } Catch (writerexception E1 ){
- E1.printstacktrace ();
- }
- }
Decoding process:
[Java]View plaincopy
- Bufferedimage image = ImageIO. Read (File); // read the 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 );