参考地址: 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 { /** * 生成二维码 * * @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<>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "GBK"); try { //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(); } } /** * 解析二维码 * * @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; } /** * 生成条形码 * * @param contents * @param width * @param height * @param imgPath */ // int width = 105, height = 50; 长度很容易报错: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 { //条形码貌似只能写入条形码固定规则的信息 BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null); MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(imgPath)); } catch (Exception e) { e.printStackTrace(); } } /** * 解析条形码 * * @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; } }测试:特别注意 高宽 容易报错:NotFoundException
public static void main(String[] args) throws Exception { String imgPath = "D:\\test.png"; // 益达无糖口香糖的条形码 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); }
以上是参考信息
这是我的工具类重要信息
// 条码编码规则设定(字符集) Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "GBK"); // 条码编码 String info = strCode.toString(); //BarcodeFormat.PDF_417这个是加密的条形码信息 //info 是想要写入的信息 codeWidh,codeHeight是高度宽度 BitMatrix bm = new MultiFormatWriter().encode(info, BarcodeFormat.QR_CODE, codeWidth, codeHeight, hints); bm = deleteWhite(bm);// 去掉白边 ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); // 输出条码到文件流 MatrixToImageWriter.writeToStream(bm, fileFormat, bos); byte[] buffer = bos.toByteArray(); // 直接输出文件流到页面图片标签(需要Base64编码) return ("data:image/png;base64," + (Base64.getEncoder()).encodeToString(buffer));
通过ajax 请求之后, 将图片信息通过base64转码之后, 生成所需要的二进制码,传入<img src=""> src后面,即可显示图片信息
注:条形码信息规则应需要注意;