直接上代码:
import cv2
from pyzbar.pyzbar import decodecap = cv2.VideoCapture(0) # 打开摄像头while True: # 循环读取摄像头帧ret, frame = cap.read()# 在循环中,将每一帧作为图像输入,使用pyzbar的decode()函数识别二维码barcodes = decode(frame)# 绘制二维码框和数据for barcode in barcodes:rect = barcode.rectx, y, w, h = rectcv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)data = barcode.data.decode("utf8")cv2.putText(frame, data, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)print("Result:" + data)# 显示结果 cv2.imshow('Image', frame)if cv2.waitKey(1) == ord('q'):breakcap.release()
cv2.destroyAllWindows()
简单使用的记录