简体   繁体   中英

Reading QR code in Xamarin Forms With Zxing

I am trying to import a QR code from a .png file and decode it using Zxing.Net.Mobile and ZXing.Net.Mobile.Forms .

If I scan the QR code using the ZXing.Mobile.MobileBarcodeScanner class, the decoding works as required, however, when importing it from a file, the Qr code reader ( ZXing.QrCode.QRCodeReader() ) decode function always returns null .

As I'm using Xamarin Forms ; each platform handles the bitmap/image creation and the portable part handle the rest ( Zxing BinaryBitmap creation and decoding).

//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

The DependencyService will call the platform specific function, at the moment I am working with Andorid so, the function is as follows:

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);
}

I have looked through some of the posts on SO that are having the same problem and have tried the following:

  • Using BitmapLuminanceSource : still returns null and requires the use of another library
  • Using different bitmap formats for the RGBLuminanceSource : RGB32, BGR32, ARGB32, ABGR32 (each time changing the FileToBitmap function)
  • Tried the different Binarizer , GlobalHistogramBinarizer()
  • Checked that the file is being read correctly by reading and wrinting it back to a file.
  • I have tried using the MultiFormatReader() with the Pure barcode and try harder hints
  • I have also debugged the libraries source code and from what I understand it just can't find the QR code in the imported image. No exception is thrown.

Here is where the return null is made:

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

The online Zxing decoder can decode the QR code I'm testing correctly. Here is my test QR code:

我的二维码图片

I solved that problem, with this method in the Android implementation to return an RGBLuminanceSource from the path of the image

    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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM