QR code generation and resolution code implementation

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/wangpeng047/4008532

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 twodimensioncode {/* ** Generate a QR code 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 * @ par Am 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 (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. 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 () ;}}/*** public method for generating a QR code (qrcode) image * @ 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%), and H (30% ), the higher the troubleshooting rate, the less information that can be stored, but the smaller the requirement on the definition of the QR code. setqrcodeerrorcorrect ('M'); qrcodehandler. setqrcodeencodemode ('B'); // set the two-dimensional code size. The value range is 1-40. The larger the value, the larger the size, the larger the information that can be stored. qrcodehandler. s Etqrcodeversion (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> blackgs. setcolor (color. black); // set the offset. If this parameter is not set, the parsing error int pixoff = 2 may occur. // enter 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 bytes length =" + contentbytes. length + "not in [0,800]. ");} Gs. dispos E (); 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 (ne W 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 con Tent = 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 (decodingfailedexception DFE) {system. out. println ("error:" + DFE. getmessage (); DFE. printstacktrace ();} return content;} public St Atic 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"); // try {// outputstream output = new fileoutputstream (imgpath); // handler. encoderqrcode (content, output); //} catch (exception e) {// E. printstacktrace (); //} 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;}@Overridepublic int getHeight() {return bufImg.getHeight();}@Overridepublic int getPixel(int x, int y) {return bufImg.getRGB(x, y);}@Overridepublic int getWidth() {return bufImg.getWidth();}}

 

Ii.. net

Required DLL: thoughtworks. qrcode

Http://download.csdn.net/detail/wangpeng047/4008536

The code here is similar to the above Java, the source code is not given here, you can refer to the http://download.csdn.net/detail/ywjq/3454011

 

The above Java code has been simply encapsulated to make it easier for you to use on different occasions. I hope it will help you.

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.