Today to learn how to generate two-dimensional code, the online QR code generated a lot of tutorials, there are using Google Zxing.jar, there is also the use of Qrcode.jar. I am using Qrcode.jar, the code from the online tutorial. The code is not long and is well understood. Personal level is lower, also take doctrine, write an essay for future use. Generate two-dimensional code codes as follows:
Try{qrcode Qrcodehandler=NewQRCode (); //set the two-dimensional code error rate, optional L (7%), M (15%), Q (25%), H (30%), the higher the error rate can be stored less information, but the two-dimensional code clarity requirements of the smallerQrcodehandler.setqrcodeerrorcorrect (' M '); Qrcodehandler.setqrcodeencodemode (B); //set the size of the two-dimensional code, the value range of 1-40, the larger the size of the larger, the more information can be storedQrcodehandler.setqrcodeversion (7); //get a byte array of content, set the encoding format byte[] contentbytes = Content.getbytes ("Utf-8"); //constructs a BufferedImage object set wide, highBufferedImage bufimg =NewBufferedImage (140, 140, Bufferedimage.type_int_rgb); Graphics2D GS=Bufimg.creategraphics ();
Gs.setbackground (Color.White); Gs.clearrect (0, 0, 140, 140); //set Image Color BLACKGs.setcolor (Color.Black); //setting offset does not set may cause parsing errors intPixoff = 2; //output content QR code if(Contentbytes.length > 0 && contentbytes.length < 120) { Boolean[] Codeout =Qrcodehandler.calqrcode (contentbytes); for(inti = 0; i < codeout.length; i++) { for(intj = 0; J < Codeout.length; J + +) { if(Codeout[j][i]) {Gs.fillrect (J* 3 + Pixoff, I * 3 + Pixoff, 3, 3); } } } } Else{System.err.println ("QRCode Content Bytes Length =" + Contentbytes.length + "not in [0,120]."); return-1; } //instantiates an Image object. Image img = Imageio.read (NewFile (logo)); Gs.drawimage (IMG,44, 55, 49, 30,NULL); Gs.dispose (); Bufimg.flush (); //generate two-dimensional code QRCode picturesFile Imgfile =NewFile (Imgpath); Imageio.write (bufimg,"JPG", Imgfile); }Catch(Exception e) {e.printstacktrace (); return-100; }
This is the code, encapsulated as a method, as long as the argument is called.
Java uses Qrcode.jar to make two-dimensional code with logo