2013-03-08 122 views
0

我有這種方法,讀取位圖圖像解碼文檔中的某個區域中的qr代碼(尋找四角爲qr代碼)由於我有多麼我的代碼,它總是擊中錯誤信息,我知道它無法找到位圖,但我想要採取這種錯誤和翻譯方式,執行我的剩餘代碼,這是旋轉文件,並再次尋找qr位圖圖像。com.google.zxing.binarybitmap錯誤與處理位圖圖像

代碼:

Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 }; 
        string QRinfo = ""; 
        for (int i = 0; i < corners.Length; ++i) 
        { 
         string tempQRinfo = Process(corners[i]); 
         if (tempQRinfo == null) 
         { 
          QRinfo = tempQRinfo; 
          switch (i) 
          { 
           case 0: break; //upper left 
           case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break; 
           case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break; 
           case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break; 
          } 
          break; 
         } 
        } 

工藝方法,該方法導致錯誤沒有找到圖像時。

public string Process(Bitmap bitmap) 
    { 
     var reader = new com.google.zxing.qrcode.QRCodeReader(); 

     try 
     { 
      LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height); 
      var binarizer = new HybridBinarizer(source); 
      var binBitmap = new BinaryBitmap(binarizer); 
      return reader.decode(binBitmap).Text; 
     } 
     catch (Exception e) 
     { 
      return e.Message; 
     } 
    } 

的幫助:我想翻譯的錯誤消息,在所有四個角落地方有QR碼的文檔搜索,然後旋轉,如上圖所示。

+0

我還以爲你有這方面的工作?我在你的另一個問題中提到你需要驗證'Process'方法返回的字符串。這應該相對容易。我會在一分鐘內發佈答案。 – sparky68967 2013-03-08 17:20:03

回答

0

Process方法:

public string Process(Bitmap bitmap) 
{ 
    var reader = new com.google.zxing.qrcode.QRCodeReader(); 

    try 
    { 
     LuminanceSource source = new RGBLuminanceSource(bitmap, bitmap.Width, bitmap.Height); 
     var binarizer = new HybridBinarizer(source); 
     var binBitmap = new BinaryBitmap(binarizer); 
     return reader.decode(binBitmap).Text; 
    } 
    catch (Exception e) 
    { 
     //catch the exception and return null instead 
     return null; 
    } 
} 


然後在其他代碼:

Bitmap[] corners = new Bitmap[] { bandImg1, bandImg2, bandImg3, bandImg4 }; 
string QRinfo = null; 
for (int i = 0; i < corners.Length; ++i) 
{ 
    string tempQRinfo = Process(corners[i]); 
    if (tempQRinfo != null) //if the string is NOT null, then we found the QR. If it is null, then the for loop will continue searching if it has more corners to look at 
    { 
     QRinfo = tempQRinfo; 
     switch (i) 
     { 
      case 0: break; //upper left 
      case 1: fullImg.RotateFlip(RotateFlipType.Rotate270FlipNone); break; 
      case 2: fullImg.RotateFlip(RotateFlipType.Rotate90FlipNone); break; 
      case 3: fullImg.RotateFlip(RotateFlipType.Rotate180FlipNone); break; 
     } 
     break; 
    } 
} 

if(QRinfo == null) 
{ 
    //we never found the QR! 
    MessageBox.Show("Error! No QR found!"); 
} 
else 
{ 
    //here is the QR we found! 
    MessageBox.Show(QRinfo); 
} 


那麼,這一切確實是它把圖像的角落在一個數組中。應該保存QR信息的字符串設置爲null。該數組被循環遍歷,並且每個角都被傳遞給Process方法,以查看該角是否與QR碼一致。在Process方法中,QR閱讀器嘗試閱讀圖像。如果發生錯誤,則會被捕獲,並且該方法返回null。如果沒有錯誤,則該方法返回正確的字符串。一旦Process方法完成,我們檢查返回的字符串以確保它不是null。如果不是null,則發現QR並且我們將QR字符串提供給QRinfo,並且基於哪個角具有QR圖像來旋轉fullImg。如果字符串是IS null,那麼它將繼續循環顯示圖像,直到找到具有QR的任何一個,或者沒有剩下的圖像。

一旦循環完成,我們檢查QRinfo。如果它仍然是null那麼QR在任何角落都找不到。如果不是null,則找到QR並顯示QR字符串。

所以,這就是你要求的。它會吞下錯誤,並繼續查找QR,以便您可以發生QR旋轉和顯示。

注:我改變

string QRinfo = ""; 


string QRinfo = null; 
+0

好..目前它正在爲每個空角落拋出一個錯誤,直到它用qr代碼碰到角落 - 我不想顯示錯誤消息並旋轉,然後在找到時顯示代碼。 – Masriyah 2013-03-11 14:02:15

+0

@Amina好的...所以我刪除了例外的打印。代碼完成了你想要它做的一切。如果有任何混淆,我會添加其他信息。 – sparky68967 2013-03-11 17:03:18