This project has moved and is read-only. For the latest updates, please go here.

QRCodeReader.decode / BarcodeReader.Decode gives null in return

Apr 13, 2015 at 10:38 AM
I'm trying to decode a QR code with C# (inside webAPI).
I receive the image in a base64 format.
When I get a generated QR image, the decode works fine.
But when I send a photo of the same QR image, the decoding returns null,

How can I manage that the decoder recognized the scanned QR code?

Used code:
            //imageString = base64 string of image
            var bitmapData = Convert.FromBase64String(FixBase64ForImage(imageString));
            var streamBitmap = new System.IO.MemoryStream(bitmapData);
            var bitmap = new System.Drawing.Bitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(streamBitmap));

            var luminanceSource1 = new ZXing.BitmapLuminanceSource(bitmap);
            var binarizer1 = new ZXing.Common.HybridBinarizer(luminanceSource1);
            var mapa1 = new ZXing.BinaryBitmap(binarizer1);
            var readCode1 = new ZXing.QrCode.QRCodeReader();
            var result1 = readCode1.decode(mapa1);

            var options2 = new ZXing.Common.DecodingOptions();
            options2.TryHarder = true;
            var formats2 = new List<ZXing.BarcodeFormat>();
            options2.PossibleFormats = formats2;
            var reader2 = new ZXing.BarcodeReader { AutoRotate = true, TryInverted = true, Options = options2 };
            var result2 = reader2.Decode(bitmap);
when I send the generated image:
The qr code is decoded successfully

When I send the photo of the same image:
the qr code decode returnes null

What can I do to get the photo correctly decoded?
Apr 13, 2015 at 7:46 PM
Sometimes small differences in the binarized image can make a difference.
In your case if you try the GlobalHistogramBinarizer instead of the default HybridBinarizer.
var reader2 = new ZXing.BarcodeReader(null, null, ls => new GlobalHistogramBinarizer(ls)) { AutoRotate = true, TryInverted = true, Options = options2 };
Apr 14, 2015 at 7:30 AM
Thank you micjahn!
The photo-image is now detected!