import qrcode
import cv2
import pyautogui
# 打開攝像頭
cap = cv2.VideoCapture(0)
while True:
# 讀取攝像頭圖像
ret, frame = cap.read()
# 轉(zhuǎn)換為灰度圖像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 二值化圖像
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# 查找輪廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 遍歷所有輪廓
for cnt in contours:
# 計(jì)算輪廓矩形框
x, y, w, h = cv2.boundingRect(cnt)
# 判斷是否為有效的二維碼
if w / h > 2 and w / h < 5:
# 在圖像上繪制矩形框
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 提取二維碼圖像
qr_img = gray[y:y + h, x:x + w]
# 解碼二維碼
data = qrcode.make(qr_img)
decoder = qrcode.Decoder()
result = decoder.decode(data)
# 在圖像上繪制解碼結(jié)果
cv2.putText(frame, result, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# 顯示圖像文章來源地址http://www.zghlxwxcb.cn/news/detail-563933.html
文章來源:http://www.zghlxwxcb.cn/news/detail-563933.html
到了這里,關(guān)于python 寫個(gè)手機(jī)掃描二維碼服務(wù)的小程序的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!