简体   繁体   English

在 java 服务中使用 Zxing 库从单个图像文件中读取多个条形码

[英]Read multiple barcodes from single image file using Zxing library in java service

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.嗨,我已经创建了一个 java 服务,用于从图像中读取条形码,我使用 Zxing 库来解码此处的文本,挑战是如果一个带有单个条形码的文件它工作正常,如果有多个条形码它会产生不相关的结果,我在下面给出了我的代码.

pom.xml 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?我应该如何使用 Zxing 库读取和检索给定图像中可用的所有条形码? 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();

    }

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.嗨,我已经创建了一个 java 服务,用于从这里读取图像中的条形码 我使用 Zxing 库在这里解码文本 挑战是,如果一个带有单个条形码的文件它工作正常,如果有多个条形码它会产生不相关的结果我在下面给出了我的代码.

pom.xml 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?我应该如何使用 Zxing 库读取和检索给定图像中的所有可用条形码? 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();

    }
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;
    }

**For more Details Follow This Example: Single and multiple barcode data read example ** **有关更多详细信息,请按照此示例: 单个和多个条码数据读取示例**

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM