?前言
? ? ? ? 關(guān)于動態(tài)識別二維碼信息,利用電腦攝像頭動態(tài)掃描二維碼,掃描視頻中的二維碼。
簡易程序
import cv2 pip install opencv-python
def start():
capture = cv2.VideoCapture(0)
while True:
fet, frame = capture.read()
qrCodeDetector = cv2.QRCodeDetector()
data, bbox, straight_qrcode = qrCodeDetector.detectAndDecode(frame)
print(data) # data即讀取到的數(shù)據(jù)
cv2.namedWindow('read-QR', 0)
cv2.imshow("read-QR", frame)
k = cv2.waitKey(1) # 保持畫面的持續(xù)。
if k == 27: # 通過esc鍵退出攝像
break
start()
加強(qiáng)版Python GUI(圖像用戶界面編程)
????????由于我在項(xiàng)目中設(shè)計(jì)了一個PC端執(zhí)行軟件,所以用到了GUI編程,即圖像用戶界面編程,所以把關(guān)于這部分的GUI編程取了出來寫在了下面。實(shí)際上用上面這個程序已經(jīng)可以識別二維碼了,下面這個程序是將程序由基于控制臺來控制執(zhí)行變成了圖形用戶界面來控制。將這個程序打包成軟件可以用pip install auto-py-to-exe,這個庫就是一個圖像用戶界面的程序,相比別的方式用起來比較友好,具體可以網(wǎng)上搜索學(xué)習(xí)。
import cv2
import tkinter
from tkinter import *
from PIL import Image, ImageTk
from tkinter import messagebox
import numpy as np
capture = cv2.VideoCapture(0)
root = tkinter.Tk()
root.title("read-QR")
root.geometry('900x600')
img_width = 800
img_height = 600 - 20
canvas = Canvas(root, bg='white', width=img_width, height=img_height)
canvas.place(x=0, y=0)
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
capture.release()
def start():
while True:
fet, frame = capture.read()
qrCodeDetector = cv2.QRCodeDetector()
data, points, straight_qrcode = qrCodeDetector.detectAndDecode(frame)
if data:
cv2.drawContours(frame, [np.int32(points)], 0, (0, 255, 0), 2) # 框出二維碼位置
print(data)
tkinter.Label(root, text="姓名:" + data).place(x=900 - 100, y=0, width=100, height=20)
tk_img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
tk_img = Image.fromarray(tk_img)
tk_img = tk_img.resize((img_width, img_height), Image.ANTIALIAS)
tk_img = ImageTk.PhotoImage(image=tk_img)
canvas.create_image(0, 0, anchor='nw', image=tk_img)
root.update()
root.after(1)
btnStart = tkinter.Button(root, text='開始', command=start)
btnStart.place(x=0, y=600 - 20, width=70, height=20)
root.protocol('WM_DELETE_WINDOW', on_closing)
root.mainloop()
關(guān)于如何制作二維碼可看我同專欄的另一邊篇文章,鏈接如下。文章來源:http://www.zghlxwxcb.cn/news/detail-550945.html
Python用qrcode和PIL制作二維碼并添加漢字(用Python在圖片上添加漢字)_"殤影的博客-CSDN博客python制作二維碼,python批量制作二維碼,python批量制作帶漢字的二維碼,在圖片上添加漢字,用Python在圖片上添加漢字https://blog.csdn.net/weixin_45694843/article/details/128046585?spm=1001.2014.3001.5502文章來源地址http://www.zghlxwxcb.cn/news/detail-550945.html
到了這里,關(guān)于Python用opencv實(shí)現(xiàn)動態(tài)識別二維碼,以及加強(qiáng)版Python GUI(圖像用戶界面編程)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!