c# - 使用 Zxing 读取 Xamarin Forms 中的二维码

标签 c# xamarin.forms qr-code zxing

我正在尝试从.png 文件 中导入QR 码 并使用Zxing.Net.Mobile 对其进行解码。和 ZXing.Net.Mobile.Forms .

如果我使用 ZXing.Mobile.MobileBarcodeScanner 类扫描 QR 码,解码会按要求工作,但是,当从文件导入它时,Qr 码阅读器 (ZXing. QrCode.QRCodeReader()) 解码函数总是返回null

因为我正在使用 Xamarin Forms;每个平台处理位图/图像创建,可移植部分处理其余部分(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);
}

我浏览了一些有同样问题的 SO 帖子,并尝试了以下方法:

  • 使用 BitmapLuminanceSource:仍然返回 null 并且需要使用另一个库
  • RGBLuminanceSource 使用不同的位图格式:RGB32、BGR32、ARGB32、ABGR32(每次更改 FileToBitmap 函数)
  • 尝试了不同的BinarizerGlobalHistogramBinarizer()
  • 通过读取文件并将其写回文件来检查文件是否被正确读取。
  • 我已尝试将 MultiFormatReader() 与 Pure 条形码一起使用,并尝试更难的提示
  • 我还调试了库源代码,据我所知,它只是无法在导入的图像中找到 QR 码。不会抛出异常。

这里是返回null的地方:

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

在线Zxing decoder可以正确解码我正在测试的二维码。这是我的测试二维码:

My QR Code Image

最佳答案

我解决了这个问题,在 Android 实现中使用这个方法从图像路径返回 RGBLuminanceSource

    public RGBLuminanceSource GetRGBLuminanceSource(string imagePath)
    {
        if (File.Exists(imagePath))
        {
            Bitmap bitmap = BitmapFactory.DecodeFile(imagePath);
            List<byte> rgbBytesList = new List<byte>();
            for (int y = 0; y < bitmap.Height; y++)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    var c = new Color(bitmap.GetPixel(x, y));
                    rgbBytesList.AddRange(new[] { c.A, c.R, c.G, c.B });
                }
            }
            byte[] rgbBytes = rgbBytesList.ToArray();
            return new RGBLuminanceSource(rgbBytes, bitmap.Height, bitmap.Width, RGBLuminanceSource.BitmapFormat.ARGB32);
        }
        return null;
    }

关于c# - 使用 Zxing 读取 Xamarin Forms 中的二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44901943/

相关文章:

xamarin.forms - iOS 应用程序中的工具栏图标具有 xamarin 表单的色彩

c# - 如何在 Xamarin.Forms 中创建类似于 Facebook 应用程序的选项卡式页面

android - flutter : How to get the format of the scanned barcode using flutter?

用于打开相机和扫描二维码的Android API?

c# - 使用 UDP 接收时安全地结束线程

c# - 在 VS2015 中以 .Net 4.5 为目标,但我可以使用 C# 6 功能吗?

mvvm - Xamarin Forms - 绑定(bind)图像比例

c - qrencode - 段错误(核心转储)

c# - 覆盖 Asp.net MVC 中的全局缓存设置

c# - WCF .net 4.0 HTTP 基本身份验证