当执行核心java程序时,com.google.zxing.NotFoundException异常会出现? [英] com.google.zxing.NotFoundException exception comes when core java program executed?

查看:2468
本文介绍了当执行核心java程序时,com.google.zxing.NotFoundException异常会出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有2D条形码的jpeg文件。图像分辨率为1593X1212。我正在使用xing库从图像中解码这个条形码。我在网上收到以下代码。

  import java.awt.image.BufferedImage; 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;


public class NewLibTest {
public static void main(String args []){
System.out.println(decode(new File(E: \\\xyz.jpg)));
}

/ **
*解码方法用于读取图像或条形码本身,并识别条形码,
*获取编码内容并返回。
* @param< DecodeHintType>
* @param需要读取的文件映像。
* @param配置配置读取条码时使用。
* @返回条形码的解码结果。
* /
public static String decode(File file){//,Map< DecodeHintType,Object>提示异常{
//检查所需参数
if(file == null || file.getName()。trim()。isEmpty())
throw new IllegalArgumentException(找不到文件,或无效的文件名。);
BufferedImage image = null;
尝试{
image = ImageIO.read(file);
} catch(IOException ioe){
try {
throw new Exception(ioe.getMessage());
} catch(Exception e){
// TODO自动生成的catch块
e.printStackTrace();
}
}
if(image == null)
throw new IllegalArgumentException(Could not decode image);
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader barcodeReader = new MultiFormatReader();
结果结果;
String finalResult = null;
try {
// if(hints!= null&!hints.isEmpty())
// result = barcodeReader.decode(bitmap,hints);
// else
result = barcodeReader.decode(bitmap);
//设置结果。
finalResult = String.valueOf(result.getText());
} catch(Exception e){
e.printStackTrace();
// throw new BarcodeEngine()。new BarcodeEngineException(e.getMessage());
}
return finalResult;
}

}



当我执行这个简单的核心java程序,我给了例外

  com.google.zxing.NotFoundException 

它甚至不给任何stackstrace。



我想问专家们为什么会有这样的例外。
谢谢你!

解决方案

我也有同样的问题。我使用了一个我知道有效的QR码的图像,并且我也收到了com.google.zxing.NotFoundException。



问题是您使用的图像来源是为了大图书馆来解码。在我减小了我的图像的大小后,QR码解码器工作。



为了我的应用程序的目的,图像上的QR码总是或多或少在相同的区域,所以我使用BufferedImage类的getSubimage函数隔离QR码。

  BufferedImage image; 
image = ImageIO.read(imageFile);
BufferedImage cropedImage = image.getSubimage(0,0,914,400);
//使用cropedImage而不是图像
LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
//条码解码
QRCodeReader reader = new QRCodeReader();
结果result = null;
try
{
result = reader.decode(bitmap);
}
catch(ReaderException e)
{
returnreader error;
}


I have a jpeg file which has 2D bar code. Image resolution is 1593X1212. I am using xing library to decode this barcode from image. I got following code on net.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.ChecksumException;
import com.google.zxing.FormatException;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
    import com.google.zxing.Reader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;


public class NewLibTest {
    public static void main(String args[]){
    System.out.println(decode(new File("E:\\xyz.jpg")));
    }

    /**
      * Decode method used to read image or barcode itself, and recognize the barcode,
      * get the encoded contents and returns it.
     * @param <DecodeHintType>
      * @param file image that need to be read.
      * @param config configuration used when reading the barcode.
      * @return decoded results from barcode.
      */
     public static String decode(File file){//, Map<DecodeHintType, Object> hints) throws Exception {
         // check the required parameters
         if (file == null || file.getName().trim().isEmpty())
             throw new IllegalArgumentException("File not found, or invalid file name.");
         BufferedImage image = null;
         try {
             image = ImageIO.read(file);
         } catch (IOException ioe) {
             try {
                throw new Exception(ioe.getMessage());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
         }
         if (image == null)
             throw new IllegalArgumentException("Could not decode image.");
         LuminanceSource source = new BufferedImageLuminanceSource(image);
         BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
         MultiFormatReader barcodeReader = new MultiFormatReader();
         Result result;
         String finalResult = null;
         try {
             //if (hints != null && ! hints.isEmpty())
               //  result = barcodeReader.decode(bitmap, hints);
             //else
                 result = barcodeReader.decode(bitmap);
             // setting results.
             finalResult = String.valueOf(result.getText());
         } catch (Exception e) {
             e.printStackTrace();
           //  throw new BarcodeEngine().new BarcodeEngineException(e.getMessage());
         }
         return finalResult;
    }

}

When I executed this simple core java program I given exception

com.google.zxing.NotFoundException

Its not even gives any stackstrace.

I want to ask the experts that why such kind of exception is comming. Thanks You!

解决方案

I had the same problem. I used an image that I knew had a valid QR code and I also got the com.google.zxing.NotFoundException.

The problem is that the image you use as a source is to large for the library to decode. After I reduced the size of my image the QR code decoder worked.

For the purpose of my application, the QR code on the image would always be more or less in the same area, so I used the getSubimage function of the BufferedImage class to isolate the QR code.

     BufferedImage image;
     image = ImageIO.read(imageFile);
     BufferedImage cropedImage = image.getSubimage(0, 0, 914, 400);
     // using the cropedImage instead of image
     LuminanceSource source = new BufferedImageLuminanceSource(cropedImage);
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
     // barcode decoding
     QRCodeReader reader = new QRCodeReader();
     Result result = null;
     try 
     {
         result = reader.decode(bitmap);
     } 
     catch (ReaderException e) 
     {
         return "reader error";
     }

这篇关于当执行核心java程序时,com.google.zxing.NotFoundException异常会出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