首页 > 解决方案 > com.google.zxing.NotFoundException - 在 Android 中从图库中扫描 QR 图像时

问题描述

尝试从图库扫描 QR 图像时出现以下错误。而且文件大小只有4kb。

com.google.zxing.NotFoundException

下面是示例代码:

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

标签: android

解决方案


通过使用 google play services vision API 修复了该问题。以下是对解决问题非常有帮助的链接。

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


推荐阅读