嗨,我已经创建了一个Java服务,用于在此处使用Zxing库对图像进行解码,以便从图像中读取条形码。在这里,我面临的挑战是,如果单个条形码的文件工作正常,并且有多个条形码,则产生不相关的结果,我给出了我的代码如下。
pom.xml
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
java服务
@GetMapping(value = "OCR/GetBarcodeRead")
@ApiOperation(value = "Get result from Barcode Zxing library")
public String GetBarcodeRead() throws Exception {
InputStream barCodeInputStream = new FileInputStream("images/multiple.jpg");
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
return result.getText();
}
结果是这样的
CODE93
具有多个条形码的图像
我应该如何使用Zxing库读取和检索给定图像中的所有条形码? 有人可以帮助我实现这一目标吗?预先感谢
解决方法
@GetMapping(value = "OCR/GetBarcodeRead")
@ApiOperation(value = "Get result from Barcode Zxing library")
public String GetBarcodeRead() throws Exception {
InputStream barCodeInputStream = new FileInputStream("images/multiple.png");
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader();
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);
//Result result = reader.decode(bitmap);
return results.toString();
}
工作代码
@GetMapping(value = "OCR/GetBarcodeRead")
@ApiOperation(value = "Get result from Barcode Zxing library")
public String GetBarcodeRead() throws Exception {
InputStream barCodeInputStream = new FileInputStream("images/K71NM.jpg");
BufferedImage barCodeBufferedImage = ImageIO.read(barCodeInputStream);
LuminanceSource source = new BufferedImageLuminanceSource(barCodeBufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
com.google.zxing.Reader reader = new MultiFormatReader();
MultipleBarcodeReader bcReader = new GenericMultipleBarcodeReader(reader);
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
StringBuilder sb = new StringBuilder();
for (Result result : bcReader.decodeMultiple(bitmap, hints)) {
sb.append(result.getText()).append(" \n");
}
return sb.toString();
}
答案 0 :(得分:1)
您可以将阅读器包装到GenericMultipleBarcodeReader
中,并使用decodeMultiple
返回结果数组:
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);