1
votes

I am trying to generate a custom QR code using the qrcode package, but I've run into an issue where it seems that it fails on valid input. I'm wondering if this is a bug in the package or expected behavior of a QR code that I'm not familiar with.

According to this site a version 1 QR code with Medium error correction can hold up to 20 alphanumeric values. Below myStr is a simple alphanumeric string that is 19 characters long and it fails with the output

qrcode.exceptions.DataOverflowError: Code length overflow. Data size (131) > size available (128)

However if I change myStr from 0000000000A0000A000 to 000000000000000A000, the string is still alphanumeric and still the same length, but then it passes.

import qrcode
qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_M,
)

myStr = '0000000000A0000A000'
print(len(myStr))
qr.add_data(myStr)
qr.make(fit=False)
img = qr.make_image()

Windows, Python 2.7, qrcode version 5.3, Pillow

2

2 Answers

1
votes

The error

qrcode.exceptions.DataOverflowError: Code length overflow. Data size (131) > size available (128)

is pointed to the buff which created by qrcode, not yours. In the qrcode/util.py:

print 'len(buffer):', len(buffer)
print 'buffer:', buffer
if len(buffer) > bit_limit:
    raise exceptions.DataOverflowError(
        "Code length overflow. Data size (%s) > size available (%s)" %
        (len(buffer), bit_limit))

The output about 0000000000A0000A000:

bit_limit: 128
len(buffer): 131
buffer: 16.40.0.0.0.0.64.20.17.1.0.0.32.33.194.0.0

The output about 000000000000000A000:

bit_limit: 128
len(buffer): 99
buffer: 16.60.0.0.0.0.0.0.32.33.194.0.0

You can use upper level version which has bigger bit_limit.

qr = qrcode.QRCode(
    version=2,      # use upper level version
    error_correction=qrcode.constants.ERROR_CORRECT_M,
)
> bit_limit: 224
0
votes

This does look like a bug in qrcode. I was able to repeat the issue on different OS, python versions, and qr versions. It seems that this specifically affects alphanumeric data. I've reported this as an issue on the qrcode github page.

For others who come across this, I found that if I set the optimize parameter in add_data to 0, then it passes. i.e.

qr.add_data(myStr, optimize=0)

I've tested the above by generating thousands of random alphanumeric strings of a specific length and generating QR codes for all of them. I've tested over different QR versions and error correction levels and I haven't had any failures.