Problem with QR decoding - probably solved

1,399 views
Skip to first unread message

Jiří Petráš

unread,
Nov 16, 2011, 4:44:43 AM11/16/11
to zx...@googlegroups.com
Hi all,

sometimes I had a problem with decoding QR code image directly generated from QR encoder. After many hours source code searching, I found, that  the function 

image.get_Renamed(j, i)  in com.google.zxing.qrcode.detector.FinderPatternFinder.find(System.Collections.Hashtable hints) -- line 132 in C# code

has probably switched parameters. 

After changing code to "get_Renamed(i, j)" my problem was solved.

Maybe this could help you.

Have a nice day.

Jiri Petras

Sean Owen

unread,
Nov 16, 2011, 5:08:13 AM11/16/11
to zx...@googlegroups.com
Hm, I'm not sure about that. What is the order of parameters supposed to be -- x,y? Meaning, horizontal then vertical coordinate? That's very standard for image APIs, and how the Java APIs work for instance, and if so this line is correct.

If it solves a problem for you, then I wonder if you are actually scanning mirror-image QR codes rather than valid ones?

Jiri Petras

unread,
Nov 27, 2011, 8:45:50 AM11/27/11
to zxing
Hi,

I thing it's not about horizontal and vertical changing, but about
mapping image into linear array (named bits) in
com.google.zxing.common.BitMatrix class.
The image is saved into BitMatrix by rows, it is mapped into bits
array, but when detector try to find pattern, indexes are changed,
wrong bits are returned and pattern finder detects error.

Jiri Petras

Sean Owen

unread,
Nov 27, 2011, 3:21:30 PM11/27/11
to zx...@googlegroups.com
There could be a problem, but I'm not sure where it is. The line you cite is probably correct, just based on your description. Do you have a more concrete test case, that leads you to a different change perhaps?

Bas Vijfwinkel

unread,
Nov 27, 2011, 11:01:55 PM11/27/11
to zxing
I vaguely remember that I also spend some debugging some parts of the
Actionscript stuff that involved switched i,j values.
But note that during a certain ZXing revision all i,j pairs became j,i
pairs and all underlying code was also changed to reflect these
changes.
If flipping back to i,j solves your problem, maybe check if some other
source files might be out of date.

Jiri Petras

unread,
Nov 28, 2011, 10:17:57 AM11/28/11
to zxing
Hi all.

This is my code for QRcode creation:
...
System.Drawing.Bitmap bMap =
(System.Drawing.Bitmap)image;

LuminanceSource source = new RGBLuminanceSource(bMap,
bMap.Width, bMap.Height);
BinaryBitmap bitmap = new BinaryBitmap(new
HybridBinarizer(source));

QRCodeReader dec = new QRCodeReader();
Hashtable hint = new Hashtable();
hint.Add(DecodeHintType.POSSIBLE_FORMATS,
BarcodeFormat.QR_CODE);
hint.Add(DecodeHintType.TRY_HARDER, true);

Result result = dec.decode(bitmap, hint);
...
You can see, I am using RGBLuminanceSource and HybridBinarizer for
converting source image into BinaryBitmap array.
Is here something wrong?

Thank's for answer.

Jiri Petras

Jiri Petras

unread,
Nov 28, 2011, 10:52:04 AM11/28/11
to zxing
Hi,

on http://www.petras-cz.eu/QR/Error.qr.png you can find example of not
detected QR code, if I'm not mistaken :-)

JP

Jiri Petras

unread,
Nov 28, 2011, 10:58:42 AM11/28/11
to zxing
Hi again...
I am using code from version 1.7 of ZXing distribution.
JP

On Nov 28, 5:01 am, Bas Vijfwinkel <bas5win...@gmail.com> wrote:

Steven Parkes

unread,
Nov 28, 2011, 10:59:25 AM11/28/11
to Jiri Petras, zxing
This decodes fine in both Java and C++ from the command line, i.e., if it doesn't decode on a device, it's probably code related to the device.

Jiri Petras

unread,
Nov 28, 2011, 2:02:04 PM11/28/11
to zxing
HI all

I confirm, using the above C# code and compiled ZXing (C#) library
(from distribution 1.7) for decoding the above published QRcode image
Exception of type 'com.google.zxing.ReaderException' was thrown
(QRCode Matrix is not detected).

Try it.

Jiri Petras


