在 java 服务中使用 Zxing 库从单个图像文件读取多个条码
Read multiple barcodes from single image file using Zxing library in java service
你好,我已经创建了一个 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();
}
您可以将 reader 包装到 GenericMultipleBarcodeReader
中并使用 decodeMultiple
returns 结果数组:
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);
public static void main(String[] args) throws Exception {
String path = "./";
//For Read Single Bar Code Image Info
System.out.println(readSingleBarcodeImageData(path + "generatedBarCodeImage.jpg"));
//For Read Multiple Bar Code Image Info
System.out.println(Arrays.toString(readMultipleBarcodeImageData(path + "multipleBarCodeImageDemo.png")));
}
private static String readSingleBarcodeImageData(String singleImagePath) throws NotFoundException, IOException {
BufferedImage img = ImageIO.read(new File(singleImagePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
MultipleBarcodeReader mbReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
/*List<BarcodeInfo> list = new ArrayList<>();//if have any custom data then convert to dto like this
for (Result result : mbReader.decodeMultiple(bb, hints)) {
list.add(new BarcodeInfo(result.getText(), result.getBarcodeFormat().name()));
}
return list;*/
Result[] currentBarCodeResult = mbReader.decodeMultiple(bb, hints);
return currentBarCodeResult[0].getText();
}
private static Result[] readMultipleBarcodeImageData(String multipleImagePath /* if have multiple barcode in an image then*/) throws NotFoundException, IOException {//
BufferedImage img = ImageIO.read(new File(multipleImagePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
MultipleBarcodeReader mbReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
/*List<BarcodeInfo> list = new ArrayList<>();//if have any custom data then convert to dto like this
for (Result result : mbReader.decodeMultiple(bb, hints)) {
list.add(new BarcodeInfo(result.getText(), result.getBarcodeFormat().name()));
}
return list;*/
Result[] currentBarCodeResult = mbReader.decodeMultiple(bb, hints);//every result represent a bar code
return currentBarCodeResult;
}
**有关更多详细信息,请遵循以下示例:Single and multiple barcode data read example **
你好,我已经创建了一个 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();
}
您可以将 reader 包装到 GenericMultipleBarcodeReader
中并使用 decodeMultiple
returns 结果数组:
MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
Result[] results = multipleReader.decodeMultiple(bitmap);
public static void main(String[] args) throws Exception {
String path = "./";
//For Read Single Bar Code Image Info
System.out.println(readSingleBarcodeImageData(path + "generatedBarCodeImage.jpg"));
//For Read Multiple Bar Code Image Info
System.out.println(Arrays.toString(readMultipleBarcodeImageData(path + "multipleBarCodeImageDemo.png")));
}
private static String readSingleBarcodeImageData(String singleImagePath) throws NotFoundException, IOException {
BufferedImage img = ImageIO.read(new File(singleImagePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
MultipleBarcodeReader mbReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
/*List<BarcodeInfo> list = new ArrayList<>();//if have any custom data then convert to dto like this
for (Result result : mbReader.decodeMultiple(bb, hints)) {
list.add(new BarcodeInfo(result.getText(), result.getBarcodeFormat().name()));
}
return list;*/
Result[] currentBarCodeResult = mbReader.decodeMultiple(bb, hints);
return currentBarCodeResult[0].getText();
}
private static Result[] readMultipleBarcodeImageData(String multipleImagePath /* if have multiple barcode in an image then*/) throws NotFoundException, IOException {//
BufferedImage img = ImageIO.read(new File(multipleImagePath));
BinaryBitmap bb = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
MultipleBarcodeReader mbReader = new GenericMultipleBarcodeReader(new MultiFormatReader());
Hashtable<DecodeHintType, Object> hints = new Hashtable<>();
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
/*List<BarcodeInfo> list = new ArrayList<>();//if have any custom data then convert to dto like this
for (Result result : mbReader.decodeMultiple(bb, hints)) {
list.add(new BarcodeInfo(result.getText(), result.getBarcodeFormat().name()));
}
return list;*/
Result[] currentBarCodeResult = mbReader.decodeMultiple(bb, hints);//every result represent a bar code
return currentBarCodeResult;
}
**有关更多详细信息,请遵循以下示例:Single and multiple barcode data read example **