2017-07-04 257 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