? ? ? ? 使用pyzbar模塊來識別二維碼和條形碼。ZBar是一個開源軟件,用來從圖像中讀取條形碼,支持多種編碼,比如EAN-13/UPC-A、UPC-E、EAN-8、代碼128、代碼39、交錯2/5以及二維碼。
? ? ? ? pyzbar是python封裝ZBar的模塊,我們用它來做條形碼和二維碼的識別。
? ? ? ? 安裝方法:
????????
平臺 | 安裝方法 |
Windows | 使用pip安裝即可 pip install pyzbar |
Ubuntu | sudo apt-get install libzbar-dev pip install zbar 參考: ubuntu中安裝zbar_ubuntu 安裝libzbar依賴-CSDN博客 |
? ? ? ? python識別二維碼并繪制邊框和文字的代碼:
import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
from pyzbar.pyzbar import decode
#讀取二維碼圖像
img = cv.imread('../../SampleImages/QRCodes.jpg')
QRCodes = decode(img)
for QRCode in QRCodes:
print(QRCode)
stringData = QRCode.data.decode('utf-8')
print("二維碼字符串是:\"" + stringData + "\"")
#繪制出二維碼邊框
points = np.array([QRCode.polygon], np.int32)
#numpy reshape: https://blog.csdn.net/DocStorm/article/details/58593682
points = points.reshape((-1,1,2))
cv.polylines(img, [points], True, (0,255,0), 5)
rectPoints = QRCode.rect
cv.putText(img, stringData, (rectPoints[0], rectPoints[1]), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
plt.imshow(img[:,:,::-1])
運(yùn)行結(jié)果(最后的二維碼是殘缺的,因此未識別):?
? ? ? ? 識別條形碼的代碼和識別二維碼的代碼是一樣的:
#讀取條形碼圖像
img = cv.imread('../../SampleImages/BARCodes.png')
BARCodes = decode(img)
for BARCode in BARCodes:
print(QRCode)
stringData = BARCode.data.decode('utf-8')
print("條形碼字符串是:\"" + stringData + "\"")
#繪制出二維碼邊框
points = np.array([BARCode.polygon], np.int32)
#numpy reshape: https://blog.csdn.net/DocStorm/article/details/58593682
points = points.reshape((-1,1,2))
cv.polylines(img, [points], True, (0,255,0), 5)
rectPoints = BARCode.rect
cv.putText(img, stringData, (rectPoints[0] - 20, rectPoints[1] - 5), cv.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
plt.imshow(img[:,:,::-1])
? ? ? ? ?識別結(jié)果:文章來源:http://www.zghlxwxcb.cn/news/detail-761284.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-761284.html
到了這里,關(guān)于Python Opencv實(shí)踐 - 二維碼和條形碼識別的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!