1. Support QRcode and ZXing QR code generation and analysis;
2. QRCode generates QR codes and supports adding pictures, as follows:
package common; import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;image java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.util.Map;import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder;import jp.sourceforge.qrcode.exception.DecodingFailedException; import com.google.zxing.BarcodeFormat;import com.google.zxing.Binarizer;import com.google.zxing.BinaryBitmap;import com.google.zxing.EncodeHintType;import com.google.zxing.LuminanceSource;import com.google.zxing.MultiFormatReader;import com.google.zxing.MultiFormatWriter;import com.google.zxing.NotFoundException;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.common.HybridBinarizer;import com.swetake.util.Qrcode; /** * QR code generation tool class* @author Cloud * @data 2016-12-15 * QRCode */ public class QRCodeUtil { //QR code color private static final int BLACK = 0xFF000000; //QR code color private static final int WHITE = 0xFFFFFFFF; /** * <span style="font-size:18px;font-weight:blod;">ZXing method generates QR code</span> * @param text <a href="javascript:void();" rel="external nofollow" >QR code content</a> * @param width QR code width* @param height QR code height* @param outPutPath QR code generation and save path* @param imageType QR code generation format*/ public static void zxingCodeCreate(String text, int width, int height, String outPutPath, String imageType){ Map<EncodeHintType, String> his = new HashMap<EncodeHintType, String>(); //Set the encoded character set his.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { //1. Generate QR code BitMatrix encode = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, his); //2. Get the QR code width and height int codeWidth = encode.getWidth(); int codeHeight = encode.getHeight(); //3. Put the QR code into the buffered stream BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB); for (int i = 0; i < codeWidth; i++) { for (int j = 0; j < codeHeight; j++) { //4. Loop to fix the QR code content into the image image.setRGB(i, j, encode.get(i, j) ? BLACK : WHITE); } } File outPutImage = new File(outPutPath); //If the image does not exist, create the image if(!outPutImage.exists()) outPutImage.createNewFile(); //5. Write the QR code to the image ImageIO.write(image, imageType, outPutImage); } catch (WriterException e) { e.printStackTrace(); System.out.println("QR code generation failed"); } catch (IOException e) { e.printStackTrace(); System.out.println("Change QR code image failed"); } } /** * <span style="font-size:18px;font-weight:blod;">QR code analysis</span> * @param analyzePath QR code path* @return * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static Object zxingCodeAnalyze(String analyzePath) throws Exception{ MultiFormatReader formatReader = new MultiFormatReader(); Object result = null; try { File file = new File(analyzePath); if (!file.exists()) { return "The QR code does not exist"; } BufferedImage image = ImageIO.read(file); LuminanceSource source = new LuminanceSourceUtil(image); Binarizer binarizer = new HybridBinarizer(source); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); result = formatReader.decode(binaryBitmap, hints); } catch (NotFoundException e) { e.printStackTrace(); } return result; } /** * <span style="font-size:18px;font-weight:blod;">QRCode generates QR code</span> * @param content QR code content* @param imgPath QR code generation path* @param version QR code version* @param isFlag Whether to generate a logo image as NULL does not generate*/ public static void QRCodeCreate(String content, String imgPath, int version, String logoPath){ try { Qrcode qrcodeHandler = new Qrcode(); //Set the QR code error-decoding rate, optional L(7%) M(15%) Q(25%) H(30%). The higher the error-decoding rate, the less information can be stored, but the smaller the requirement for the clarity of the QR code qrcodeHandler.setQrcodeErrorCorrect('M'); //N represents the number, A represents the character aZ, and B represents other characters qrcodeHandler.setQrcodeEncodeMode('B'); // Version 1 is a 21*21 matrix, and for every 1 increase in version, the two sides of the QR code are increased by 4; so version 7 is a matrix with 45*45; the highest version is 40, which is a matrix with 177*177 qrcodeHandler.setQrcodeVersion(version); // Calculate the size according to version int imgSize = 67 + 12 * (version - 1) ; byte[] contentBytes = content.getBytes("gb2312"); BufferedImage bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize , imgSize); // Set image color> BLACK gs.setColor(Color.BLACK); // Set offset not to set may cause parsing error int pixoff = 2; // Output content> QR code if (contentBytes.length > 0 && contentBytes.length < 130) { 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 { System.err.println("QRCode content bytes length = " + contentBytes.length + " not in [ 0,130 ]. "); } /* Determine whether you need to add a logo image*/ if(logoPath != null){ File icon = new File(logoPath); if(icon.exists()){ int width_4 = imgSize / 4; int width_8 = width_4 / 2; int height_4 = imgSize / 4; int height_8 = height_4 / 2; Image img = ImageIO.read(icon); gs.drawImage(img, width_4 + width_8, height_4 + height_8, width_4, height_4, null); gs.dispose(); bufImg.flush(); }else{ System.out.println("Error: login image does not exist!"); } } gs.dispose(); bufImg.flush(); //Create QR code file File imgFile = new File(imgPath); if(!imgFile.exists()) imgFile.createNewFile(); //Get the image from the generated image String imgType = imgPath.substring(imgPath.lastIndexOf(".") + 1, imgPath.length()); //Generate QR code QRCode image ImageIO.write(bufImg, imgType, imgFile); } catch (Exception e) { e.printStackTrace(); } } /** * <span style="font-size:18px;font-weight:blod;">QRCode QR code analysis</span> * @param codePath QR code path* @return parsing result*/ public static String QRCodeAnalyze(String codePath) { File imageFile = new File(codePath); BufferedImage bufImg = null; String decodedData = null; try { if(!imageFile.exists()) return "The QR code does not exist"; bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); decodedData = new String(decoder.decode(new ImageUtil(bufImg)), "gb2312"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return decodedData; } } 3. Last post test code: package test; import java.awt.image.BufferedImage;import java.io.InputStream;import java.net.URL; import javax.imageio.ImageIO; import common.ImageUtil; import common.QRCodeUtil; import jp.sourceforge.qrcode.QRCodeDecoder; /** * QR code generation test class* @author Cloud * @data 2016-11-21 * QRCodeTest */ public class QRCodeTest { public static void main(String[] args) throws Exception { /** * QRcode QR code generation test* QRCodeUtil.QRCodeCreate("http://blog.csdn.net/u014266877", "E://qrcode.jpg", 15, "E://icon.png"); */ /** * QRcode QR code analysis test* String qrcodeAnalyze = QRCodeUtil.QRCodeAnalyze("E://qrcode.jpg"); */ /** * ZXingCode QR code generation test* QRCodeUtil.zxingCodeCreate("http://blog.csdn.net/u014266877", 300, 300, "E://zxingcode.jpg", "jpg"); */ /** * ZxingCode QR code analysis* String zxingAnalyze = QRCodeUtil.zxingCodeAnalyze("E://zxingcode.jpg").toString(); */ System.out.println("success"); }}