?? 作者:韓信子@ShowMeAI
?? Python3?技能提升系列:https://www.showmeai.tech/tutorials/56
?? 本文地址:https://showmeai.tech/article-detail/398
?? 宣告:著作權所有,轉載請聯系平臺與作者并注明出處
?? 收藏ShowMeAI查看更多精彩內容
二維碼用某種特定的幾何圖形來記錄資料符號資訊,這些黑白相間的圖形按照一定的規律分布在平面上(二維方向),二維碼是目前最常使用的快捷資訊存盤方式之一,微信等都可以通過這項技術實作快掃快用,
在本篇內容中,ShowMeAI帶大家來學習二維碼的應用技能,包括構建二維碼和解碼二維碼,
?? 二維碼歷史
QR(Quick Response,快速回應)Code 誕生于 1994 年的日本汽車公司 Denso Wave,是一種二維條形碼,由在白色背景上排列成方形網格的黑色方塊組成,允許立即訪問隱藏在代碼中的資訊,
QR碼(也就是我們常說的二維碼)可存盤 7000 多個字符,由相機等設備讀取,并從像素影像中決議出包含的資訊,讀取速度比其他條碼快得多,
?? 二維碼應用場景
生成和讀取二維碼的簡便性導致它們在零售店、銀行、醫院、旅游和食品服務行業的產品包裝、非接觸式商務、訂單處理、結帳和支付服務中得到廣泛采用,我們常用到通信軟體、社交平臺都幾乎都可以通過二維碼來掃碼識別,
2020 年 9 月對美國和英國消費者進行的一項調查發現,在COVID-19大流行期間二維碼的使用有所增加,
?? 生成二維碼
我們先安裝和匯入本次需要用到的 Python 工具庫qrcode,它可以很方便地創建和讀取二維碼,
import qrcode
創建資料,
data="https://www.showmeai.tech"
創建二維碼實體,
qr= qrcode.QRCode(version=1, box_size=10, border=4, error_correction=qrcode.constants.ERROR_CORRECT_H)
我們對引數做一個解釋:
version引數是一個從 1 到 40 的整數,控制二維碼的大小;最小的是版本 1,它是一個 21x21 矩陣,box_size引數控制二維碼每個方框的像素數,border控制框邊框的粗細,error_correction控制用于 QR 碼的糾錯,特別是當 QR 碼因錯誤而無法讀取時,選項error_correction包括:ERROR_CORRECT_L:可以糾正大約 7% 或更少的錯誤ERROR_CORRECT_M(默認):可以糾正大約 15% 或更少的錯誤,ERROR_CORRECT_Q:可以糾正大約 25% 或更少的錯誤,ERROR_CORRECT_H:可以糾正大約 30% 或更少的錯誤,
qr.add_data(data)
qr.make(fit=True)
最后,使用生成二維碼make_image()將 QRCode 物件轉換為影像檔案并保存在檔案中,
qr_img=qr.make_image(fill_color="black", back_color="white")
qr_img.save("qr.jpg")
其中,fill_color和back_color可以改變二維碼的背景和繪畫顏色,
?? 閱讀二維碼
本篇我們將講解兩種不同的方式來讀取二維碼,使用cv2和pyzbar,
?? opencv 讀取解碼
匯入庫,
import cv2
打開上方存盤的qr.jpg影像檔案,
cv_img= cv2.imread("qr.jpg")
在 CV2 中創建類 QRCodeDetector 的物件,
qr_detect= cv2.QRCodeDetector()
data, bbox, st_qrcode= qr_detect.detectAndDecode(cv_img)
detectAndDecode()檢測并解碼影像中存在的二維碼,該方法回傳以下內容:
- 解碼后的資料,如果沒有找到二維碼,則資料為空,
- 包含檢測到的二維碼頂點的邊界框,
- 可選的包含經過校正和二值化的 QR 碼的輸出影像,
print(f"QRCode data:\n{data}")
?? pyzbar 讀取解碼
使用 cv2 讀取影像,
import cv2
from pyzbar.pyzbar import decode
# read the image using cv2
img = cv2.imread("qr.jpg")
接下來,找到影像中的條形碼和二維碼,
# Decode the barcode and QR Code in the image
detectedBarcodes = decode(img)
decode會遍歷影像中所有檢測到的條形碼,回傳結果陣列的每個元素代表一個檢測到的條形碼,可以讀取影像中的多個條形碼或 QR 碼,
每個檢測到的條碼包含以下資訊:
data:條形碼/二維碼中嵌入的資料,type:它是條碼型別,如 QR Code、EAN-13、UPC-A、UPC-E、EAN-8、Code 128 和 Code 39 符號體系,rect:定位框的邊界點集合,對于QR碼,它是對應QR碼四邊形的四個角的四個點的串列,polygon:檢索位置多邊形中的點數,位置多邊形定義影像中條形碼被解碼的區域,quality:質量,orientation:表示條碼的方向,
# read the image in numpy array using cv2
img = cv2.imread("qr.jpg")# Decode the barcode image
detectedBarcodes = decode(img)# If barcode is not detected then print the message
if not detectedBarcodes:
print("Bar code not detected or your barcode is blank or corrupted!")
else:# Iterate through all the detected barcodes in image
for bar_code in detectedBarcodes:# Locate the barcode position in image using rect
(x, y, w, h) = bar_code.rect# Highlight the rectanngela round the bar code
cv2.rectangle(img, (x-10, y-10),
(x + w+10, y + h+10),
(255, 0, 0), 2)if bar_code.data!="":# Print the barcode data
print(f"Data : {bar_code.data.decode('UTF-8')}")
print(f"Bar Code Type: {bar_code.type}")
print(f"Bar Code Orientation: {bar_code.orientation}")
參考資料
- ?? During the last six months, in which of these locations or instances have you scanned a QR code?
- ?? 圖解Python編程:從入門到精通系列教程:ttps://www.showmeai.tech/tutorials/56
- ?? 編程語言速查表 | Python3 速查表:https://www.showmeai.tech/article-detail/98
推薦閱讀
- ?? 資料分析實戰系列 :https://www.showmeai.tech/tutorials/40
- ?? 機器學習資料分析實戰系列:https://www.showmeai.tech/tutorials/41
- ?? 深度學習資料分析實戰系列:https://www.showmeai.tech/tutorials/42
- ?? TensorFlow資料分析實戰系列:https://www.showmeai.tech/tutorials/43
- ?? PyTorch資料分析實戰系列:https://www.showmeai.tech/tutorials/44
- ?? NLP實戰資料分析實戰系列:https://www.showmeai.tech/tutorials/45
- ?? CV實戰資料分析實戰系列:https://www.showmeai.tech/tutorials/46
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/538922.html
標籤:其他
