QR code and analysis

When it comes to the QR code, everyone must be familiar with it. It can be said that the QR code has almost penetrated into every corner of our lives. For example, when we go to the supermarket and shop to scan the QR code to pay, we take the bus to scan the subway. When the QR code enters the station, we scan the QR code when picking up at the rookie post. If we meet an interesting person one day, we may also scan the QR code to add a friend on WeChat. If you read our article, you feel right. I have a little help myself. I can also scan the QR code at the end of the article to follow our official account.

The libraries we need to generate and identify QR codes through Python are: qrcode, myqr, zxing, and install through pip install qrcode / myqr / zxing.

QR code structure

First, let's briefly understand the structure of the QR code, as shown in the following figure:

From the figure, we can see that the structure of the two-dimensional code is divided into two parts: the functional graphic and the coding area. The functional graphic is further subdivided into: blank area, position detection graphic, position detection graphic separator, positioning graphic, correction graphic, and The coding area is subdivided into: format information, version information, data and error correction codewords, to briefly understand the function of each part:

  • Blank area: leave blank, no need to do anything
  • Position detection graphics: assist the scanning software to locate the QR code
  • Position detection graphic separator: distinguish between functional graphics and coding area
  • Positioning graphics: indicating the density of the mark and determining the coordinate system
  • Correction graphics: the number and location of correction graphics
  • Format information: information for storing formatted data
  • Version information: the specifications of the QR code, the QR code symbol has a matrix of 40 specifications
  • Data and error correction code words: the actual saved QR code information and error correction code words (used to correct errors caused by the damage of the QR code)

Generate QR code

We can use qrcode and myqr to create two-dimensional codes. Let's take a closer look.

qrcode

In life, we may see some QR codes. After we scan, we jump to an address. Such a function qrcode can be realized. Let's look at an example:

import qrcode

# 二维码内容(链接地址或文字)
data = 'https://www.baidu.com/'
# 生成二维码
img = qrcode.make(data=data)
# 显示二维码
img.show()
# 保存二维码
# img.save('qr.jpg')

Look at the effect:

We can jump to the address we set by scanning the QR code above.

The above QR code is relatively primitive, we can also set it up and beautify it simply. The code implementation is as follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import qrcode

'''
version:二维码的格子矩阵大小,可以是 1 到 40,1 最小为 21*21,40 是 177*177
error_correction:二维码错误容许率,默认 ERROR_CORRECT_M,容许小于 15% 的错误率
box_size:二维码每个小格子包含的像素数量
border:二维码到图片边框的小格子数,默认值为 4
'''
qr = qrcode.QRCode(
    version=2,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=15,
    border=3,
)
# 二维码内容
data = 'https://www.taobao.com/'
qr.add_data(data=data)
# 启用二维码颜色设置
qr.make(fit=True)
img = qr.make_image(fill_color='blue', back_color='white')
# 显示二维码
img.show()

 

myqr

If we are not satisfied with the style of the QR code generated above, we need to use the myqr library, which can not only generate ordinary QR codes, but also generate QR codes with pictures and dynamic QR codes, but we want When viewing the generated QR code, you need to go to the save location and open it manually.

Ordinary QR code

Let's first use myqr to generate an ordinary QR code, look at the example:

from MyQR import myqr

'''
words:内容
    version:容错率
save_name:保存的名字
'''
myqr.run(words='https://www.baidu.com/',
         version=1,
        save_name='myqr.png')    #保存地址

Look at the effect:

QR code with picture

We then use myqr to generate a QR code with a graph, take a look at the example:

from MyQR import myqr

'''
picture:生成二维码用到的图片
colorized:False 为黑白,True 为彩色
'''
myqr.run(words='https://www.baidu.com/',
         version=1,
         picture='bg.jpg',
         colorized=True,
         save_name='pmyqr.png')

Look at the effect:

 

 

Dynamic QR code

Finally, we use myqr to generate a dynamic QR code, look at the example:

from MyQR import myqr

myqr.run(words='https://www.baidu.com/',
         version=1,
         picture='my.gif',
         colorized=True,
         save_name='myqr.gif')

Look at the effect:

 

Parsing the QR code

We use the zxing library to identify the QR code, and it is relatively simple to use. Let's take a look at the following example:

import zxing

reader = zxing.BarCodeReader()
barcode = reader.decode('myqr.gif')
print(barcode.parsed)

 

Of course, we can also beautify the existing QR code, the implementation is:

  • Parse existing QR code content
  • Use the parsed content to generate a new QR code

Take a public account as an example, as shown in the following figure:

 

Take a look at the implementation code:

import zxing
from MyQR import myqr

reader = zxing.BarCodeReader()
barcode = reader.decode('gzh.jpg')
myqr.run(words=str(barcode.parsed),
         version=1,
         picture='my.gif',
         colorized=True,
     save_name='gmyqr.gif')

Look at the effect:

 

 

 

Published 6 original articles · received 1 · views 655

Guess you like

Origin blog.csdn.net/DY1316434466/article/details/105439960