On Nov 28, 4:59 pm, Steven Parkes <smpar...@smparkes.net> wrote:
> This decodes fine in both Java and C++ from the command line, i.e., if it doesn't decode on a device, it's probably code related to the device.
>
> On Nov 28, 2011, at 7:52 AM, Jiri Petras wrote:
>
>
>
>
>
>
>
> > Hi,
>

> > onhttp://www.petras-cz.eu/QR/Error.qr.pngyou can find example of not

Steven Parkes

unread,
Nov 28, 2011, 2:06:26 PM11/28/11
to Jiri Petras, zxing
> I confirm, using the above C# code and compiled ZXing (C#) library
> (from distribution 1.7) for decoding the above published QRcode image
> Exception of type 'com.google.zxing.ReaderException' was thrown
> (QRCode Matrix is not detected).
>
> Try it.

Sorry. I lost track of the fact that you were using the C# port. Thought you were using Java.

So that means it's probably bit rot in the C# port: something's been fixed in Java and C++ that hasn't gotten ported to C#.

Unless there are other things I've lost track of as well ...

robob

unread,
Nov 29, 2011, 11:56:56 AM11/29/11
to zxing
Hi, I have the same problem on C# and 1.7. I compiled the 1.7 C#
version in Visual Studio 2010 and applied the patch you suggested
(flip i and j).

The patch (for me) does not completely solve the problem but decrease
the errors.

This is the code I use to encode/decode the QRCode in C#:

****************** ENCODE ***********************
QRCodeWriter writer = new QRCodeWriter();
com.google.zxing.common.ByteMatrix matrix;

Hashtable hints = new Hashtable();
hints.Add(EncodeHintType.ERROR_CORRECTION, correctionLevel);
matrix = writer.encode(textToEncode, BarcodeFormat.QR_CODE, sizex,
sizey, hints);

Bitmap img = new Bitmap(sizex, sizey);
Color Color = Color.FromArgb(0, 0, 0);

for (int y = 0; y < matrix.Height; ++y)
{
for (int x = 0; x < matrix.Width; ++x)
{
Color pixelColor = img.GetPixel(x, y);

//Find the colour of the dot
if (matrix.get_Renamed(x, y) == -1)
img.SetPixel(x, y, Color.White);
else
img.SetPixel(x, y, Color.Black);
}
}
return img;
*******************************************

****************** DECODE ***********************
LuminanceSource ls = new RGBLuminanceSource(image, image.Width,
image.Height);
Result result = new QRCodeReader().decode(new BinaryBitmap(new
HybridBinarizer(ls)));

return result.Text;
*******************************************

I noted that, in the encode function, if I decrease the sizex and
sizex (for example from 200 to 120) the erroneous QRCode disappears.

And now some question:

- Are my methods right?
- Is the patch you suggest working for you with any dimension?

Thanks a lot

Bas Vijfwinkel

unread,
Nov 29, 2011, 7:53:41 PM11/29/11
to zxing
Maybe there is something wrong with the C# Binarizer?
If you make the resolution 120 pixels, then it is 1 pixel per datadot.

The only method for digging into this is to feed the C# and java core
library the same data and compare to see where
the data differs. The data should essentially be the same during the
whole decoding process.
It is a very tedious and time consuming process but that might be the
only way to hunt down the bug....
Maybe check if java casts and C# casts work in the same way.
When creating the actionscript port, I noticed that java and
actionscript don't handle float->int casts in the same way.

robob

unread,
Dec 1, 2011, 1:21:28 AM12/1/11
to zxing
But do you (as zxing developer) not have a testbed to execute every
time you make a port? I could produce some QRCode as testbed to use
for future tests. Tell me if you need it.

Bas Vijfwinkel

unread,
Dec 2, 2011, 2:18:35 AM12/2/11
to zxing
I haven't set up an extensive testing case for creating barcodes yet.
For decoding I just throw the entire test folder at the decoders and
check if they produce the same result as the core library.
I'm in the middle of trying to create something for automated testing
of the encoders.
I don't know all internal details for each format so sometimes it is a
bit of a guess whether my test cases cover all
possible scenarios.
1D barcodes are pretty easy to test but especially QR codes have so
much internal dependencies that it would
be great if there was a list of data+settings that should be possible
to encode properly.
Currently I'm just taking a large number of samples from the test
folder and check if it generates a QR code that can be decoded again
with the ZXing library.

I don't know if the core library or perhaps some of the other ports
does have such a list for testing?

Reply all
Reply to author
Forward
0 new messages