使用Zxing在Xamarin Forms中读取QR码

3
我正在尝试从一个.png文件中导入一个QR码并使用Zxing.Net.MobileZXing.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
< p > DependencyService 将调用平台特定的函数,目前我正在使用 Android,因此该函数如下:

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()
  • 我还调试了库源代码,从我的理解来看,它只是找不到导入图像中的QR码。没有抛出异常。

这里是返回null的地方:

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

这个在线Zxing解码器可以正确地解码我正在测试的QR码。这是我的测试QR码:

My QR Code Image


一个 Java 解决方案可以帮助您找到正确的路径,https://dev59.com/rU_Ta4cB1Zd3GeqPCJdd。 - Kevin
谢谢您提供的链接,但我已经尝试过这个解决方案,但没有成功。 - PMARSH
1个回答

0

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接