zxing / zxing

ZXing ("Zebra Crossing") barcode scanning library for Java, Android
Apache License 2.0
32.8k stars 9.36k forks source link

NotFoundException #628

Closed CoXier closed 8 years ago

CoXier commented 8 years ago

Recently I have a difficulty using zxing to decode bitmap. I search solutions on Internet and I have tried some of them.Here is my try:

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.qrcode);
    String result = DecodeUtils.decodeWithZxing(bitmap);

R.drawable.qrcode is a .jpg file.

And BarCodeUtil.java is:

 public static String decodeWithZxing(Bitmap bitmap) {
    MultiFormatReader multiFormatReader = new MultiFormatReader();
    Map<DecodeHintType, Object> hints = new Hashtable<>();
    hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
    multiFormatReader.setHints(hints);

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    Result rawResult = null;
    RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

    if (source != null) {
        BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(source));
        try {
            rawResult = multiFormatReader.decodeWithState(binaryBitmap);
        } catch (ReaderException re) {
            re.printStackTrace();
        } finally {
            multiFormatReader.reset();
        }
    }
    return rawResult != null ? rawResult.getText() : null;
}

But when I run the above code,I got a exception:

com.google.zxing.NotFoundException

So I search the exception,someone think bitmap size causes this exception.Then I resize the bitmap size:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inSampleSize = 4; 
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.zhifubao,options);
    String result = DecodeUtils.decodeWithZxing(bitmap)

But it still does not work for me.

Is there a good solution to decode bitmap with a qrcode?

srowen commented 8 years ago

Please don't ask questions here. Use the mailing list. It's not clear what you're asking; it's normal to get NotFoundException in some images. Maybe consider TRY_HARDER or PURE_BARCODE mode.

CoXier commented 8 years ago

I am sorry for that!