【问题标题】:Read multiple barcodes from single image file using Zxing library in java service在 java 服务中使用 Zxing 库从单个图像文件中读取多个条形码
【发布时间】:2020-02-24 13:43:32
【问题描述】:

嗨,我已经创建了一个 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();

    }

【问题讨论】:

    标签: java spring-boot zxing zxing.net


    【解决方案1】:

    您可以将阅读器包装到 GenericMultipleBarcodeReader 中,并使用返回结果数组的 decodeMultiple

    MultipleBarcodeReader multipleReader = new GenericMultipleBarcodeReader(reader);
    Result[] results = multipleReader.decodeMultiple(bitmap);
    

    Reference

    【讨论】:

    • 我试过不工作我已经给出了代码,请检查
    • 你能澄清一下你得到了什么结果吗?
    • 那是因为你直接在数组上调用 toString 。您必须遍历它并单独获取值。我建议你在调试模式下运行它并使用断点来更熟悉那里的结构。
    • 它现在可以工作了,我已经给出了上面的工作代码,非常感谢
    • 嗨@Alex - 创建了另一个与库 apache camel 相关的主题附加链接供您参考stackoverflow.com/questions/60389632/…
    【解决方案2】:
    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 **

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      • 2013-11-21
      • 2023-04-08
      相关资源
      最近更新 更多