java如何使用zxing识别图片条码

 时间:2024-10-14 04:57:46

ZXing 是一个开源 Java 类库用于解析多种格式的 1D/2D 条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。

工具/原料

电脑

intellij IDEA

第一步:代码实现。

1、第一步骤:创建springboot项目。1、使用IDEA创建springboot项目2、使用eclipse创建springboot项目3、添加依赖zxing依赖: <!--java zxing二维码(可带logo)、条形码生成--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <!--解析需要依赖的架包--> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency>

java如何使用zxing识别图片条码

2、第二步骤:编辑生成二维码和条形码的java类1、具体代码如下所示:import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.io.OutputStream;import javax.imageio.ImageIO;import com.google.zxing.common.BitMatrix;/*** 二维码的生成需要借助MatrixToImageWriter类,该类是由Google提供的,可以将该类直接拷贝到源码中使用,当然你也可以自己写个* 生产条形码的基类*/public class WriteBitMatricToFile { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static BufferedImage toBufferedImage(BitMatrix bm) { int width = bm.getWidth(); int height = bm.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { image.setRGB(i, j, bm.get(i, j) ? BLACK : WHITE); } } return image; } public static void writeBitMatricToFile(BitMatrix bm, String format, File file) throws IOException { // 生成二维码或者条形码 BufferedImage image = toBufferedImage(bm); // 设置logo图标 // 在图片上添加log,如果不需要则不用执行此步骤 LogoConfig logoConfig = new LogoConfig(); image = logoConfig.LogoMatrix(image); try { if (!ImageIO.write(image, format, file)) { throw new RuntimeException("Can not write an image to file" + file); } } catch (IOException e) { e.printStackTrace(); } } /** * 生成不带log的二维码或者条形码 * * @param matrix * @param format * @param stream * @throws IOException */ public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, stream)) { throw new IOException("Could not write an image of format " + format); } }}

java如何使用zxing识别图片条码
java如何使用zxing识别图片条码

3、第三步骤:给图片(二维码和条形码)添加log的java类。1、具体代码如下所示:import java.awt.BasicStroke;i罪焐芡拂mport java.awt.Color;import java.awt.Graphics2D;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;//二维码 添加 logo图标 处理的方法, 模仿微信自动生成二维码效果,有圆角边框,logo和二维码间有空白区,logo带有灰色边框public class LogoConfig { /** * 设置 logo * * @param matrixImage * 源二维码图片 * @return 返回带有logo的二维码图片 * @throws IOException * @author Administrator sangwenhao */ public BufferedImage LogoMatrix(BufferedImage matrixImage) throws IOException { /** * 读取二维码图片,并构建绘图对象 */ Graphics2D g2 = matrixImage.createGraphics(); int matrixWidth = matrixImage.getWidth(); int matrixHeigh = matrixImage.getHeight(); /** * 读取Logo图片 */ BufferedImage logo = ImageIO.read(new File("E:/file/log.jpg")); // 开始绘制图片 g2.drawImage(logo, matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, null);// 绘制 BasicStroke stroke = new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke);// 设置笔画对象 // 指定弧度的圆角矩形 RoundRectangle2D.Float round = new RoundRectangle2D.Float( matrixWidth / 5 * 2, matrixHeigh / 5 * 2, matrixWidth / 5, matrixHeigh / 5, 20, 20); g2.setColor(Color.white); g2.draw(round);// 绘制圆弧矩形 // 设置logo 有一道灰色边框 BasicStroke stroke2 = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); g2.setStroke(stroke2);// 设置笔画对象 RoundRectangle2D.Float round2 = new RoundRectangle2D.Float( matrixWidth / 5 * 2 + 2, matrixHeigh / 5 * 2 + 2, matrixWidth / 5 - 4, matrixHeigh / 5 - 4, 20, 20); g2.setColor(new Color(128, 128, 128)); g2.draw(round2);// 绘制圆弧矩形 g2.dispose(); matrixImage.flush(); return matrixImage; }}

java如何使用zxing识别图片条码
java如何使用zxing识别图片条码

4、第四步骤:生成二维码和条形码测试代码。1、具体代码如下所示:import java.io.掂迎豢畦File;import java.io.FileOutputStream;import java.io.IOException;import java.util.HashMap;import com.google.zxing.BarcodeFormat;import com.google.zxing.EncodeHintType;import com.google.zxing.MultiFormatWriter;import com.google.zxing.WriterException;import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class EncodeTest {public static void main(String[] args) throws Exception {EncodeTest test = new EncodeTest();// 生成带log的二维码test.qr_code();// 生成不带log的条形码test.ean_code();}/*** 二维码** @throws IOException*/public void qr_code() throws WriterException, IOException { int width = 300; int height = 300; String text = "https://jingyan.baidu.com/user/npublic?uid=be683ee17853d6d6a312287b"; String format = "png"; HashMap<EncodeHintType, Object> hints = new HashMap<>(); // 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%) hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 内容所使用字符集编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 二维码的格式是BarcodeFormat.QR_CODE BitMatrix bm = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); // 生成二维码图片 // File out = new File("qr_code.png"); //默认项目根目录里 File out = new File("E:" + File.separator + "file/qr_code.png"); // 指定输出路径 File.separator解决跨平台 WriteBitMatricToFile.writeBitMatricToFile(bm, format, out); }/*** 条形码** @throws IOException*/public void ean_code() throws WriterException, IOException { int width = 200; int height = 100; String text = "6923450657713"; String format = "png"; HashMap<EncodeHintType, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 条形码的格式是 BarcodeFormat.EAN_13 BitMatrix bm = new MultiFormatWriter().encode(text, BarcodeFormat.EAN_13, width, height, hints); // 生成条形码图片 File out = new File("E:" + File.separator + "file/ean3.png");// 指定输出路径 // File.separator解决跨平台 //不需要条形码的log WriteBitMatricToFile.writeToStream(bm, format, new FileOutputStream(out)); //需要条形码的log //WriteBitMatricToFile.writeBitMatricToFile(bm, format, out); }}

java如何使用zxing识别图片条码
java如何使用zxing识别图片条码
java如何使用zxing识别图片条码

5、第五步骤:读取二维码和条形码测试类。1、具体代码如下所示:import com.google.zxing.*稆糨孝汶;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;import com.google.zxing.common.GlobalHistogramBinarizer;import com.google.zxing.common.HybridBinarizer;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.util.HashMap;import java.util.Map;public class DecodeTest { public static void main(String[] args) throws Exception { // 这是二维码图片 BufferedImage bi = ImageIO.read(new FileInputStream(new File("E:" + File.separator + "file/qr_code.png"))); if (bi != null) { Map<DecodeHintType, String> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); LuminanceSource source = new BufferedImageLuminanceSource(bi); // 这里还可以是 //BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); Result res = new MultiFormatReader().decode(bitmap, hints); System.out.println("图片中内容: "+ res.getText()); System.out.println("图片中格式: " + res.getBarcodeFormat()); } // 这是条形码图片 BufferedImage rs = ImageIO.read(new FileInputStream(new File("E:" + File.separator + "file/ean3.png"))); if (bi != null) { Map<DecodeHintType, String> hints = new HashMap<>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); LuminanceSource source = new BufferedImageLuminanceSource(rs); // 这里还可以是 //BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source)); Result res = new MultiFormatReader().decode(bitmap, hints); System.out.println("图片中内容: "+ res.getText()); System.out.println("图片中格式: " + res.getBarcodeFormat()); } }}

第二步:功能测试。

1、第一步骤:生成二维码和条形码。1、生成含有log的二维码和条形码2、生成没有log的条形码

java如何使用zxing识别图片条码
java如何使用zxing识别图片条码

2、第二步骤:读取二维码和条形码。1、执行DecodeTest主函数main方法2、输出读取内容

java如何使用zxing识别图片条码
  • 如何用js读取和写入cookie
  • 联想小新700笔记本如何设置u盘启动
  • 华硕主板BIOS怎么设置从U盘启动
  • 易语言-取数组下标-实例讲解
  • 19寸显示器无法设置1440*900分辨率的解决方法
  • 热门搜索
    绿色低碳环保手抄报 手抄报模板手画 春节手抄报a3纸 小学消防安全手抄报 健康知识手抄报内容 中秋手抄报图片大全 五年级上册英语手抄报 好看的手抄报花边 有关卫生的手抄报 关爱环卫工人手抄报