pyzbar 识别QR二维码
from PIL import Image
from pyzbar.pyzbar import decode
# 打开图像文件
image_path = 'qr01.jpg' # 替换为你的图像路径
image = Image.open(image_path)
# 解码图像中的二维码
decoded_objects = decode(image)
# 输出识别结果
for obj in decoded_objects:
print("二维码类型:", obj.type)
print("二维码数据:", obj.data.decode("utf-8"))
opencv识别识别QR二维码 opencv-contrib-python==4.5.5.62
import cv2
# 图像路径
image_path = 'qr01.jpg'
# 检查图像是否存在
if not cv2.haveImageReader(image_path):
print(f"错误: 图像文件 '{image_path}' 不存在或无法读取,请检查路径和文件名。")
else:
# 读取图像
img = cv2.imread(image_path)
# 检查图像是否成功加载
if img is None:
print("错误: 图像加载失败,请检查文件格式或路径。")
else:
# 创建 QRCodeDetector 对象
qr_decoder = cv2.QRCodeDetector()
# 检测并解码二维码
data, bbox, _ = qr_decoder.detectAndDecode(img)
# 输出识别结果
if data:
print("二维码数据:", data)
else:
print("未检测到二维码。请确保图像是清晰的,并包含有效的二维码。")