生成二维码
https://github.com/lincolnloop/python-qrcode
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('信息')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("img.png")
读取二维码:opencv
# -*-coding:utf-8 -*-
import cv2 as cv
import numpy as np
src_image = cv.imread("./img.png")
qrcode = cv.QRCodeDetector()
# qr检测并解码
msg, points, straight_qrcode = qrcode.detectAndDecode(src_image)
# 绘制qr检测到的边框
cv.drawContours(src_image, [np.int32(points)], 0, (0, 0, 255), 2)
print("points", points)
# 打印解码结果
print("qrcode :", msg)
cv.imshow("result", src_image)
cv.waitKey(0)
- 对拍照之类的都能准确定位边框
- 某些系统产出的二维码无法提取信息(但边框检测还是可以准确定位)