29 lines
824 B
Python
29 lines
824 B
Python
|
import cv2
|
||
|
from pyzbar import pyzbar
|
||
|
|
||
|
def read_qrcode(image_path):
|
||
|
# 读取图像
|
||
|
img = cv2.imread(image_path)
|
||
|
|
||
|
if img is None:
|
||
|
print("无法加载图像,请检查文件路径是否正确。")
|
||
|
return
|
||
|
|
||
|
# 图像预处理:转换为灰度图并进行二值化处理
|
||
|
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||
|
_, binary_img = cv2.threshold(gray_img, 128, 255, cv2.THRESH_BINARY)
|
||
|
|
||
|
# 识别二维码
|
||
|
barcodes = pyzbar.decode(binary_img)
|
||
|
|
||
|
# 输出结果
|
||
|
if not barcodes:
|
||
|
print("未检测到二维码。")
|
||
|
else:
|
||
|
for barcode in barcodes:
|
||
|
barcode_data = barcode.data.decode("utf-8")
|
||
|
barcode_type = barcode.type
|
||
|
print(f"发现 {barcode_type} 二维码: {barcode_data}")
|
||
|
|
||
|
# 使用示例
|
||
|
read_qrcode("image.png")
|