Topic
: Java uses Qrcode.jar to generate and parse two-dimensional code demoWelcome Novice Encouragement, the great God supervision
# do not know QRCode please visit the wiki, self-understanding, there is not much to explain
******* work before creating the QR code ********
Go to the address given below to download the Qrcode.jar package, which already includes the build and parse.
The jar package downloaded to the official website is not resolved.
Https://files.cnblogs.com/files/bigroc/QRCode.zip
* * * Create your test class to guide the jar pack start it * * *Part I: Generating two-dimensional code
PackageCom.bigroc.qrcode;ImportCom.swetake.util.Qrcode;ImportJavax.imageio.ImageIO;Importjava.awt.*;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;/*** Created by Bigroc on 2017/9/8.*/ Public classCreateqrcode { Public Static voidMain (string[] args)throwsIOException {//API documentation Specifies how to calculate the width of the image, 7 is the version number of this test intwidth = 67+12* (7-1); intHeight = 67+12* (7-1); QRCode x=NewQRCode (); X.setqrcodeerrorcorrect (' M ');//Error Correction levelX.setqrcodeencodemode (' B ');//( N: number) (A:a~z,a~z) (B: Others)X.setqrcodeversion (7);//Version number 1-40String qrdata = "WWW.BIGROC.XYZ";//Content Information byte[] D = qrdata.getbytes ("Utf-8");//Man to format needs to throw an exception//buffersBufferedImage BufferedImage =Newbufferedimage (width, height, bufferedimage.type_int_bgr); //DrawingGraphics2D GS =Bufferedimage.creategraphics (); Gs.setbackground (Color.White); Gs.setcolor (Color.Black); Gs.clearrect (0, 0, width, height); //Offset Amount intPixoff = 2; /*** Easy to step on the pit * Note the order of the i,j inside the For Loop, * S[j][i] The order of the j,i of the two-dimensional array to be associated with the Gs.fillrect in this method (J*3+pixoff, I*3+pixoff, 3, 3); * Sequential match, otherwise the parsing picture is a string of numbers*/ if(D.length > 0 && d.length < 120) { Boolean[] s =X.calqrcode (d); for(inti = 0; i < s.length; i++) { for(intj = 0; J < S.length; J + +) { if(S[j][i]) {Gs.fillrect (J* 3 + Pixoff, I * 3 + Pixoff, 3, 3); }}}} gs.dispose (); Bufferedimage.flush (); Imageio.write (BufferedImage,"PNG",NewFile ("D:/qrcode.png")); System.out.println ("Two-dimensional code generation complete"); }}
Part II: Parsing two-dimensional code(Note: There is a need to implementQrcodeimage Interface)
PackageCom.bigroc.qrcode;ImportJp.sourceforge.qrcode.QRCodeDecoder;ImportJavax.imageio.ImageIO;ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException;/*** Created by Bigroc on 2017/9/8.*/ Public classReadqrcode { Public Static voidMain (string[] args)throwsIOException {//Picture PathFile File =NewFile ("D:/qrcode.png"); //reading a picture to a bufferBufferedImage BufferedImage =imageio.read (file); //QRCode DecoderQrcodedecoder Codedecoder =NewQrcodedecoder (); /***codedecoder.decode (New Myqrcodeimage ()) * The Qrcodeimage interface needs to be implemented here, and the last piece of code*/ //get information by parsing a QR codeString result =NewString (Codedecoder.decode (NewMyqrcodeimage (BufferedImage)), "Utf-8"); SYSTEM.OUT.PRINTLN (result); }}
Part III: Implementation of the Qrcodeimage interface
PackageCom.bigroc.qrcode;ImportJp.sourceforge.qrcode.data.QRCodeImage;ImportJava.awt.image.BufferedImage;/*** Implement Qrcodeimage interface, * Set the decoded picture information * Tell Codedecoder.decode () the image type to be parsed * Created by Bigroc on 2017/9/9.*/ Public classMyqrcodeimageImplementsqrcodeimage{bufferedimage BufferedImage; Publicmyqrcodeimage (BufferedImage bufferedimage) { This. bufferedimage=BufferedImage; } //Wide@Override Public intgetwidth () {returnbufferedimage.getwidth (); } //High@Override Public intgetheight () {returnbufferedimage.getheight (); } //pixel or color?@Override Public intGetPixel (intIintj) {returnBufferedimage.getrgb (I,J); }}
Important!! A very important part!!! The problems encountered in development are explained in the code,If there are other questions in the test, please feel free to comment or ask questions!
Java uses Qrcode.jar to generate and parse two-dimensional code