用zxing扫描java中的二维条码
我尝试用以下代码扫描Java中的二维条形码:用zxing扫描java中的二维条码,java,zxing,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(file
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());
}
}
InputStream in=null;
BuffereImage bfi=null;
File[]files=新文件(“代码”).listFiles();
对于(int i=0;i
我尝试扫描的图像类型与此类似:
我需要扫描pdf417
当我简单地扫描二维条形码的图片时,它就可以工作了。zxing是用来做我想做的事情的吗
我应该说清楚:对于所有.tif我扫描的图像,我得到的信息是“没有缓冲图像用于…”
更新:
我将以下JAR添加到类路径:
jai_imageio_linux-amd64.jar
而且没有按照Robby的建议更改任何代码
它仍然不起作用
第二次更新:
我现在拿到了。ImageIO.getReaderFileSuffix()现在还包括.tiff
新问题:我必须告诉zxing图像上有多个条形码吗?我认为这个问题与条形码或zxing无关。您的问题是
ImageIO.read
返回null
,对吗?你的问题不清楚
阅读javadoc:
Java无法读取图像。默认情况下,无法使用将TIFF文件读入缓冲图像。调用
ImageIO.getReaderFileSuffix()
将返回一个支持的文件格式数组,其中至少应包括JPEG、PNG、BMP、WBMP和GIF
您可以使用支持TIFF格式的。只需将JAI
jar添加到类路径就足以使代码正常工作
您可以在您的操作系统上找到JAI下载。是的,鉴于Robby的评论,很可能就是这样。我不是百分之百肯定,但看起来就是这样。塔克斯!谢谢,我会查出来的!