Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java中的zxing扫描多个条形码_Java_Zxing - Fatal编程技术网

用java中的zxing扫描多个条形码

用java中的zxing扫描多个条形码,java,zxing,Java,Zxing,我需要读取TIFF的pdf417条形码。图像上通常有多个条形码- 这是我的代码: InputStream in = null; BufferedImage bfi = null; File[] files = new File(DIR).listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isFile()) {

我需要读取TIFF的pdf417条形码。图像上通常有多个条形码-

这是我的代码:

InputStream in = null;
        BufferedImage bfi = null;
        File[] files = new File(DIR).listFiles();

        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                try {
                    System.out.println(files[i].getName());
                    in = new FileInputStream(files[i]);
                    bfi = ImageIO.read(in);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

                if (bfi != null) {
                    LuminanceSource ls = new BufferedImageLuminanceSource(bfi);
                    BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls));
                    Reader reader = new MultiFormatReader();
                    Result result = null;

                    Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
                    decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

                    try {
                        result = reader.decode(bmp, decodeHints);
                    } catch (NotFoundException e) {
                        e.printStackTrace();
                    } catch (ChecksumException e) {
                        e.printStackTrace();
                    } catch (FormatException e) {
                        e.printStackTrace();
                    }
                    System.out.println(result.getBarcodeFormat());
                    System.out.println("Text " + result.getText());
                    System.out
                            .println("-------------------------------------------------------");

                } else {
                    System.out.println("No Buffered Image for "
                            + files[i].getName());
                }
            }

        }
第三次编辑:

current version of code:

public static void main(final String[] args) {

        BufferedImage bfi = null;
        File[] files = new File(DIR).listFiles();
        int counttiffs = 0;
        String filename = null;
        TreeMap<String, Exception> errormap = new TreeMap<String, Exception>();
        int onebarcode = 0;
        int twobarcodes = 0;
        int threebarcodes = 0;

        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                try (BufferedInputStream bfin = new BufferedInputStream(
                        new FileInputStream(files[i]))) {
                    filename = files[i].getName();

                    bfi = ImageIO.read(bfin);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (bfi != null) {
                    LuminanceSource ls = new BufferedImageLuminanceSource(bfi);
                    BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls));

                    Reader reader = new MultiFormatReader();
                    GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(
                            reader);
                    Result[] result = null;

                    Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
                    decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

                    try {
                        result = greader.decodeMultiple(bmp, decodeHints);
                    } catch (NotFoundException e) {
                        errormap.put(filename, e);
                    } catch (NullPointerException e) {
                        errormap.put(filename, e);
                    }
                    if (result != null) {
                        switch (result.length) {
                            case 1:
                                onebarcode++;
                                break;
                            case 3:
                                threebarcodes++;
                                break;
                            default:
                                twobarcodes++;

                        }
                        try (BufferedWriter bfwr = new BufferedWriter(
                                new FileWriter(FILE, true))) {
                            bfwr.write(filename + ": number of barcodes found = "
                                    + result.length);
                            bfwr.newLine();
                            for (int j = 0; j < result.length; j++) {
                                bfwr.write(result[j].getText());
                            }
                            bfwr.newLine();
                            bfwr.write("---------------------------------------------------------");
                            bfwr.newLine();
                            counttiffs++;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }
                else {
                    System.out.println("No Buffered Image for "
                            + files[i].getName());
                }
            }
        }
和第一个一样。在您第一次提交后,我在另一行上得到了一个NPE,但现在要么我使用了错误的依赖项,要么它再次发生

另一件事是:我扫描了大约2.500个tiff文件,每个文件上都有两个pdf417条形码,有些有点倾斜,有些质量不太好(意味着有些像素是白色而不是黑色)。我总共得到1644个错误,或者是由NotFoundException或NullPointer异常引起的。在成功扫描的948个文件中,218个result.length是1(它只找到了一个条形码),68个result.length是3(但它应该扫描2个条形码)


根据您的经验,当条形码不笔直且边缘有小错误时,zxing有多明智?pixles没有完美打印?

这看起来就像一个bug,我在中修复了它。您可以通过抓取3.1.1-SNAPSHOT来尝试修复吗?

