【问题标题】:Scanning 2D barcodes in java with zxing使用zxing在java中扫描二维条码
【发布时间】:2014-07-15 13:53:51
【问题描述】:

我正在尝试使用以下代码在 Java 中扫描二维条码:

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

        for (int i = 0; i < files.length; i++) {
            try {
                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 source = new BufferedImageLuminanceSource(
                        bfi);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(
                        source));
                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(bitmap, decodeHints);
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (ChecksumException e) {
                    e.printStackTrace();
                } catch (FormatException e) {
                    e.printStackTrace();
                }
                System.out.println("Text: " + result.getText());
            } else {
                System.out.println("No Buffered Image for"
                        + files[i].getName());
            }

        }

我尝试扫描的图像类型类似于以下图像: http://www.apparategemeinschaft.de/07_s01.jpg

我主要需要扫描 pdf417。

当我简单地扫描二维条码的图片时,它就可以工作。 zxing 甚至是用来做我想做的事吗?

我应该说清楚:对于我扫描的所有 .tif 图像,我都会收到消息“没有用于...的缓冲图像”

更新:

我在类路径中添加了以下 jar: jai_imageio_linux-amd64.jar

并且没有按照 Robby 的建议更改任何代码。

还是不行。

第二次更新:

我现在明白了。 ImageIO.getReaderFileSuffixes() 现在也包含 .tiff。

新问题:我必须告诉zxing图片上有多个条形码吗?

【问题讨论】:

    标签: java zxing


    【解决方案1】:

    默认情况下,您无法使用ImageIO 将 TIFF 文件读入缓冲图像。调用ImageIO.getReaderFileSuffixes() 将返回一个受支持文件格式的数组,其中至少应包括JPEG、PNG、BMP、WBMP 和GIF。

    您可以使用支持 TIFF 格式的Java Advanced Imaging (JAI) API。只需将 JAI jar 添加到您的类路径就足以使您的代码正常工作。

    您可以找到适用于您的操作系统的 JAI 下载 here

    【讨论】:

    • 谢谢,我去看看!
    【解决方案2】:

    我认为这个问题与条形码或 zxing 无关。你的问题是ImageIO.read 返回null,对吗?从你的问题看不清楚。

    阅读 javadoc:http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html#read(java.io.File)

    Java 无法读取该图像。

    【讨论】:

    • 是的,很可能就是这样,鉴于 Robby 的评论。我不是100%肯定,但看起来就是这样。谢谢!
    猜你喜欢
    • 2011-11-06
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    相关资源
    最近更新 更多