Java QR code generation and parsing, java Parsing
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
TwoDimensionCode class: QR code operation core class
TwoDimensionCode. java
Package com. QRCode. demo; import java. awt. color; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. file; import java. io. fileOutputStream; 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. encode RQRCode (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 () ;}}/*** 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'); qrcode Handler. 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, I MgSize, 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) ;}}} el Se {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. printStackTrace ();} return content; }/*** Resolve 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. print StackTrace ();} catch (DecodingFailedException dfe) {System. out. println ("Error:" + dfe. getMessage (); dfe. printStackTrace ();} return content;} public static void main (String [] args) {String imgPath = "E:/5.jpg"; String encoderContent =" try "; // String content = "E:/5.jpg"; TwoDimensionCode handler = new TwoDimensionCode (); handler. encoderQRCode (encoderContent, imgPath, "png");/* try {OutputStre Am 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 !!! ");}}
Package com. QRCode. demo; import java. awt. color; import java. awt. graphics2D; import java. awt. image. bufferedImage; import java. io. file; import java. io. fileOutputStream; 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. encode RQRCode (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 () ;}}/*** 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'); qrcode Handler. 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, I MgSize, 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) ;}}} el Se {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. printStackTrace ();} return content; }/*** Resolve 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. print StackTrace ();} catch (DecodingFailedException dfe) {System. out. println ("Error:" + dfe. getMessage (); dfe. printStackTrace ();} return content;} public static void main (String [] args) {String imgPath = "E:/5.jpg"; String encoderContent =" try "; // String content = "E:/5.jpg"; TwoDimensionCode handler = new TwoDimensionCode (); handler. encoderQRCode (encoderContent, imgPath, "png");/* try {OutputStre Am 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. java
package com.QRCode.demo;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(); }}