QR code _ encode and decode

Source: Internet
Author: User

QR code encode and decode Tool


Import Java. AWT. color; import Java. AWT. graphics2d; import Java. AWT. image. bufferedimage; import Java. io. file; import Java. io. ioexception; import Java. io. inputstream; import Java. io. outputstream; import javax. imageIO. imageIO; import JP. sourceForge. qrcode. qrcodedecoder; import JP. sourceForge. qrcode. exception. decodingfailedexception; import COM. swetake. util. qrcode; public class twodimensioncode {/*** Generate a QR code (qrcode) image * @ Param content storage content * @ Param imgpath image path */Public void encoderqrcode (string content, string imgpath) {This. encoderqrcode (content, imgpath, "PNG", 7);}/*** generate a QR code (qrcode) image * @ Param content storage content * @ Param output stream */Public void encoderqrcode (string content, outputstream output) {This. encoderqrcode (content, output, "PNG", 7);}/*** generate a QR code (qrcode) image * @ Param content storage content * @ Param imgpath image path * @ Param imgtype image type */Public void encoderqrcode (string content, string imgpath, string imgtype) {This. encoderqrcode (content, imgpath, imgtype, 7);}/*** generate a QR code (qrcode) image * @ Param content storage content * @ Param output stream * @ Param imgtype image type */Public void encoderqrcode (string content, outputstream output, string imgtype) {This. encoderqrcode (content, output, imgtype, 7 );}/*** Generate a QR code image * @ Param content storage content * @ Param imgpath image path * @ Param imgtype image type * @ Param size QR code size */Public void encoderqrcode (string content, string imgpath, string imgtype, int size) {try {bufferedimage bufimg = This. qrcodecommon (content, imgtype, size); file imgfile = new file (imgpath); // generate a QR code qrcode image ImageIO. write (bufimg, imgtype, imgfile);} catch (exception e) {e. printstacktrace (); }/*** Generate a QR code (qrcode) image * @ Param content storage content * @ Param output stream * @ Param imgtype image type * @ Param size QR code size */Public void encoderqrcode (string content, outputstream output, string imgtype, int size) {try {bufferedimage bufimg = This. qrcodecommon (content, imgtype, size); // generates the QR code qrcode image ImageIO. write (bufimg, imgtype, output);} catch (exception e) {e. printstacktrace () ;}}/*** generates a QR code (qrcod E) Public Image Method * @ Param content storage content * @ Param imgtype image type * @ Param size QR code size * @ return */private bufferedimage qrcodecommon (string content, string imgtype, int size) {bufferedimage bufimg = NULL; try {qrcode qrcodehandler = new qrcode (); // set the error rate of the QR code. Optional values: L (7%), m (15%) q (25%), H (30%), the higher the error rate, the less information that can be stored, but the smaller the requirements for the definition of the QR code qrcodehandler. setqrcodeerrorcorrect ('M'); qrcodehandler. setqrcodeencodemode ('B'); // you can specify the QR code ruler. The value range is 1-40. The larger the value, the larger the size, the larger the information that can be stored. qrcodehandler. setqrcodeversion (size); // obtains the byte array of the content, and sets the encoding Format byte [] contentbytes = content. getbytes ("UTF-8"); // image size int imgsize = 67 + 12 * (size-1); bufimg = new bufferedimage (imgsize, imgsize, bufferedimage. type_int_rgb); graphics2d GS = bufimg. creategraphics (); // sets the background color Gs. setbackground (color. white); GS. clearrect (0, 0, imgsize, imgsize); // sets the image color> black Gs. setcolo R (color. black); // set the offset. If this parameter is not set, the parsing error int pixoff = 2 may occur. // The output content> QR code if (contentbytes. length> 0 & contentbytes. length <800) {Boolean [] [] codeout = qrcodehandler. calqrcode (contentbytes); For (INT I = 0; I <codeout. length; I ++) {for (Int J = 0; j <codeout. length; j ++) {If (codeout [J] [I]) {Gs. fillrect (J * 3 + pixoff, I * 3 + pixoff, 3, 3) ;}}} else {Throw new exception ("qrcode content byte S length = "+ contentbytes. length + "not in [0,800]. ");} Gs. dispose (); bufimg. flush ();} catch (exception e) {e. printstacktrace ();} return bufimg;}/*** parse the QR code (qrcode) * @ Param imgpath image path * @ return */Public String decoderqrcode (string imgpath) {// file imagefile = new file (imgpath); bufferedimage bufimg = NULL; string content = NULL; try {bufimg = ImageIO. read (imagefile ); Qrcodedecoder decoder = new qrcodedecoder (); content = new string (decoder. decode (New twodimensioncodeimage (bufimg), "UTF-8");} catch (ioexception e) {system. out. println ("error:" + E. getmessage (); E. printstacktrace ();} catch (decodingfailedexception DFE) {system. out. println ("error:" + DFE. getmessage (); DFE. printstacktrace ();} return content;}/*** parse the QR code (qrcode) * @ Param input stream *@ Return */Public String decoderqrcode (inputstream input) {bufferedimage bufimg = NULL; string content = NULL; try {bufimg = ImageIO. read (input); qrcodedecoder decoder = new qrcodedecoder (); content = new string (decoder. decode (New twodimensioncodeimage (bufimg), "UTF-8");} catch (ioexception e) {system. out. println ("error:" + E. getmessage (); E. printstacktrace ();} catch (decodingfailedexceptio N dfe) {system. out. println ("error:" + DFE. getmessage (); DFE. printstacktrace ();} return content;} public static void main (string [] ARGs) {string imgpath = "E:/abc.png"; string encodercontent = "http://blog.csdn.net/benjamin_whx "; twodimensioncode handler = new twodimensioncode (); handler. encoderqrcode (encodercontent, imgpath, "PNG"); // try {// outputstream output = new fileoutputstream (imgp ATH); // handler. encoderqrcode (content, output); //} catch (exception e) {// E. printstacktrace (); //} system. out. println ("======== encoder success"); string decodercontent = handler. decoderqrcode ("E:/aaa.jpg"); system. out. println ("Resolution Result:"); system. out. println (decodercontent); system. out. println ("========= decoder success !!! ");}}


Twodimensioncodeimage object class


import java.awt.image.BufferedImage;    import jp.sourceforge.qrcode.data.QRCodeImage;    public class TwoDimensionCodeImage implements QRCodeImage {        BufferedImage bufImg;            public TwoDimensionCodeImage(BufferedImage bufImg) {          this.bufImg = bufImg;      }            @Override      public int getHeight() {          return bufImg.getHeight();      }        @Override      public int getPixel(int x, int y) {          return bufImg.getRGB(x, y);      }        @Override      public int getWidth() {          return bufImg.getWidth();      }    }  


Jar package download http://download.csdn.net/detail/u012959829/7941481

QR code _ encode and decode

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.