In depth understanding of QR code generation size

In depth understanding of QR code generation size

To learn more about the principle of QR code, CSDN's two blogs are good:

From MachineChen's blog:

Transferred from Qi xiaoshrimp's blog:

I understand the generated dimension of QR code and verify the following:

#Generate binary code
def create_qr_code(url, icon_file, file_name, save_path, ec='H'):
    ecn = 2
    if ec == "L":
        ecn = qrcode.ERROR_CORRECT_L
    if ec == 'M':
        ecn = qrcode.ERROR_CORRECT_M
    if ec == 'Q':
        ecn = qrcode.ERROR_CORRECT_Q
    if ec == 'H':
        ecn = qrcode.ERROR_CORRECT_H
    version = 6
    box_size = 5
    border = 1
    qr = qrcode.QRCode(
        # Set the fault tolerance rate to the highest
    qr_size = (21 + (version - 1) * 4 + border * 2) * box_size

The size of QR code is related to these parameters: version, border and box_size, the specific calculation formula is as follows:

qr_size = (21 + (version - 1) * 4 + border * 2) * box_size

For example, the above code: qr_size = (21 + (6 - 1) * 4 + 1* 2) * 5 = 215

However, the calculation formula of this is based on one parameter:

Of course, I use qrcode

import qrcode
:param fit: If ``True`` (or if a size has not been provided), find the
            best fit for the data to avoid data overflow errors.

When fit is True, the version will be automatically matched according to the data filled by the QR code to avoid data loss.

Of course, the program code judges this place. When fit is False and the filled data is greater than the version specified data, a dataoverflow error exception will be thrown:

Traceback (most recent call last):
  File "D:/thecover_project/cover_app_platform/app/test/", line 118, in <module>124
    create_qr_code(content, icon_path, 'cs', save_path, 'H')
  File "D:/thecover_project/cover_app_platform/app/test/", line 63, in create_qr_code
  File "C:\Users\DELL\PycharmProjects\cover_app_platform\venv\lib\site-packages\qrcode\", line 95, in make
    self.makeImpl(False, self.best_mask_pattern())
  File "C:\Users\DELL\PycharmProjects\cover_app_platform\venv\lib\site-packages\qrcode\", line 181, in best_mask_pattern
    self.makeImpl(True, i)
  File "C:\Users\DELL\PycharmProjects\cover_app_platform\venv\lib\site-packages\qrcode\", line 122, in makeImpl
    self.data_cache = util.create_data(
  File "C:\Users\DELL\PycharmProjects\cover_app_platform\venv\lib\site-packages\qrcode\", line 573, in create_data
    raise exceptions.DataOverflowError(
qrcode.exceptions.DataOverflowError: Code length overflow. Data size (1004) > size available (368)

The program calculation length is determined according to the Number of data bits in the standard. If we judge ourselves, we can use the Byte in Data capacity for reference.

The version ranges from 1 to 40. There are fault tolerance levels corresponding to LMQH. It is allowed to fill in the value corresponding to the data length. What is LMQH

When the error level is set to L, the QR code can still scan the result when it is blurred or incomplete by 7%, and H is 30%.

The higher the fault tolerance level, the better, but the less data will be filled, and the size of the QR code will become larger.

ISOIEC 18004_2006Cor 1_2009 (QR code ISO international standard):

When version is 9, the number of 8-bit bytes L is 230, M is 180, Q is 130, and H is 98. I calculate here according to the standard corresponding to Byte.

In other words, if my error correction level is H, my fill content cannot exceed 98 8-bit bytes. For example, if I want to fill 99 jump URLs, the version will automatically match 10.

So when you generate a QR code, fit must be set to True, so you don't have to calculate the length of the data you want to fill to match the version.

Another point is that when someone looks through the qrcode source code like me, they are a little confused:

constants. Why is the LMQH level maintained in py 1,0,3,2 instead of 0,1,2,3? At first, I was confused. I was also wrong about the source code. I also planned to modify this place. After many verifications, I found that it was right,

After studying the document, I found that this is actually a decimal system converted from binary. Ha ha:

Error correction level code:

When the data you want to fill in is less than the corresponding allowable value, the size calculation formula can be directly brought into the parameter calculation. When your fit is True and the filled data is greater than the corresponding value of version, the calculated size will be matched automatically,

At this time, the size of QR code is not what you expected before. Those interested can verify themselves.

How is it realized that some websites that generate QR codes have selected sizes? For example, the Internet: :

In fact, you only need to confirm the value of the data to be filled in advance, and then calculate the box dynamically_ Size is OK:

content = "itms-se://?action=download-manifest&url="
icon_path = '/Users/drew/.jenkins/workspace/Android-xx/app/src/main/res/mipmap-xxhdpi/ic_collect_icon.png'
save_path = '/Users/Work/PycharmProjects/FM_Build_Packaging/resources/img/qrcode'
create_qr_code(content, icon_path, 'cs', save_path, 'H')

1. As mentioned above, it is calculated that the content length is 118 (the maximum possible length can be estimated here), the fault tolerance level is H, and version 10 is just enough. Of course, you can set a larger version, so that fit can be set to False.

The size I want to generate is 1500px. Bring it into the formula to find box_size():

version = 10
box_size = ?
border = 1
size = 1500
1500 = (21 + (10- 1) * 4 + 1* 2) * box_size, calculate the box_size = 25.42.

When a QR code is generated, box_ Set the size to 25.42! But box_ The value of size is int:

Therefore, the calculation will not be just appropriate:

# QR code size and version information corresponding to version = 12 and border = 1
# qr_size = {'6': 402, '9': 603, '12': 804, '15': 1005, '18': 1206, '23': 1541, '30': 2010, '38': 2546, '45': 3015}

The above is the QR code size calculated when version = 12 and border = 1. What should I do if I want to generate a 1500 size?

img = img.resize((1500, 1500), Image.ANTIALIAS)

Use the resize method of PIL to fine tune it and pull it up to the required proportion: if calculated according to the formula, it will generally be smaller when it is int, so the resize here will generally be pulled up and will not be compressed, but the clarity of the generated QR code can be guaranteed. It is very clear after trying various sizes.

Above, my humble opinion on the size of QR code generated by python's qrcode.

Attach the version and capacity table of QR Code:

If you need 125 pages of QR code ISO international standard pdf document, you can leave a message.

Keywords: Python

Added by CoB-Himself on Sun, 19 Dec 2021 12:20:32 +0200