Zxing multiple qrcode reading dot net core

156 Views Asked by At

I'm trying to create a simple library for my project where I can read multiple QR codes from a single image.

So far, I have a simple BMP reader:

var bytes = File.ReadAllBytes(@"C:\\test qr\\testowa bitmapa.bmp");

var result = Decrypt.DecryptQRs(bytes, 771, 454);

result.ForEach(Console.WriteLine);

And here's the library part:

public static List<string> DecryptQRs(byte[] imageBytes, int bitmapWidth, int bitmapHeight)
{
    LuminanceSource ls = new RGBLuminanceSource(imageBytes, bitmapWidth, bitmapHeight);

    BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(ls));

    var reader = new QRCodeMultiReader();
    var results = reader.decodeMultiple(binaryBitmap);

    return results.Select(x=>x.Text).ToList();
}

This is a really simple attempt without any error handling or additional features.

In this version, everything is working smoothly up until the decoding of multiple QR codes:

var results = reader.decodeMultiple(binaryBitmap);

The results variable is null, even though the luminance source and binary bitmap are generated correctly.

Do any of you have experience with this library and decoding multiple QR codes?

1

There are 1 best solutions below

0
Michael On

If your file "testowa bitmapa.bmp" really contains true bitmap file data then you can't read it this way. There is a file header at the beginning which RGBLuminanceSource doesn't handle. It takes the header as pixel data which wouldn't work. Please try the following code snippet:

    public static List<string> DecryptQRs(string filename)
    {
        var barcodeReader = new BarcodeReader();
        using(var barcodeBitmap = (Bitmap)Bitmap.FromFile(filename))
        {
            var results = barcodeReader.DecodeMultiple(barcodeBitmap);
            return results.Select(x=>x.Text).ToList();
        }
    }

    var results = Decrypt.DecryptQRs(@"C:\\test qr\\testowa bitmapa.bmp");

    results.ForEach(Console.WriteLine);