QR code is a barcode picture that uses black and white plane geometric figures to record text, pictures, URLs and other information through corresponding coding algorithms. The following figure
Features of QR code:
1. High-density coding, large information capacity
It can accommodate up to 1850 capital letters or 2710 numbers or 1108 bytes, or more than 500 Chinese characters, which is about dozens of times higher than ordinary barcode information.
2. Wide encoding range
This barcode can encode digitized information such as pictures, sounds, texts, signatures, fingerprints, etc., and express them in barcodes; they can represent text in multiple languages; and they can represent image data.
3. Strong fault tolerance and error correction function
This allows the two-dimensional barcode to be properly damaged due to perforation, defilement, etc., and the information can still be recovered if the damage area reaches 50%.
4. High decoding reliability
It is much lower than the error rate of ordinary barcode decoding of two-parties per million, and the error rate is no more than one-parties per million.
5. Encryption measures can be introduced
Good confidentiality and anti-counterfeiting.
6. Low cost, easy to make, durable
Because of the above characteristics, QR codes are becoming more and more popular and are more widely used (see Baidu Encyclopedia for details, the introduction is not the focus of this article). Therefore, mastering how to develop QR codes is a very good knowledge reserve. Therefore, this article will explain to you how to generate and analyze QR codes.
1. Java
Required jar package: QRCode.jar
http://sourceforge.jp/projects/qrcode/
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 QR code (QRCode) picture* @param content Store content* @param imgPath Picture path*/ public void encoderQRCode(String content, String imgPath) { this.encoderQRCode(content, imgPath, "png", 7); } /** * Generate QR code (QRCode) picture* @param content Store content* @param output Output Stream*/ public void encoderQRCode(String content, OutputStream output) { this.encoderQRCode(content, output, "png", 7); } /** * Generate QR code (QRCode) picture* @param content Store content* @param imgPath Picture path* @param imgType Picture type*/ public void encoderQRCode(String content, String imgPath, String imgType) { this.encoderQRCode(content, imgPath, imgType, 7); } /** * Generate QR code (QRCode) picture* @param content Store content* @param output Output stream* @param imgType Image type*/ public void encoderQRCode(String content, OutputStream output, String imgType) { this.encoderQRCode(content, output, imgType, 7); } /** * Generate QR code (QRCode) image* @param content Store content* @param imgPath Picture 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 QR code QRCode image ImageIO.write(bufImg, imgType, imgFile); } catch (Exception e) { e.printStackTrace(); } } /** * Generate QR code (QRCode) image* @param content Store content* @param output 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); // Generate QR code QRCode image ImageIO.write(bufImg, imgType, output); } catch (Exception e) { e.printStackTrace(); } } /** * Public method to generate QR code (QRCode) images* @param content Store 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 QR code error rate, which can be selected as L (7%), M (15%), Q (25%), and H (30%). The higher the error rate, the less information can be stored, but the smaller the requirement for QR code clarity qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); // Set the QR code size, with a value range of 1-40. The larger the value, the larger the size, the larger the information you can store qrcodeHandler.setQrcodeVersion(size); // Get the byte array of content and set 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(); // Set background color gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize); // Set image color> BLACK gs.setColor(Color.BLACK); // Set offset, not setting may cause parsing errors int pixoff = 2; // 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 bytes length = " + contentBytes.length + " not in [0, 800]."); } gs.dispose(); bufImg.flush(); } catch (Exception e) { e.printStackTrace(); } return bufImg; } /** * Analyze QR code (QRCode) * @param imgPath Image path * @return */ public String decoderQRCode(String imgPath) { // QRCode QR code image file 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; } /** * Parsing QR code (QRCode) * @param input 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 [ [email protected] ]"; 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("The parsing result is as follows:"); 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(); }}The above is the information for the generation and analysis of Java QR codes. We will continue to add them later. Thank you for your support for this website!