使用java服务中的Zxing库从单个图像文件读取多个条形码
您好,我已经创建了一个java服务,用于读取图像中的条形码。我在这里使用Zxing库解码文本。挑战在于,如果一个文件具有单个条形码,那么它工作正常。如果有多个条形码,那么它会产生不相关的结果。我在下面给出了我的代码 pom.xml使用java服务中的Zxing库从单个图像文件读取多个条形码,java,spring-boot,zxing,zxing.net,Java,Spring Boot,Zxing,Zxing.net,您好,我已经创建了一个java服务,用于读取图像中的条形码。我在这里使用Zxing库解码文本。挑战在于,如果一个文件具有单个条形码,那么它工作正常。如果有多个条形码,那么它会产生不相关的结果。我在下面给出了我的代码 pom.xml <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.goog
<!-- 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>
结果是这样的
CODE93
带有多个条形码的图像
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/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();
}
@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();
}
GetMapping(value=“OCR/GetBarcodeRead”)
@ApiOperation(value=“从条形码Zxing库获取结果”)
公共字符串GetBarcodeRead()引发异常{
InputStream barCodeInputStream=新文件InputStream(“images/K71NM.jpg”);
BuffereImage BarCodeBuffereImage=ImageIO.read(barCodeInputStream);
LuminanceSource source=新的BufferedImageLuminanceSource(条形码BufferedImage);
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(源));
com.google.zxing.Reader Reader=新的MultiFormatReader();
MultipleBarcodeReader bcReader=新的通用MultipleBarcodeReader(读取器);
Hashtable hints=新的Hashtable();
put(DecodeHintType.TRY_,Boolean.TRUE);
StringBuilder sb=新的StringBuilder();
for(结果:bcReader.decodeMultiple(位图、提示)){
sb.append(result.getText()).append(“\n”);
}
使某人返回字符串();
}
您可以将阅读器包装到一个
通用MultipleBarCodeReader
中,并使用返回结果数组的decodeMultiple
:
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);
我已尝试不工作,我已给出了代码,请检查您是否可以澄清您得到的结果?这是因为您直接在阵列上调用toString。您必须对其进行迭代并分别获取值。我建议您在调试模式下运行它,并使用断点来更熟悉那里的结构。它现在可以工作了,我已经给出了上面的工作代码,感谢lotHi@Alex-创建了另一个与库apache camel相关的主题,附加链接供您参考