Data read from a generated QR Code do not match the original data

113 views
Skip to first unread message

Lorenzo

unread,
Oct 13, 2017, 11:10:52 AM10/13/17
to zxing
Hi everyone, I am experiencing some data mismatching, and I do not understand what I am missing. Can anyone point me to the right direction please?

I made two demo applications in C#: the first generates a QR Code which is saved as bmp and the second scans this saved bmp.

The first demo application generates a QR Code via "QRCodeGenerator.CreateQrCode" from a byte array named "dataStream". This byte stream (i.e. dataStream) is equal to { 254, 111, 2, 0, 128, 73, 28, 80, 1, 0, 2, 0, 89, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; and the generated QRCode is obtained by:

var barcode = new ZXingBarcodeImageView
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
WidthRequest = 300,
HeightRequest = 300,
Margin = 10,
};
barcode.BarcodeFormat = ZXing.BarcodeFormat.QR_CODE;
barcode.BarcodeOptions.Hints.Add(ZXing.EncodeHintType.CHARACTER_SET, "UTF-8");
barcode.BarcodeValue = Encoding.UTF8.GetString(dataStream, 0, dataStream.Length);

The second demo application scans this QR Code and produces a byte array which is compared to the one that generated such QR Code. This byte array is obtained by processing the obtained ZXing.Result named "dataQRCode" as such:
byte[] receivedDataStream = System.Text.Encoding. UTF8.GetBytes(dataQRCode.Text);

While I am expecting to obtain that receivedDataStream is equal to dataStream, I obtain that receivedDataStream is equal to {63, 11, 2, 0, 63, 73, 28, 86, 1, 0, 2, 0, 89, 1, 2, ...}.

I did some google and I found this google group and that the issue could be on the encoding part... However, I already specified that the encoding is "UTF-8" in both encoding and decoding.

Anyone knows what I am missing? Can you point me in the right direction, please?

I join the QR Code obtained by the first application.

All the best from the sunny Italy,
Lorenzo

2017-10-13 17_07_40-XDE.png

Sean Owen

unread,
Oct 14, 2017, 4:25:10 AM10/14/17
to zxing
QR codes encode text rather than bytes. I suspect you're not specifying an encoding here and the round trip from bytes to text to bytes is lossy. UTF-8 for example won't work for this purpose. ISO-8859-1 might.
BUt you're also asking here about code that is not in this project.

Lorenzo

unread,
Oct 16, 2017, 6:32:34 AM10/16/17
to zxing
Il giorno sabato 14 ottobre 2017 10:25:10 UTC+2, Sean Owen ha scritto:
> QR codes encode text rather than bytes. I suspect you're not specifying an encoding here and the round trip from bytes to text to bytes is lossy. UTF-8 for example won't work for this purpose. ISO-8859-1 might.
> BUt you're also asking here about code that is not in this project.

I thought the very same, and I tried the "ISO-8859-1" encoding without success : \.

Definitely, I believe that I am missing something; I attach here the archives of the application demos which are based on ZXing examples (i.e. XFGenBarcode and XFScan), and the updated QRCode. I hope to figure out what I am missing with your help.

Thanks in advance ;)

P.S. I pruned the code, so in order to dump the generated QRCode you should do a print screen, edit it via paint or similar, and save the bmp. Then, load the bmp or the attached one to the camera during emulation.

XFScan.zip
XFGenBarcode.zip
2017-10-16 10_58_06-XDE.bmp

Lorenzo

unread,
Oct 18, 2017, 4:18:08 AM10/18/17
to zxing
First of all, I introduced a bug while pruning the code for these demos. In fact, in XFGenBarcode was still using UTF-8 to encode the byte array. Fixed that the generated wr code was not recognized by the XFScan demo.

Fortunately, I found a this question on stack (https://stackoverflow.com/questions/11067723/encoding-and-decoding-byte-with-zxing). Following this idea, the issue seems resolved. Attached, the right QR Code containing { 254, 111, 2, 0, 128, 73, 28, 80, 1, 0, 2, 0, 89, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }.

To summarize: I setted the CHARACTER_SET to ISO-8859-1 in QR Code; set the QR Code text as the byte array encoded as base64; and, decoded the QR Code text as string from base64.

2017-10-18 09_07_23-XDE.bmp
Reply all
Reply to author
Forward
0 new messages