使用zxing扫描java中的二维条形码

时间:2014-07-15 13:53:51

标签: java zxing

我尝试使用以下代码扫描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。

当我简单地扫描2d条形码的图片时,它可以工作。 zxing是否意味着用于我想做的事情?

我应该说清楚:对于我扫描的所有.tif图像,我收到消息&#34;没有缓冲图像...&#34;

更新:

我将以下jar添加到类路径中: jai_imageio_linux-amd64.jar

并没有改变任何代码,正如罗比所建议的那样。

它仍然无效。

第二次更新:

我现在得到了它。 ImageIO.getReaderFileSuffixes()现在也包含.tiff。

新问题:我是否必须告诉zxing图像上有多个条形码?

2 个答案:

答案 0 :(得分:1)

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

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

您可以找到适用于您的操作系统的JAI下载{。{3}}。

答案 1 :(得分:0)

我不认为这个问题与条形码或zxing有任何关系。您的问题是ImageIO.read会返回null,对吗?你的问题并不清楚。

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

图像对Java不可读。

相关问题