QR code generation and resolution example generate a QR code

Source: Internet
Author: User

A QR code is a bar code image that uses black and white plane ry to record text, images, URLs, and other information through the corresponding encoding algorithm. For example

Features of the QR code:

1. High-density coding, large information capacity

It can contain up to 1850 uppercase letters, 2710 numbers, 1108 bytes, or more than 500 Chinese characters, which is about dozens of times higher than the normal bar code information.

2. Wide encoding range

This bar code can encode pictures, sounds, text, signatures, fingerprints, and other digital information, which can be represented by a bar code. It can represent texts in multiple languages and image data.

3. Strong fault tolerance and Error Correction

This makes the two-dimensional bar code due to perforation, fouling, and other causes of local damage, still can be correctly read, the damaged area of up to 50% still can restore information.

4. High decoding Reliability

It is much lower than the error rate of normal bar code decoding, and the error rate cannot exceed one thousandth of a thousand.

5. encryption measures can be introduced

Good confidentiality and anti-counterfeiting.

6. low cost, easy to make, and durable

Because of these features, QR codes are becoming increasingly popular and widely used. (For more information, see Baidu encyclopedia. This article is not the focus, this blog post will show you how to generate and parse the QR code.

I. Java

Jar package: qrcode. Jar

: Http://download.csdn.net/detail/dengsilinming/5035963

Twodimensioncode class: QR code operation core class

Package qrcode; 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 twodimen Sioncode {/*** 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 * @ para M 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, imgt Ype, 7);}/*** generate a QR code (qrcode) 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. PR Intstacktrace () ;}}/*** 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 ();}}/ *** Generate a QR code (qrcode) 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. Options include L (7%), m (15%), and Q (25%) H (30%), the higher the error rate, the less information that can be stored, but the smaller the requirement on the definition of the QR code qrcodehandler. setqrcodeerrorcorrect ('M'); qrcodehandler. setqrcodeencodemode (' B '); // set the size of the two-dimensional code. The value ranges from 1 to 40. The larger the value, the larger the size, the larger the information that can be stored. 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. setcolor (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 ("qrc Ode content bytes 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 = imagei O. 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) * @ par Am 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 (decodi Ngfailedexception DFE) {system. out. println ("error:" + DFE. getmessage (); DFE. printstacktrace ();} return content;} public static void main (string [] ARGs) {string imgpath = "G:/tddownload/michael_qrcode.png "; string encodercontent = "Hello, big, small, welcome to qrcode! "+" \ Nmyblog [http://sjsky.iteye.com] "+" \ nemail [sjsky007@gmail.com] "; twodimensioncode handler = new twodimensioncode (); handler. encoderqrcode (encodercontent, imgpath, "PNG"); system. out. println ("======== encoder success"); string decodercontent = handler. decoderqrcode (imgpath); system. out. println ("Resolution Result:"); system. out. println (decodercontent); system. out. println ("========= decoder success !!! ");}}

Twodimensioncodeimage class: QR code image object

package qrcode;    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();      }  }  

OK to generate and parse the QR code.

Article conversion: QR code generation and resolution code implementation

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.