简体   繁体   English

使用Zxing扫描Java中的二维条形码

[英]Scanning 2D barcodes in java with zxing

I'm trying to scan 2D barcodes in Java with the following code: 我正在尝试使用以下代码在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());
            }

        }

The types of images I'm trying to scan resemble this one: http://www.apparategemeinschaft.de/07_s01.jpg 我尝试扫描的图像类型与此相似: http : //www.apparategemeinschaft.de/07_s01.jpg

I would need to scan pdf417 predominantly. 我将主要需要扫描pdf417。

It works when I simply scan a picture of a 2d-barcode. 当我简单地扫描2D条形码的图片时,它就可以工作。 Is zxing even meant to be used for what Im trying to do? zxing甚至打算用于Im试图做的事情吗?

I should make clear: for all .tif-images I scan, I get the message "no buffered image for ..." 我应该明确指出:对于我扫描的所有.tif图像,我都会收到消息“ ...没有缓冲图像”。

UPDATE: 更新:

I added the following jars to the classpath: jai_imageio_linux-amd64.jar 我将以下jar添加到类路径:jai_imageio_linux-amd64.jar

And didnt change any code, as suggested by Robby. 并没有更改任何代码,如罗比建议的那样。

It still doesnt work. 它仍然不起作用。

SECOND UPDATE: 第二次更新:

I've got it now. 我知道了 The ImageIO.getReaderFileSuffixes() now includes .tiff as well. ImageIO.getReaderFileSuffixes()现在也包含.tiff。

New question: Do I have to tell zxing that there are more than one barcodes on the image? 新问题:我是否必须告诉zxing图像上有多个条形码?

You cannot, by default, read a TIFF file into a buffered image using ImageIO . 默认情况下,您无法使用ImageIO将TIFF文件读取到缓冲的图像中。 Calling ImageIO.getReaderFileSuffixes() will return an array of the supported file formats which should at least include JPEG, PNG, BMP, WBMP and GIF. 调用ImageIO.getReaderFileSuffixes()将返回受支持的文件格式的数组,该格式至少应包括JPEG,PNG,BMP,WBMP和GIF。

You could use the Java Advanced Imaging (JAI) API which supports the TIFF format. 您可以使用支持TIFF格式的Java Advanced Imaging(JAI)API Just adding the JAI jars to your classpath should be sufficient to make your code work. 只需将JAI jar添加到您的类路径中就足以使您的代码正常工作。

You can find JAI downloads for your OS here . 您可以在此处找到适用于您的操作系统的JAI下载。

I don't think this question has anything to do with barcodes or zxing. 我认为这个问题与条形码或zxing无关。 Your problem is that ImageIO.read returns null , right? 您的问题是ImageIO.read返回null ,对吗? It's not clear from your question. 您的问题不清楚。

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

The image is not readable to Java. 该图像对Java不可读。

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

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