NPE听起来像个bug。你能澄清一下你正在使用的代码的哪个版本吗?也就是说,你编译的代码中的第163行是什么?我无法访问它。该方法被自动调用为GenericMultipleBarcodeReader。您至少不知道代码的哪个版本?您是指zxing的哪个版本?应该是最新的。我三周前就下载了它。嗯,不是100%确定,但在pom上写着:com.google.zxing-zxing-parent 3.1.1-SNAPSHOT-pom。我不是已经有3.1.1-Snapshot了吗?或者您正在谈论另一个包?是的,这意味着您正在引用最新的快照。当然,在我进行修复之前,您不会得到我的修复,但是现在我已经提交了修复并发布了一个新快照,您应该得到更新。你可能需要强迫Maven用-U选项检查它啊,对不起,我没有看到你现在就写了修复程序!我会试试看,然后再打给你。也许明天吧!太多了!完全一样?我认为不可能。这条线上再也没有解引用了。有点不同吗?或者,我认为你没有更新。你可以通过删除~/.m2/repository/com/google/zxing/来确保你没有本地缓存的工件。对,这是同一问题的另一个实例。我想就这些了。我刚刚又推了一张快照。
for (int i = 0; i < files.length; i++) {
            try (BufferedInputStream bfin = new BufferedInputStream(
                    new FileInputStream(files[i]))) {
                dateiname = files[i].getName();

                bfi = ImageIO.read(bfin);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (bfi != null) {
                LuminanceSource ls = new BufferedImageLuminanceSource(bfi);
                BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls));

                Reader reader = new MultiFormatReader();
                GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(
                        new ByQuadrantReader(reader));
                Result[] result = null;

                Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
                decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

                try {
                    result = greader.decodeMultiple(bmp, decodeHints);
                } catch (NotFoundException e) {
                    e.printStackTrace();
                    System.out.println("No result");
                    System.out.println("+++++++++++++++++++++++++++++++");
                }
                if (result != null) {
                    for (int j = 0; j < result.length; j++) {
                        System.out.println(result[j].getText());
                        System.out.println("+++++++++++++++++++++++++++++++");
                    } 
                }

            } else {
                System.out.println("No Buffered Image for "
                        + files[i].getName());
            }

        }
Exception in thread "main" java.lang.NullPointerException
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:109)
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:148)
    at com.google.zxing.multi.GenericMultipleBarcodeReader.decodeMultiple(GenericMultipleBarcodeReader.java:65)
    at barcode.ZXingTestMulti.main(ZXingTestMulti.java:86)
current version of code:

public static void main(final String[] args) {

        BufferedImage bfi = null;
        File[] files = new File(DIR).listFiles();
        int counttiffs = 0;
        String filename = null;
        TreeMap<String, Exception> errormap = new TreeMap<String, Exception>();
        int onebarcode = 0;
        int twobarcodes = 0;
        int threebarcodes = 0;

        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                try (BufferedInputStream bfin = new BufferedInputStream(
                        new FileInputStream(files[i]))) {
                    filename = files[i].getName();

                    bfi = ImageIO.read(bfin);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (bfi != null) {
                    LuminanceSource ls = new BufferedImageLuminanceSource(bfi);
                    BinaryBitmap bmp = new BinaryBitmap(new HybridBinarizer(ls));

                    Reader reader = new MultiFormatReader();
                    GenericMultipleBarcodeReader greader = new GenericMultipleBarcodeReader(
                            reader);
                    Result[] result = null;

                    Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
                    decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);

                    try {
                        result = greader.decodeMultiple(bmp, decodeHints);
                    } catch (NotFoundException e) {
                        errormap.put(filename, e);
                    } catch (NullPointerException e) {
                        errormap.put(filename, e);
                    }
                    if (result != null) {
                        switch (result.length) {
                            case 1:
                                onebarcode++;
                                break;
                            case 3:
                                threebarcodes++;
                                break;
                            default:
                                twobarcodes++;

                        }
                        try (BufferedWriter bfwr = new BufferedWriter(
                                new FileWriter(FILE, true))) {
                            bfwr.write(filename + ": number of barcodes found = "
                                    + result.length);
                            bfwr.newLine();
                            for (int j = 0; j < result.length; j++) {
                                bfwr.write(result[j].getText());
                            }
                            bfwr.newLine();
                            bfwr.write("---------------------------------------------------------");
                            bfwr.newLine();
                            counttiffs++;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }

                }
                else {
                    System.out.println("No Buffered Image for "
                            + files[i].getName());
                }
            }
        }
java.lang.NullPointerException
    at com.google.zxing.multi.GenericMultipleBarcodeReader.translateResultPoints(GenericMultipleBarcodeReader.java:163)
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:96)
    at com.google.zxing.multi.GenericMultipleBarcodeReader.doDecodeMultiple(GenericMultipleBarcodeReader.java:148)
    at com.google.zxing.multi.GenericMultipleBarcodeReader.decodeMultiple(GenericMultipleBarcodeReader.java:65)
    at zBarcodes_Test.ZXingTestMulti.main(ZXingTestMulti.java:72)