2017-07-04 260 views
1

我想從.png文件導入QR碼並使用Zxing.Mobile.net(https://www.nuget.org/packages/ZXing.Net.Mobile/https://www.nuget.org/packages/ZXing.Net.Mobile.Forms)對其進行解碼。閱讀Xamarin Forms與Zxing的QR碼

如果我使用ZXing.Mobile.MobileBarcodeScanner類掃描QR碼,解碼將按需要工作,但是,從文件導入時,Qr代碼閱讀器(ZXing.QrCode.QRCodeReader())解碼函數始終返回null。

當我使用xamarin表單時,每個平臺處理位圖/圖像創建,便攜部分處理其餘部分(Zxing BinaryBitmap創建和解碼)。

//Store rawBytes and image demensions 
PotableBitmap bMap = DependencyService.Get<IBitmap>().FileToBitmap(filePath); 

RGBLuminanceSource source = new RGBLuminanceSource(bMap.RgbBytes, bMap.Width, bMap.Height, RGBLuminanceSource.BitmapFormat.RGB32); 
HybridBinarizer binarized = new HybridBinarizer(source); 
BinaryBitmap bitmap = new BinaryBitmap(binarized); 
var reader = new ZXing.QrCode.QRCodeReader(); 
data = reader.decode(qrCodeBitmap); // This is always null 

的DependencyService將調用平臺特定的功能,此刻我與Andorid的工作,所以,功能如下:

public PortableBitmap FileToBitmap(string ms) 
{ 
    var bytes = File.ReadAllBytes(ms); 
    Android.Graphics.Bitmap bMap = BitmapFactory.DecodeByteArray(bytes, 0, bytes.Length); 

    int[] intArray = new int[bMap.Width * bMap.Height]; 
    bMap.GetPixels(intArray, 0, bMap.Width, 0, 0, bMap.Width, bMap.Height) 

    List<byte> result = new List<byte>(); 
    foreach (int intRgb in intArray) 
    { 
     Color pixelColor = new Color(intRgb); 
     result.Add(pixelColor.R); 
     result.Add(pixelColor.G); 
     result.Add(pixelColor.B); 
    } 

    return new PortableBitmap(result.ToArray(), bMap.Width, bMap.Height); 
} 

我已經經歷了一些職位,這樣看着有同樣的問題,並曾嘗試以下:

  • 使用BitmapLuminanceSource:仍然返回null,需要使用另一個庫的
  • 使用不同的位圖格式的RGBLuminanceSource:RGB32,BGR32,ARGB32,ABGR32
  • 試過不同二值化(每次改變FileToBitmap功能),GlobalHistogramBinarizer()
  • 經過通過閱讀和wrinting認爲文件正在被正確地讀出回到一個文件。更難提示
  • 我已經使用MultiFormatReader()與純條形碼嘗試和嘗試,我也調試庫的源代碼,並從我的理解,它只是無法找到導入的圖像中的QR碼。沒有例外被拋出。

這裏就是返回NULL由:

private FinderPattern[] selectBestPatterns() 
    { 
     int startSize = possibleCenters.Count; 
     if (startSize < 3) 
     { 
      // Couldn't find enough finder patterns 
      return null; // It returns here 
     } 
     ... 

在線斑馬線解碼器(https://zxing.org/w/decode.jspx)可以解碼QR碼我正確的測試。這是我測試的QR碼:

enter image description here

+0

您的問題的Java解決方案,它可能會幫助把你放在正確的路徑,https://stackoverflow.com/questions/3422651/decoding-qr-code-from-image-stored-on-the-phone -with-zxing-on-android-phone – Kevin

+0

謝謝你的鏈接,但我已經試過這個解決方案沒有成功 – PMARSH

回答

0

創建一個掃描儀頁面

你都能夠得到條碼掃描在您的應用程序的代碼只需兩行。首先,創建一個新的ZXingScannerPage,然後按下頁面上的導航堆棧:

var scanPage = new ZXingScannerPage(); 
// Navigate to our scanner page 
await Navigation.PushAsync (scanPage); 

這將帶來條形碼掃描器,放入掃描模式立即。

檢查結果 當然,您需要檢查實際掃描結果。

scanPage.OnScanResult += (result) => 
{ 
    // Stop scanning 
    scanPage.IsScanning = false; 

    // Pop the page and show the result 
    Device.BeginInvokeOnMainThread (async() => 
    { 
     await Navigation.PopAsync();   
     await DisplayAlert("Scanned Barcode", result.Text, "OK"); 
    }); 
}; 

處理權限 在Android斑馬線:當OnScanResult事件被觸發停止掃描,彈出掃描儀頁面,並檢查結果,你可以做到這一點。網絡需要照相機和手電筒的權限運行,所以你需要在項目設置檢查這些權限,或將它們添加到您的Android清單:

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-permission android:name="android.permission.FLASHLIGHT" /> 

獲取更多信息,您可以使用此鏈接

https://blog.xamarin.com/barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms/

+0

我能夠掃描QR碼。我有掃描儀頁面。我希望能夠做的是允許用戶從圖像文件(.png)中導入qr代碼。但是,圖書館無法檢測到我傳遞給解碼功能的圖像中的QR碼。 – PMARSH