Files
SaltBot/Plugin/nonebot_plugin_maimai_helper/helper/qrcode.py
2025-03-04 20:48:27 +08:00

45 lines
1.2 KiB
Python

import cv2
import logging
import sys
sys.setrecursionlimit(5000)
def decode_qr_code(image_path: str) -> str:
"""
使用 OpenCV 识别二维码并返回内容
:param image_path: 输入图片的路径
:return: 二维码的内容
"""
try:
# 读取图片
image = cv2.imread(image_path)
if image is None:
print(f"无法读取图片: {image_path}")
return None
# 创建 QRCodeDetector 对象
qr_code_detector = cv2.QRCodeDetector()
# 解码二维码
data, points, straight_qrcode = qr_code_detector.detectAndDecode(image)
if data:
print(f"二维码内容: {data}")
return data
else:
print("未找到二维码")
return None
except Exception as e:
print(f"解码二维码时发生错误: {e}")
return None
if __name__ == "__main__":
# 输入图片路径
image_path = input("请输入图片的路径: ")
# 识别二维码
qr_code_content = decode_qr_code(image_path)
if qr_code_content:
print(f"二维码内容: {qr_code_content}")
else:
print("无法识别二维码")