vishnu
vishnu

Reputation: 4599

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

Getting the following error while trying to scan the QR image from gallery. And the file size is only 4kb.

com.google.zxing.NotFoundException

Below is the sample code:

@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;
    }
  }

Upvotes: 1

Views: 930

Answers (2)

sanevys
sanevys

Reputation: 559

This could be because QR code image is too big or too small. I'm not talking about bitmap size. But more about QR as an object in image. For example if you try to scan this image that only consist of QR: enter image description here

It most likely fail.

Try something like this. This image does not consist of only one big QR thus scanner has higher chance to decode it: enter image description here

Upvotes: 0

vishnu
vishnu

Reputation: 4599

Fixed the issue by using the google play services vision API. Below is the link really helpful to fix the issue.

https://code.tutsplus.com/tutorials/reading-qr-codes-using-the-mobile-vision-api--cms-24680

Upvotes: 1

Related Questions