JAVA generates 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. Such as the characteristics of the QR code: 1. high-density encoding, large information capacity can accommodate up to 1850 uppercase letters, 2710 digits, 1108 bytes, or more than 500 Chinese characters, about dozens of times higher than the normal bar code information capacity. 2. A wide range of codes: This code can encode pictures, sounds, text, signatures, fingerprints, and other information that can be digitalized and expressed with codes. It can represent texts in multiple languages and image data. 3. Strong fault tolerance ability, with the error correction function, so that when the two-dimensional bar code is damaged due to perforation, fouling, etc., it can still be read correctly, and the damaged area is up to 50% square meters and information can still be restored. 4. High decoding Reliability: It is much lower than the error rate of normal bar code decoding. The error rate cannot exceed one thousandth. 5. encryption measures can be introduced to ensure confidentiality and prevent counterfeiting. 6. low cost, easy to make, and durable because of these features, QR codes are becoming more and more popular and applications are becoming wider and wider. (For details, refer to Baidu encyclopedia. This article does not focus on introduction ), therefore, it is a good knowledge reserve to know how to develop QR codes. Therefore, this blog post will explain how to generate and parse QR codes. Jar package: QRCode. jar TwoDimensionCode class: Two-dimensional code core class [java] 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; im Port 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 (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 (bufI Mg, 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 () ;}}/*** 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. setQrcodeErr OrCorrect ('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. 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 + pix Off, 3, 3) ;}}} else {throw new Exception ("QRCode 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 = 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. printStackTrac E ();} 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 (DecodingFailedException 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"); // 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 [java] 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 ();}

Related Article

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.