2010-08-06 59 views
4

我有一個從服務器接收qr碼的應用程序。我想解碼它(而不是意圖和相機),並顯示它包含在我的應用程序中的文本。我已經alredy與罐做到了這一點在Java SE從這個代碼斑馬線:使用Zxing從手機上存儲的圖像解碼qr碼(在Android手機上)

private class QRCodeDecoder { 
     public String decode(File imageFile) { 
     BufferedImage image; 
     try { 
     image = ImageIO.read(imageFile); 
     } catch (IOException e1) { 
     return "io outch"; 
     } 

     // creating luminance source 
     LuminanceSource lumSource = new BufferedImageLuminanceSource(image); 
     BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(lumSource)); 

     // barcode decoding 
     QRCodeReader reader = new QRCodeReader(); 

     Result result = null; 
     try { 
     result = reader.decode(bitmap); 
     } catch (ReaderException e) { 
     return "reader error"; 
     } 

     return result.getText(); 

     } 
     } 

但是,在Android上的BufferedImage沒有找到。 有沒有人從手機上存儲的圖像解碼Android上的qr碼? Tnx。

+0

嗨! 你解決了這個問題嗎? 我有同樣的問題,直到知道我無法找到解決方案。 在此先感謝 – 2010-12-17 10:22:54

+0

不,我沒有任何運氣,所以我不得不忘記這個想法。也許現在,半年後,事情已經解決了。 – DixieFlatline 2010-12-23 14:35:02

+0

captureActivity從相機中捕獲QR碼圖像,解碼後會根據存儲在QR碼中的數據類型顯示結果。例如如果網站URL使用QR碼編碼,結果屏幕將會有一個按鈕來打開該URL和likevise。 我需要從SD卡讀取圖像,解碼它並以相同的方式處理輸出zxing在通過captureActivity解碼的情況下。 在「結果結果」中輸出結果後需要做什麼? – 2012-03-28 12:37:40

回答

-3

Quickmark和qr droid實際上會讀出代碼所說的內容,並且您可以解碼保存在手機上的條形碼。當你加載圖像並選擇共享時,點擊菜單按鈕,找到解碼qr droid或解碼quickmark,並且會做魔術。我更喜歡閱讀代碼的快速標記,因爲它告訴我代碼中輸入的是什麼。

+3

但我需要在代碼中。 – DixieFlatline 2011-02-17 08:04:01

2

從谷歌代碼下載ZXing,這個類文件:ZXing-1.6/zxing-1.6/androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java可以幫助你。

10

在Android中,你可以這樣說:

@Override 
    protected Result doInBackground(Void... params) 
    { 
     try 
     { 
      InputStream inputStream = activity.getContentResolver().openInputStream(uri); 
      Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
      if (bitmap == null) 
      { 
       Log.e(TAG, "uri is not a bitmap," + uri.toString()); 
       return null; 
      } 
      int width = bitmap.getWidth(), height = bitmap.getHeight(); 
      int[] pixels = new int[width * height]; 
      bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 
      bitmap.recycle(); 
      bitmap = null; 
      RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); 
      BinaryBitmap bBitmap = new BinaryBitmap(new HybridBinarizer(source)); 
      MultiFormatReader reader = new MultiFormatReader(); 
      try 
      { 
       Result result = reader.decode(bBitmap); 
       return result; 
      } 
      catch (NotFoundException e) 
      { 
       Log.e(TAG, "decode exception", e); 
       return null; 
      } 
     } 
     catch (FileNotFoundException e) 
     { 
      Log.e(TAG, "can not open file" + uri.toString(), e); 
      return null; 
     } 
    } 
+0

你有沒有測試過這個解決方案? – Erum 2015-01-26 18:08:16

+0

我測試過這個解決方案,是的它的工作 – n00b 2015-05-14 11:30:29

+0

有人可以請讓我們知道如何獲得圖像的URI – dmSherazi 2016-08-16 09:33:55