首先需要安装pyzbar和opencv
pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ opencv-python pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ pyzbar
这里要注意,pyzbar可能安装上依赖的dll不存在会报错,需要去下载安装c++相关dll
地址为:
https://www.microsoft.com/en-US/download/details.aspx?id=40784
上代码
from cv2 import cv2 from pyzbar import pyzbar #wanmait.com def codedemo(): camera = cv2.VideoCapture(0) cv2.namedWindow('wanmait') cv2.namedWindow('small') while True: ret, frame = camera.read() if ret: #二维码解析的信息 barcodes = pyzbar.decode(frame) for barcode in barcodes: print(f"万码学堂读取的二维码信息:{barcode.data.decode('utf-8')}") x,y,w,h = barcode.rect cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) #截取图片中的二维码部分 img = frame[y:y+h, x:x+w] #保存二维码 cv2.imwrite('d:/photos/code.jpg',img) #把二维码单独显示出来 cv2.imshow('small', img) cv2.imshow('wanmait', frame) #cv2.imwrite() if cv2.waitKey(100) == 27: break camera.release() cv2.destroyAllWindows() if __name__=='__main__': codedemo()
运行如图:
0条评论
点击登录参与评论