标题python3下使用pyzbar读取条码
使用的mac系统。
第一步:先安装zbar的共享库,使用brew安装,安装时经常会出错,多试几次,到全部库文件都安装好。
brew install zbar
第二步,安装pyzbar
python -m pip install pyzbar
安装好后就可以使用了,写个用摄像头读取条码都测试程序,使用了opencv调用摄像头以及把识别结果叠加在图像上。
# coding=utf-8
import cv2
import pyzbar.pyzbar as pyzbar
def decode(image): #解码
barcodes = pyzbar.decode(image)
for barcode in barcodes:
# 提取并绘制图像中条形码的边界框
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# 绘出图像上条形码的数据和条形码类型
text = "Text:" + barcodeData
text2 = "Type:" + barcodeType
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,text,(30,30), font, 0.8,(0,255,0),2)
cv2.putText(image,text2,(30,80), font, 0.8,(0,255,0),2)
return image
def camera():
camera = cv2.VideoCapture(0)
while True:
# 读取当前帧
ret, img = camera.read()
img=decode(img)
img = cv2.resize(img,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_CUBIC)
cv2.imshow("camera", img)
#按q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
camera.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
camera()
原文链接:https://blog.csdn.net/weixin_43585672/article/details/106889469