Java implementation to generate two-dimensional code

Source: Internet
Author: User

Java implementation to generate two-dimensional code

QR code vs Barcode

The biggest difference is: two-dimensional code has a fault-tolerant function, when the two-dimensional code image is obscured part, can still be scanned out. The principle of fault tolerance is that the QR code in the coding process is redundant, like 123 is encoded into 123123, so as long as the scan to a part of the QR code image, the QR code content can be read all.

The two-dimensional code fault tolerance is the ability of the two-dimensional code icon to be obscured, and can still be scanned. The higher the fault tolerance, the more parts of the two-dimensional code image can be obscured.

Two-dimensional code fault tolerance is expressed in letters, fault tolerance level is divided into: L, M, Q, H four:

L 7%

M 15%

Q 25%

H 30%

In most cases, we strongly recommend a 30% tolerance rate. For this we have done various models of mobile phone scanning test. The result: For the current mainstream mobile phones, in the vast majority of scanning scenarios, the higher the fault tolerance, the more easy to scan quickly!!!

If you need to design two-dimensional code, such as the need to be in the middle of the two-dimensional code logo, you need to choose the H-level fault tolerance of the two-dimensional code. Of course, the cost of high fault tolerance is that the complexity of the picture increases. The appropriate level of fault tolerance should be selected in the actual operation according to the requirements.

Note that the three-position and middle-positioned blocks on the two-dimensional code edge cannot be obscured. Otherwise, the fault-tolerant rate is high and cannot be scanned.

So supermarkets when the supermarket inside the barcode of the goods on the damage event, is not to be scanned out, it is necessary for Gong staff to enter the barcode number of the computer to calculate the price.

Realization of two-dimensional code ideas:

1. First introduced two packages, Qrcode_a.rar and Qrcode_b.rar two packets, these two packages have realized the creation of two-dimensional code, computing two-dimensional code of many methods, only need to apply to use it, very convenient.

2. Create a buffer picture;

3. Create a brush from the buffer picture;

4.g.clearrect (0, 0, width, height); Clear the screen, ready to draw two-dimensional code;

5. Set up the information of the last realization of the QR code;

6. Convert the information you want to implement into a byte array;

7. Use a Boolean two-dimensional array to store the two-dimensional code, if True then black, false is left white;

Code implementation:

 PackageQRCode;ImportJava.awt.Color;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;ImportJavax.imageio.ImageIO;ImportCom.swetake.util.Qrcode; Public classCreatqrcode {/** Generate two-dimensional code*/     Public Static voidMain (string[] args)throwsunsupportedencodingexception {qrcode QRCode=NewQRCode (); //set error correction levelQrcode.setqrcodeerrorcorrect (' m '); Qrcode.setqrcodeencodemode (' B ');//N Digit A A-z B Chinese, Japanese, etc.Qrcode.setqrcodeversion (10);//Version number 1-40        /** GUI Programming*/        //The version number is associated with the length and width of the picture: This is a fixed formula        intwidth=67+12* (10-1); intheight=67+12* (10-1); //set the offset to correct the error of the two-dimensional code at parse time        intpianyiliang=2; //buffer PictureBufferedImage buffimmage=Newbufferedimage (width, height, bufferedimage.type_int_rgb); //Create a brush based on a buffer pictureGraphics2D g=Buffimmage.creategraphics ();        G.setbackground (Color.White);        G.setcolor (Color.Black); G.clearrect (0, 0, width, height); //start drawing two-dimensional code        /** Set QR code information*/String str= "http://47.93.198.182";//This is a programmer's confession page//Convert a string into a byte array        byte[] Bytes=str.getbytes ("Utf-8"); if(bytes.length>0){            //A two -dimensional array with a Boolean type is used to store the QR code, and if true it is black and false remains white. Qrcode.calqrcode (bytes); Is the method to calculate the information of the QR code            Boolean[] bool=Qrcode.calqrcode (bytes); //long and wide are the same             for(inti=0;i<bool.length;i++){                 for(intj=0;j<bool[i].length;j++){                    if(bool[i][j]==true){                        //draw a small squareG.fillrect (I*3+pianyiliang, J*3+pianyiliang, 3, 3);        }}}} g.dispose ();                        Buffimmage.flush (); //CTRL 1: Shortcut keys that prompt for errors            Try{imageio.write (buffimmage,"PNG",NewFile ("D://a1.png")); } Catch(IOException e) {e.printstacktrace (); }        }            }

NOTES:

1. The version number of the two-D code is associated with the length and width of the image, and there is a fixed formula: 67+12* (version number-1);

2. In the drawing of small squares, in order to correct the two-dimensional code in the resolution of the error, you need to set a certain amount of offset, g.fillrect (I*3+pianyiliang, J*3+pianyiliang, 3, 3);

3. After you finish drawing the QR code, the brushes and buffer pictures are released.

Operation Result:

Java implementation to generate two-dimensional code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.