com.google.zxing.NotFoundException,用于解码包含条形码的图像的所有可能性

时间:2013-04-03 07:06:57

标签: android zxing

我正在使用zxing核心并集成了我的Android应用程序拍照。我正在给RGBLuminanceSource提供位图图片输入。但每次我得到NOTFoundException。我不想通过意图或CaptureActivity与zxing条形码扫描仪集成。这是代码。

       Map<DecodeHintType,Object> HINTS;
       Map<DecodeHintType,Object> HINTS_PURE;
       HINTS = new EnumMap<DecodeHintType,Object>(DecodeHintType.class);
       HINTS.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
       HINTS.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
       HINTS_PURE = new EnumMap<DecodeHintType,Object>(HINTS);
       HINTS_PURE.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
       ImageView mImg;

       if (requestCode == SCANNER_REQUEST_CODE && resultCode == RESULT_OK) {  
          // Bitmap bMap = (Bitmap) data.getExtras().get("data");

        Bitmap bMap=BitmapFactory.decodeStream(getContentResolver().openInputStream(outputFileUri));


            // creating binary bitmap from source image


           int length=bMap.getWidth()*bMap.getHeight()*10;
           int[] intArray = new int[bMap.getWidth()*bMap.getHeight()]; 
           bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(),                bMap.getHeight());

           //test view to display captured image 
           mImg = (ImageView) findViewById(R.id.imageView);
           mImg.setImageBitmap(bMap);


           // zxing decoding of bitmap image
           Reader reader = new MultiFormatReader();
           String msg=bMap.getWidth()+" * "+bMap.getHeight();
           Log.e("DEBUGGG", msg);
           LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(),intArray);


           BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));


           Collection<Result> results = new ArrayList<Result>(1);
           ReaderException savedException = null;



           try {
                  // Look for multiple barcodes
                  MultipleBarcodeReader multiReader = new GenericMultipleBarcodeReader(reader);

                  Result[] theResults = multiReader.decodeMultiple(bitmap, HINTS);
                  if (theResults != null) {
                    results.addAll(Arrays.asList(theResults));
                  }
                } catch (ReaderException re) {
                    re.printStackTrace();
                  savedException = re;
                  re.printStackTrace();
                }


                if (results.isEmpty()) {
                    try {
                      // Look for pure barcode
                      Result theResult = reader.decode(bitmap, HINTS_PURE);
                      if (theResult != null) {
                        results.add(theResult);
                      }
                    } catch (ReaderException re) {
                      savedException = re;
                      re.printStackTrace();

                    }
                  }

                  if (results.isEmpty()) {
                    try {
                      // Look for normal barcode in photo
                      Result theResult = reader.decode(bitmap, HINTS);
                      if (theResult != null) {
                        results.add(theResult);
                      }
                    } catch (ReaderException re) {
                      savedException = re;
                      re.printStackTrace();
                    }
                  }

                  if (results.isEmpty()) {
                    try {
                      // Try again with other binarizer
                      BinaryBitmap hybridBitmap = new BinaryBitmap(new HybridBinarizer(source));
                      Result theResult = reader.decode(hybridBitmap, HINTS);
                      if (theResult != null) {
                        results.add(theResult);
                      }
                    } catch (ReaderException re) {
                      savedException = re;
                      re.printStackTrace();
                    }
                  }
                String barcoderesult="";
                  for (Result result : results) {
                      barcoderesult=barcoderesult+result.getText();
                      Log.e("Debugger","code:  " +result.getText() );
                    }

                //result is empty and notfound exception in logs

3 个答案:

答案 0 :(得分:0)

是的,它一般解码很好。最可能的答案就是这个特定的图像不扫描。也许,尝试一个没有阴影的人。您也可能无法正确解析图像。尝试其他图片验证

答案 1 :(得分:0)

我必须使用bitmap.config从输入流创建rgb中的位图。

答案 2 :(得分:-2)

这对我有用。我没有使用zxing。

添加到Gradle:

compile 'com.google.android.gms:play-services:7.8.0'

代码:

    if (requestCode == SCANNER_REQUEST_CODE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();         
        String textQR="";            

        Bitmap myQRCode = null;
        try {
            InputStream inputStream = getContentResolver().openInputStream(selectedImage);
            myQRCode = BitmapFactory.decodeStream(inputStream);
        } catch (IOException e) {
            Toast.makeText(youractivity.this,  e.getMessage(), Toast.LENGTH_LONG).show();
        }

        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();

        Frame myFrame = new Frame.Builder().setBitmap(myQRCode).build();

        SparseArray<Barcode> barcodes = barcodeDetector.detect(myFrame);

        if (barcodes.size() > 0) {
            textQR = barcodes.valueAt(0).displayValue;
        }
        else{
            Toast.makeText(youractivity.this, "QR not Found", Toast.LENGTH_LONG).show();
        }
    }
}