标题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), fo