Java implements encoding and decoding of QR code QRCode

Source: Internet
Author: User

Related to some of the main class library, convenient for everyone to download:

Code Lib:Qrcode_swetake.jar (Official website--http://www.swetake.com/qr/index-e.html)

Decoding Lib:qrcode.jar (official website--http://sourceforge.jp/projects/qrcode/)

"One", Code:

Java Code Qrcodeencoderhandler.java

 PackageMichael.qrcode; ImportJava.awt.Color;ImportJava.awt.Graphics2D;ImportJava.awt.image.BufferedImage;ImportJava.io.File; ImportJavax.imageio.ImageIO; ImportCom.swetake.util.Qrcode; /*** QR code generator * @bloghttp://sjsky.iteye.com * @authorMichael*/  Public classQrcodeencoderhandler {/*** Generate QR Code (qrcode) image *@paramcontent *@paramImgpath*/      Public voidEncoderqrcode (string content, String imgpath) {Try{qrcode Qrcodehandler=NewQRCode (); Qrcodehandler.setqrcodeerrorcorrect (M); Qrcodehandler.setqrcodeencodemode (B); Qrcodehandler.setqrcodeversion (7);             SYSTEM.OUT.PRINTLN (content); byte[] contentbytes = Content.getbytes ("gb2312"); BufferedImage 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].");             } gs.dispose ();              Bufimg.flush (); File Imgfile=NewFile (Imgpath); //generate two-dimensional code QRCode picturesImageio.write (bufimg, "PNG", Imgfile); } Catch(Exception e) {e.printstacktrace (); }      }      /**     * @paramargs the command line arguments*/      Public Static voidMain (string[] args) {String Imgpath= "D:/test/twocode/michael_qrcode.png"; String content= "Hello big, small, welcome to qrcode!" + "\nmyblog [http://sjsky.iteye.com]" + "\nemail [[email protected]]" + "\ntwitter [@suncto]"; Qrcodeencoderhandler Handler=NewQrcodeencoderhandler ();          Handler.encoderqrcode (content, Imgpath); System.out.println ("Encoder QRCode Success"); } } 

The image of the two-dimensional code generated after running is as follows:

At this point on the use of mobile phone QR code scanning software (I use: Android fast QR code) to test, the recognition of the following success:

Favorite friends can download after a try, do some business cards or things you like. Of course, Java can also be the QR code image decoding, specific to see the following about decoding content.

"Two", decoding:

Java Code Qrcodedecoderhandler.java

 PackageMichael.qrcode; ImportJava.awt.image.BufferedImage;ImportJava.io.File;Importjava.io.IOException; ImportJavax.imageio.ImageIO; ImportJp.sourceforge.qrcode.QRCodeDecoder;ImportJp.sourceforge.qrcode.data.QRCodeImage;Importjp.sourceforge.qrcode.exception.DecodingFailedException; /*** @bloghttp://sjsky.iteye.com * @authorMichael*/  Public classQrcodedecoderhandler {/*** decode QR code *@paramImgpath *@returnString*/      Publicstring Decoderqrcode (String imgpath) {//QRCode Two-dimensional code picture fileFile ImageFile =NewFile (Imgpath); BufferedImage bufimg=NULL; String Decodeddata=NULL; Try{bufimg=Imageio.read (ImageFile); Qrcodedecoder Decoder=NewQrcodedecoder (); Decodeddata=NewString (Decoder.decode (Newj2seimage (bufimg)); //try {//System.out.println (New String (Decodeddata.getbytes ("gb2312"),//"gb2312")); //} catch (Exception e) {// //Todo:handle Exception// } }Catch(IOException e) {System.out.println ("Error:" +e.getmessage ());         E.printstacktrace (); } Catch(Decodingfailedexception DfE) {System.out.println ("Error:" +dfe.getmessage ());         Dfe.printstacktrace (); }         returnDecodeddata; }      /**     * @paramargs the command line arguments*/      Public Static voidMain (string[] args) {Qrcodedecoderhandler handler=NewQrcodedecoderhandler (); String Imgpath= "D:/test/twocode/michael_qrcode.png"; String decodercontent=Handler.decoderqrcode (Imgpath); System.out.println (The results of the analysis are as follows:);         System.out.println (decodercontent); System.out.println ("========decoder Success!!!"); }      classJ2seimageImplementsqrcodeimage {bufferedimage bufimg;  Publicj2seimage (BufferedImage bufimg) { This. bufimg =bufimg; }           Public intgetwidth () {returnbufimg.getwidth (); }           Public intgetheight () {returnbufimg.getheight (); }           Public intGetPixel (intXinty) {returnBufimg.getrgb (x, y); }      } } 

The result of the operation is as follows (the decoded content is consistent with the previously entered content):

The parsing results are as follows:

Hello big, small, welcome to qrcode!

MyBlog [http://sjsky.iteye.com]

EMail [sjsky007@gmail.com]

Twitter [@suncto]

========decoder Success!!!

Java implements encoding and decoding of QR code QRCode

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.