com.google.zxing.NotFoundException - reading the QR image from gallery in Android

  • Replies:11
  • Answered
vishnu siddareddy
  • Forum posts: 4

Nov 27, 2020, 12:26:14 PM via Website

Getting the following error while trying to read the QR image from gallery,

com.google.zxing.NotFoundException

Also try to resize the image but no luck.

Below is the sample code:

findViewById(R.id.readQRBtn).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            System.out.println("called hrre>>>");
            Intent pickIntent = new Intent(Intent.ACTION_PICK);
            pickIntent.setDataAndType( android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(pickIntent, 111);
          }
        });

@Override
  protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    switch (requestCode) {
      //the case is because you might be handling multiple request codes here
      case 111:
        Uri selectedImage = imageReturnedIntent.getData();
        InputStream imageStream = null;
        try {
          //getting the image
          imageStream = getContentResolver().openInputStream(selectedImage);
        } catch (FileNotFoundException e) {
          Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
          e.printStackTrace();
        }
        //decoding bitmap
        Bitmap bMap = BitmapFactory.decodeStream(imageStream);
        int[] intArray = new int[bMap.getWidth() * bMap.getHeight()];
        // copy pixel data from the Bitmap into the 'intArray' array
        bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());

        LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Reader reader = new MultiFormatReader();// use this otherwise
        try {
          Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
          decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
          decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);

          Result result = reader.decode(bitmap, decodeHints);
          String barcode =  result.getText().toString();
        } catch (NotFoundException e) {
          e.printStackTrace();
        } catch (ChecksumException e) {
          e.printStackTrace();
        } catch (FormatException e) {
          e.printStackTrace();
        } catch (NullPointerException e) {
          e.printStackTrace();
        }
        break;
    }
  }

— modified on Nov 27, 2020, 12:28:28 PM

Reply
Best answer
James Watson
  • Forum posts: 1,592

Nov 28, 2020, 5:48:06 AM via Website

It just means no barcode was found. In general, the issue results from your QR image from gallery is too large for the zxing library to decode.

Reduce the size of your image or replace reader.decode(...) with reader.decodeWithState(...) in your onActivityResult method.

Download size < 0.12 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Pahan_228marco sarli
Reply
vishnu siddareddy
  • Forum posts: 4

Nov 28, 2020, 7:02:51 AM via Website

@James, Thanks for your quick reply. I have tried using with the 'decodeWithState()' and not working.
But the same QR code reading through online decoder zxing.org it is working fine. can you please check and provide a better solution.

Helpful?
Pahan_228
Reply
James Watson
  • Forum posts: 1,592

Nov 28, 2020, 7:55:32 AM via Website

vishnu siddareddy

@James, Thanks for your quick reply. I have tried using with the 'decodeWithState()' and not working.
But the same QR code reading through online decoder zxing.org it is working fine. can you please check and provide a better solution.

If the 'decodeWithState()' doesn't work for you, you have to reduce the image size.
The QR code reading in Android apps dosen't work the same way as the reading through online decoder zxing.org. In fact, when you run nearly exactly the same codes on the Java SE libs, it will work. (smug)

— modified on Nov 28, 2020, 8:04:49 AM

Download size < 0.12 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Pahan_228marco sarli
Reply
vishnu siddareddy
  • Forum posts: 4

Nov 28, 2020, 9:09:37 AM via Website

@james, I have tried using the below function for resizing the image but unfortunately this does not provide the solution, could you please suggest any helpful links to sort out this issue?

calling the function like,

//decoding bitmap
        Bitmap bMap = BitmapFactory.decodeStream(imageStream);
        bMap = bitmapResize(bMap);

public Bitmap bitmapResize(Bitmap imageBitmap) {
    Bitmap bitmap = imageBitmap;
    float heightbmp = bitmap.getHeight();
    float widthbmp = bitmap.getWidth();

    // Get Screen width
    DisplayMetrics displaymetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    float height = displaymetrics.heightPixels / 3;
    float width = displaymetrics.widthPixels / 3;

    int convertHeight = (int) height, convertWidth = (int) width;

    // higher
    if (heightbmp > height) {
      convertHeight = (int) height - 20;
      bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHeight, true);
    }

    // wider
    if (widthbmp > width) {
      convertWidth = (int) width - 20;
      bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth,
        convertHeight, true);
    }

    System.out.println("converted width"+ bitmap.getWidth());
    System.out.println("converted height"+ bitmap.getHeight());

    return bitmap;
  }
Helpful?
Reply
James Watson
  • Forum posts: 1,592

Nov 28, 2020, 10:52:01 AM via Website

vishnu siddareddy

@james, I have tried using the below function for resizing the image but unfortunately this does not provide the solution, could you please suggest any helpful links to sort out this issue?

calling the function like,

//decoding bitmap
        Bitmap bMap = BitmapFactory.decodeStream(imageStream);
        bMap = bitmapResize(bMap);

public Bitmap bitmapResize(Bitmap imageBitmap) {
    Bitmap bitmap = imageBitmap;
    float heightbmp = bitmap.getHeight();
    float widthbmp = bitmap.getWidth();

    // Get Screen width
    DisplayMetrics displaymetrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    float height = displaymetrics.heightPixels / 3;
    float width = displaymetrics.widthPixels / 3;

    int convertHeight = (int) height, convertWidth = (int) width;

    // higher
    if (heightbmp > height) {
      convertHeight = (int) height - 20;
      bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHeight, true);
    }

    // wider
    if (widthbmp > width) {
      convertWidth = (int) width - 20;
      bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth,
        convertHeight, true);
    }

    System.out.println("converted width"+ bitmap.getWidth());
    System.out.println("converted height"+ bitmap.getHeight());

    return bitmap;
  }

Okay. More ideas to you. But would you like to support my free app? :D
In addition to createScaledBitmap(), make sure the barcode on the picture already has a right orientation. And please turn on some flags of the DecodeHintType class.

Download size < 0.12 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Chad V Jones
  • Forum posts: 9

Nov 30, 2020, 12:11:42 AM via Website

It will be interesting for me too

Helpful?
Reply
Favilo44
  • Forum posts: 14

Dec 3, 2020, 1:38:33 AM via Website

Yeah, it's hampering. I also faced the same issue.

Helpful?
Reply
vishnu siddareddy
  • Forum posts: 4

Dec 4, 2020, 12:28:30 PM via Website

@James, Finally got the solution(after one week) with google play services: vision API. Now am able to decrypt the QR of any size.
The below reference link really helpful for me.
code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680

Thanks a lot.

Helpful?
James Watson
Reply
Favilo44
  • Forum posts: 14

Dec 5, 2020, 2:50:28 PM via Website

That's very good if you got the solution. Thanks for letting us know.

Helpful?
Reply
James Watson
  • Forum posts: 1,592

Dec 6, 2020, 6:36:05 AM via Website

vishnu siddareddy

@James, Finally got the solution(after one week) with google play services: vision API. Now am able to decrypt the QR of any size.
The below reference link really helpful for me.
code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680

Thanks a lot.

Cool. (cool)

Download size < 0.12 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Reply
Favilo44
  • Forum posts: 14

Jan 13, 2021, 11:53:05 AM via Website

All the best and enjoy!

Helpful?
Reply