共享一个比较不错的小工具类,条形码,二维码的生成和解析
测试的时候记得在pom.xml中加入maven依赖
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>2.2</version> </dependency>
以下为工具的代码:若其他小伙伴有更好的实现方式,记得留言小编改进哦,谢谢
package com.gzu.pyu.tools.utils; 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.IOException; import java.util.Hashtable; /** * 条形码和二维码编码解码 * * @author pyu * @version 2018-02-28 */ public class QrCodeUtils { /** * 条形码编码 * * @param contents 内容,必须为13位阿拉伯数字 * @param width 宽度 * @param height 高度 * @param imgPathName 生成条形码的路径 * @throws WriterException * @throws IOException */ public static void barEncode(String contents, int width, int height, String imgPathName) throws WriterException, IOException { if (contents!=null&&contents.length()!=13){ throw new WriterException("the encode contents must be 13 digits"); } int codeWidth = 3 + // start guard (7 * 6) + // left bars 5 + // middle guard (7 * 6) + // right bars 3; // end guard codeWidth = Math.max(codeWidth, width); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.EAN_13, codeWidth, height, null); MatrixToImageWriter .writeToFile(bitMatrix, "png", new File(imgPathName)); } /** * 条形码解码 * * @param imgPathName * @return String * @exception IOException * @exception NotFoundException */ public static String barDecode(String imgPathName) throws IOException, NotFoundException { BufferedImage image = ImageIO.read(new File(imgPathName)); if (image == null) { throw new IOException("the decode image may be not exit.imgPathName:"+imgPathName); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result = new MultiFormatReader().decode(bitmap, null); return result.getText(); } /** * 二维码编码 * * @param contents 编码内容 * @param width 二维码宽度 * @param height 二维码高度 * @param imgPathName 生成图片的路径 */ public static void qrEncode(String contents, int width, int height, String imgPathName) throws WriterException, IOException { Hashtable<EncodeHintType, Object> hints = new Hashtable<>(); // 指定纠错等级 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 指定编码格式 hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPathName)); } /** * 二维码解码 * * @param imgPathName * @return String */ public static String qrDecode(String imgPathName) throws NotFoundException, IOException { BufferedImage image = ImageIO.read(new File(imgPathName)); if (image == null) { throw new IOException("the decode image may be not exit.imgPathName:"+imgPathName); } LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Hashtable<DecodeHintType, Object> hints = new Hashtable<>(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = new MultiFormatReader().decode(bitmap, hints); return result.getText(); } /** * @param args */ public static void main(String[] args) throws IOException, WriterException, NotFoundException { // 条形码 String imgPath = "target/pyu_phone_number.png"; String contents = "86187986139481"; int width = 105, height = 50; QrCodeUtils.barEncode(contents, width, height, imgPath); System.out.println("finished pyu_phone_number.png encode.\n"); String decodeContent = QrCodeUtils.barDecode(imgPath); System.out.println("barDecode解码内容如下:\n" + decodeContent); System.out.println("finished zxing EAN-13 decode.\n"); // 二维码 String imgPath2 = "target/pyu_contact.png"; StringBuffer buffer=new StringBuffer() .append("Hello pyu, welcome to shenzhen!") .append("\n") .append("Blog [ https://www.cnblogs.com/gzu-link-pyu ]") .append("\n") .append("Email [ 1203114557@qq.com ]") .append("\n"); int width2 = 300, height2 = 300; QrCodeUtils.qrEncode(buffer.toString(), width2, height2, imgPath2); System.out.println("finished pyu_contact.png encode."); String decodeContent2 = QrCodeUtils.qrDecode(imgPath2); System.out.println("qrDecode解码内容如下:\n" + decodeContent2); System.out.println("finished zxing decode."); } }
运行结果如下: