java如何使用zxing识别图片条码
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>




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的条形码

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