问题
Hi i have created a java service for reading the barcode from image here iam using Zxing library for decoding the text here the challenge is if a file with single barcode it's working fine if there are multiple barcodes it's producing irrelevant result i have given my code below.
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 service
@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();
}
Result is something like this
CODE93
Image with multiple barcodes
How should i read and retrieve all the barcodes available in the given image using Zxing library? Could some one help me to achieve this? thanks in advance
workaround
@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();
}
Working code
@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();
}
回答1:
You can wrap your reader into a GenericMultipleBarcodeReader
and use decodeMultiple
that returns an array of results:
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);
Reference
来源:https://stackoverflow.com/questions/60377223/read-multiple-barcodes-from-single-image-file-using-zxing-library-in-java-servic