heuer / segno

Python QR Code and Micro QR Code encoder
https://pypi.org/project/segno/
BSD 3-Clause "New" or "Revised" License
631 stars 54 forks source link

Problem with micro QR code generation #33

Closed nboullis closed 7 years ago

nboullis commented 7 years ago

Hi,

I was trying to generate some micro QR codes that should be read by someone else, but they reported that they could not read them. After some investigation, it seems that the code for generating format information for micro QR codes is wrong, except for M1 and M2-L code.

The patch below seems to fix that problem, although the people who read my codes still report they can’t read the M3 codes I generated (M2 and M4 seemed to be fine)…

Cheers,

-- Nicolas Boullis

diff --git a/segno/encoder.py b/segno/encoder.py
--- a/segno/encoder.py
+++ b/segno/encoder.py
@@ -786,12 +786,15 @@ def add_format_info(matrix, version, err
     is_micro = version < 1
     offset = int(is_micro)
     fmt = mask_pattern
-    if error == consts.ERROR_LEVEL_L:
-        fmt += (0x08 if not is_micro else 0x4)
-    elif error == consts.ERROR_LEVEL_H:
-        fmt += (0x10 if not is_micro else 0xe)
-    elif error == consts.ERROR_LEVEL_Q:
-        fmt += (0x18 if not is_micro else 0x14)
+    if is_micro:
+        fmt += consts.ERROR_LEVEL_TO_MICRO_MAPPING[version][error] << 2
+    else:
+        if error == consts.ERROR_LEVEL_L:
+            fmt += 0x08
+        elif error == consts.ERROR_LEVEL_H:
+            fmt += 0x10
+        elif error == consts.ERROR_LEVEL_Q:
+            fmt += 0x18
     format_info = consts.FORMAT_INFO[fmt] if not is_micro else consts.FORMAT_INFO_MICRO[fmt]
     for i in range(8):
         bit = (format_info >> i) & 0x01
heuer commented 7 years ago

Thanks for your bug report and fix. Do you have examples of M3 codes which fail with Segno and how they should look like? It's difficult to find and test Micro QR codes since most libs support QR Codes, only. If you have examples of correct M3 codes, please provide the input string, the error correction level and mask pattern.

heuer commented 7 years ago

Can you read these files? Content should be "XY"

test-m3-l test-m3-m

heuer commented 7 years ago

Additionally: Which decoder do you use?

nboullis commented 7 years ago

I wish I could help you. Unfortunately, I haven’t yet found anything to read micro QR codes myself… As far as I can tell, even DENSO WAVE’s “QRdeCODE” application for iOS does not seem to read micro QR codes… :-( I have to generate a document with some (micro) QR codes that will be read in a different company. They reported they could not read the micro QR codes I generated, but they did not tell me what decoder they are using… I will ask them, but I am not sure they will tell…

nboullis commented 7 years ago

For what it’s worth, they just replied they are using two proprietary (and IMHO quite expensive) libraries to decode micro QR codes:

It also seems that the free software Zint can generate micro QR code. I haven’t yet given it a try, but it might be useful to compare the micro QR codes it generates with the ones generated with Segno.

heuer commented 7 years ago

Thanks for your feedback, I'll have a look.

nboullis commented 7 years ago

Hi,

For what it’s worth, I just tried both your “XY” codes with the “Trial” app from the first library. It seems to read both fine.

$ ./demo_so.out image1.bmp
Barcodes count: 1

Barcode 1
Dim: 15  Version:  3  Level: L
Data: X*

Param Quality:
SC    =   66,67 (B)
AxNU  =    0,00 (A)
GrNU  =    0,38 (A)
UEC   =    1,00 (A)
FPD   = (A)
ModG  = (A)
DecG  = (A)
FrmG  = (A)
VerG  = (A)
ScanG = (B)
$ demo_so.out image2.bmp 
Barcodes count: 1

Barcode 1
Dim: 15  Version:  3  Level: M
Data: X*

Param Quality:
SC    =   66,67 (B)
AxNU  =    0,00 (A)
GrNU  =    0,38 (A)
UEC   =    1,00 (A)
FPD   = (A)
ModG  = (A)
DecG  = (A)
FrmG  = (A)
VerG  = (A)
ScanG = (B)

And here is a set of micro QR codes I generated. All 4 M2 codes seem to read fine, while all 4 M3 codes do not read. The version, encoding and content is written besides each code.

codes

heuer commented 7 years ago

Thanks a lot for your feedback. I'll try to fix it next week.

heuer commented 7 years ago

Dear Nicolas,

I don't have access to a Windows or Linux machine (maybe end of this week). If you don't mind, check out the issue-33 branch. I hope it solves the "M3" issue. The M2 test against the example in the ISO standard works with the modifications in the above mentioned branch.

https://github.com/heuer/segno/tree/feature/issue-33

Best regards, Lars

heuer commented 7 years ago

Meant https://github.com/heuer/segno/commit/7420384cd8953fadae584198805b15b9adb94636

The branch contains some more modifications which are not merged into master yet

nboullis commented 7 years ago

Hi Lars,

I just did a quick test, trying to re-generate the 8 codes from my example above, and trying to read them back with the “Trial” app. As far as I can see, all 8 micro QR codes are different from the ones I generated previously, and the decoding app an only decode the second and third ones. The other 6 ones fail to be decoded. Comparing the first code (M2-L with “12345” encoded in “numeric” mode), the only difference is in the format information zone, so I guess something must be wrong in your patch…

Sorry, I don’t have time right now to investigate further.

Best regards,

-- Nicolas

Edit: I investigated a little more. It seems that the bits 0 to 8 of the format information are swapped. Changing this, I can read the 4 M2 codes, but still fail to read the 4 M3 codes.

heuer commented 7 years ago

Thanks for your answer. Micro QR Codes tests suck. I'd love to provide some tests but it's very difficult. I'll try to investigate harder even if it's difficult to find verified (Micro) QR Codes. The ISO standard does not give many examples and all ISO examples are verified against Segno (the ISO standard lacks of M3 examples, though)

heuer commented 7 years ago

Short update: I found a problem: M1 and M3 codes may generate an invalid (too large) data module buffer. I think that causes the invalid codes. I hope to solve this issue soon. btw, I found a Micro QR code decoder online http://demo.leadtools.com/JavaScript/Barcode/index.html

heuer commented 7 years ago

Hi Nicolas,

the "develop" branch should create valid M2, M3, and M4 codes acc. to the updated test suite. M1 does not work yet, though.

Regards, Lars

heuer commented 7 years ago

Close for format information and M3, see #37 for updates of M1 codes

heuer commented 7 years ago

Thanks for your bug report, Nicolas. I discovered a lot of more problems with Micro QR Codes, especially with M1 and M3. They are solved now. I'll make a release soon