from pyzbar import pyzbar
import cv2
import numpy as np
def image_detect(img):
#QRdetecter = cv2.QRCodeDetector()
barcodes = pyzbar.decode(img)
print('识别结果为:',barcodes)
for barcode in barcodes:# 循环读取检测到的条形码
# 绘条形码、二维码多边形轮廓
points =[]
for point in barcode.polygon:
points.append([point[0], point[1]])
points = np.array(points,dtype=np.int32).reshape(-1,1, 2)
cv2.polylines(img, [points], isClosed=True, color=(0,0,255),thickness=2)
# 条形码数据为字节对象,所以如果我们想把它画出来
# 需要先把它转换成字符串
barcodeData = barcode.data.decode("UTF-8") #先解码成字符串
barcodeType = barcode.type
# 绘出图像上的条形码数据和类型
text = "({}): {} ".format(barcodeType, barcodeData )
#print('结果:',text)
#cv2.putText(img, text, (x, y - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("QR", img)
cv2.imwrite('QR.jpg',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
#img0 = cv2.imread("bar.jpg")
img0 = cv2.imread("test.png")
image_detect(img0)
测试结果: