【问题标题】:Reading QR code in Xamarin Forms With Zxing使用 Zxing 读取 Xamarin 表单中的 QR 码
【发布时间】:2017-12-07 16:31:57
【问题描述】:

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

如果我使用ZXing.Mobile.MobileBarcodeScanner 类扫描二维码,解码会按要求进行,但是,当从文件导入时,二维码阅读器 (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);
}

我浏览了一些关于 SO 的帖子,它们遇到了同样的问题,并尝试了以下方法:

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

这里是返回 null 的地方:

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

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

【问题讨论】:

  • 你的问题的java解决方案,它可能会帮助你走上正确的道路,stackoverflow.com/questions/3422651/…
  • 谢谢你的链接,但我已经尝试过这个解决方案,但没有成功

标签: c# xamarin.forms qr-code zxing


【解决方案1】:

我解决了这个问题,在 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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多