Reference address: https://www.cnblogs.com/manusas/p/6801436.html
package com.wfz.zxing; import com.google.zxing.*; import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.util.Hashtable; import java.util.Map; /** * Created by Liang on 5/3/2017. */ public class ZxingUtils { /** * Generate QR code * * @param contents * @param width * @param height * @param imgPath */ public void encodeQRCode(String contents, int width, int height, String imgPath) { Map<EncodeHintType, Object> hints = new Hashtable<>(); // Specify the error correction level hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // Specify the encoding format hints.put(EncodeHintType.CHARACTER_SET, "GBK"); try { //To generate a QR code at the end of QR_CODE, pay attention to the greater the height and width of information, the denser the QR code BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath)); } catch (Exception e) { e.printStackTrace (); } } /** * Parse QR code * * @param imgPath * @return */ public String decodeQRCode(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Map<DecodeHintType, Object> hints = new Hashtable<>(); hints.put(DecodeHintType.CHARACTER_SET, "GBK"); result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } catch (Exception e) { e.printStackTrace (); } return null; } /** * Generate barcode * * @param contents * @param width * @param height * @param imgPath */ // int width = 105, height = 50; length is easy to report error: NotFoundException public void encodeBarCode(String contents, int width, int height, String imgPath) { int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); try { //The barcode seems to only be able to write the information of the barcode fixed rules BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null); MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath)); } catch (Exception e) { e.printStackTrace (); } } /** * Parse barcode * * @param imgPath * @return */ public String decodeBarCode(String imgPath) { BufferedImage image = null; Result result = null; try { image = ImageIO.read(new File(imgPath)); if (image == null) { System.out.println("the decode image may be not exit."); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); /* Map<DecodeHintType, Object> hints = new Hashtable<>(); hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); result = new MultiFormatReader().decode(bitmap, hints);*/ result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } catch (Exception e) { e.printStackTrace (); } return null; } }Test: Pay special attention to the height and width, and it is easy to report errors: NotFoundException
public static void main(String[] args) throws Exception { String imgPath = "D:\\test.png"; // barcode of Yida sugar-free chewing gum String contents = "6923450657713"; int width = 105, height = 50; ZxingUtils handler = new ZxingUtils(); handler.encodeBarCode(contents, width, height, imgPath); String barcode = handler.decodeBarCode(imgPath); System.out.println(barcode); handler.encodeQRCode("abc123中文@#\\", 200, 200, imgPath); String qrcode = handler.decodeQRCode(imgPath); System.out.println(qrcode); }
The above is the reference information
This is my tool class important information
// Barcode encoding rule setting (character set) Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "GBK"); // barcode encoding String info = strCode.toString(); //BarcodeFormat.PDF_417 This is encrypted barcode information //info is the information you want to write codeWidh, codeHeight is the height and width BitMatrix bm = new MultiFormatWriter().encode(info, BarcodeFormat.QR_CODE, codeWidth, codeHeight, hints); bm = deleteWhite(bm);// remove white border ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); // output barcode to file stream MatrixToImageWriter.writeToStream(bm, fileFormat, bos); byte[] buffer = bos.toByteArray(); // Directly output the file stream to the page image tag (requires Base64 encoding) return ("data:image/png;base64," + (Base64.getEncoder()).encodeToString(buffer));
After passing the ajax request, after transcoding the image information through base64, the required binary code is generated, and the image information can be displayed after passing in <img src=""> src
Note: Barcode information rules should be noted;