This project has moved. For the latest updates, please go here.

Failing to find a QR code in document

Apr 8, 2014 at 7:06 PM
I'm brand new to this library. I can't seem to decode an image that I would have thought was fairly simple:

http://d0.phys.washington.edu/~gwatts/Page4.jpg

And the code that I'm using to do this:
                var x = Bitmap.FromFile(f) as Bitmap;
                if (x == null)
                {
                    Console.WriteLine("No image");
                }
                else
                {
                    var dc = new BarcodeReader();
                    //dc.Options.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
                    Console.Write("{0} ({1}x{2}): ", f, x.Width, x.Height);
                    var result = dc.Decode(x);
                    if (result == null)
                    {
                        Console.WriteLine("Nothing found");
                    }
                    else
                    {
                        Console.WriteLine(result.Text);
                    }
                }
However, I get out:
Page4.jpg (2550x3501): Nothing found
Am I doing something wrong here?
Coordinator
Apr 9, 2014 at 7:19 PM
Please use the option TryHarder = true in your case.
Your image has a high resolution and the barcode is located in the upper right corner and uses only a small area of the whole picture.
The "use-case" of your szenario is mostly desktop or server application which looks for barcodes in an image from a scanner device.
That szenario differs in one important point from a use-case with a mobile device: the barcode is not center in the middle of the camera.
With the option TryHarder = true the barcode detector looks "deeper" into the image to find a possible barcode, not only in the middle of the image.
On the other hand it uses more CPU cycles. But that doesn't matter for desktop or server based applications in most cases.
                var x = Bitmap.FromFile(f) as Bitmap;
                if (x == null)
                {
                    Console.WriteLine("No image");
                }
                else
                {
                    var dc = new BarcodeReader
                    {
                        Options = new DecodingOptions
                        {
                            TryHarder = true
                        }
                    };
                    //dc.Options.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
                    Console.Write("{0} ({1}x{2}): ", f, x.Width, x.Height);
                    var result = dc.Decode(x);
                    if (result == null)
                    {
                        Console.WriteLine("Nothing found");
                    }
                    else
                    {
                        Console.WriteLine(result.Text);
                    }
                }
Marked as answer by gwatts on 4/9/2014 at 2:18 PM
Apr 9, 2014 at 9:18 PM
Ah, great. Thanks! It definitely is better. Though it doesn't find everything I would expect. For example, it misses the one in the same location on this page:

http://d0.phys.washington.edu/~gwatts/Page6.jpg

Is the quality just too low for what I am expecting it to do?
Apr 10, 2014 at 7:49 AM
I'm facing a somewhat similar problem, so pls allow me to hijack this thread.

I downloaded the Page6.jpg image and tried to recognize it but failed.
I also tested by cropping only the barcode from the whole page to recognize it but also failed.
However, if I use the zxing Android app and using the smartphone camera, I am able to recognize it!
So what's the difference here?

I generated some QR Code but sometimes, I cannot recognize it.
But I can still recognize it using the zxing for android.
So, I'm wondering where did I do wrong???
Apr 10, 2014 at 9:48 PM
No problem hijacking the thread! :-) I'd be curious of the answer as well!
Coordinator
Apr 11, 2014 at 7:35 PM
I tried the image with the java original version without success:
http://zxing.org/w/decode.jspx
So it shouldn't be a porting error.

After that I looked a little bit closer into the image. First, the algorithm finds the QR code but has troubles while decoding the content.
The algorithm has problems with the edges of the black areas.
It seems to me that the QR code is or was already binarized before it was added to the image. The binarizer of the document scanner
or some other preprocessing step introduced some dithering to the borders.
The algorithm doesn't work well with pre-binarized (2-bit) images with dithering.
Please use 8-bit grayscale scannings. The zxing binarizer is more optimized for the barcode decoding steps.

Image