2013-04-03 111 views
0

我正在使用zxing核心並將其與我的android應用程序集成在一起拍攝照片。我將位圖圖片輸入到RGBLuminanceSource。但每次我得到NOTFoundException。我不想通過intents或CaptureActivity與zxing條碼掃描器集成。這是代碼。com.google.zxing.NotFoundException解碼包含條形碼的圖像的所有可能性

 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 
+0

爲什麼不實際顯示你正在解碼的形象呢? –

+0

@Sean這裏是圖像.. http://s1.postimg.org/pim8l22yn/product.jpg – tousif

+0

和條碼Android應用程序可以讀取此。 – tousif

回答

0

我不得不使用bitmap.config在輸入流中的rgb中創建位圖。

0

是的,它解碼一般。最可能的答案就是這個特定的圖像不掃描。也許,試試一個沒有陰影的人。也有可能你沒有正確解析圖像。嘗試其他圖像來驗證

+0

我試過不同的圖像,沒有陰影結果是一樣的。甚至試圖將圖像保存到SD卡而不是讀取和解碼。任何幫助? – tousif

+0

然後調試你如何閱讀圖像。例如,您的代碼建議您假定ARGB數據,但Android默認爲YUV平面。你可能正在讀噪音。 –

+0

如果我使用PlanarYUVLuminanceSource(byte [] yuvData,int dataWidth,int dataHeight,int left,int top, int width,int height,boolean reverseHorizo​​ntal)左邊和上邊可能是什麼值。 dataheight可以和高度一樣嗎?怎麼樣datawidth – tousif

-2

這適用於我。我沒有使用zxing。

添加到搖籃:

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(); 
     } 
    } 
}