最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python實(shí)現(xiàn)全自動二維碼識別

 更新時間:2023年11月23日 10:00:31   作者:mYlEaVeiSmVp  
這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)全自動二維碼識別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

純筆記,可以做到全屏識別二維碼,自動識別,復(fù)制鏈接,生成簡單的二維碼,將識別到的內(nèi)容轉(zhuǎn)為txt

實(shí)現(xiàn)代碼

import pyautogui
from PIL import Image
from pyzbar.pyzbar import decode
import tkinter as tk
from tkinter import Label, Button, Listbox, Entry, END, SINGLE, filedialog
import threading
import time
import qrcode
from PIL import ImageTk
import cv2

class QRCodeScannerApp:
def init(self, root):
self.root = root
self.root.title("二維碼識別器專業(yè)版")
    # 設(shè)置窗口的默認(rèn)大小
    self.root.geometry("1600x1200")
 
    # 創(chuàng)建頂部功能按鈕區(qū)域
    self.button_frame = tk.Frame(root)
    self.button_frame.pack(pady=10)
 
    self.prompt_label = Label(root, text="請選中歷史記錄欄的記錄以啟用某些功能", fg="red")
    self.prompt_label.pack(pady=5)
 
    # 將所有按鈕移到button_frame中,并將它們設(shè)置為橫向布局
    self.capture_button = Button(self.button_frame, text="捕獲屏幕并識別", command=self.capture_screen_and_recognize)
    self.capture_button.grid(row=0, column=0, padx=5)
 
    self.batch_scan_button = Button(self.button_frame, text="導(dǎo)入圖片并識別", command=self.batch_scan)
    self.batch_scan_button.grid(row=0, column=1, padx=5)
 
    self.autoscan_button = Button(self.button_frame, text="開始自動掃描", command=self.start_auto_scan)
    self.autoscan_button.grid(row=0, column=2, padx=5)
 
    self.stop_button = Button(self.button_frame, text="停止掃描", command=self.stop_scan, state=tk.DISABLED)
    self.stop_button.grid(row=0, column=3, padx=5)
 
    self.copy_button = Button(self.button_frame, text="復(fù)制選中的歷史記錄", command=self.copy_selected_history)
    self.copy_button.grid(row=0, column=4, padx=5)
 
    self.save_button = Button(self.button_frame, text="保存歷史記錄為TXT", command=self.save_history_to_txt)
    self.save_button.grid(row=0, column=5, padx=5)
 
    self.generate_button = Button(self.button_frame, text="生成二維碼", command=self.generate_qrcode)
    self.generate_button.grid(row=0, column=6, padx=5)
 
    self.delete_history_button = Button(self.button_frame, text="刪除選中歷史記錄", command=self.delete_selected_history)
    self.delete_history_button.grid(row=0, column=7, padx=5)
 
    self.clear_history_button = Button(self.button_frame, text="清空所有歷史記錄", command=self.clear_all_history)
    self.clear_history_button.grid(row=0, column=8, padx=5)
 
    self.search_button = Button(self.button_frame, text="搜索", command=self.search_history)
    self.search_button.grid(row=0, column=9, padx=5)
 
    self.export_button = Button(self.button_frame, text="導(dǎo)出QR碼圖像", command=self.export_qr_code)
    self.export_button.grid(row=0, column=10, padx=5)
 
    # 文本和輸入部分
    self.label = Label(root, text="掃描結(jié)果:")
    self.label.pack(pady=10)
 
    self.result_label = Label(root, text="", wraplength=500)
    self.result_label.pack(pady=10)
 
    self.current_label = Label(root, text="當(dāng)前識別:")
    self.current_label.pack(pady=10)
 
    # 增加寬度
    self.current_listbox_scrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL)
    self.current_listbox_scrollbar.pack(fill=tk.X)
    self.current_listbox = Listbox(root, selectmode=SINGLE, height=10, width=250,
                                   xscrollcommand=self.current_listbox_scrollbar.set)
    self.current_listbox.pack(pady=10)
    self.current_listbox_scrollbar.config(command=self.current_listbox.xview)
 
    self.history_label = Label(root, text="歷史記錄:")
    self.history_label.pack(pady=10)
 
    self.history_listbox_scrollbar = tk.Scrollbar(root, orient=tk.HORIZONTAL)
    self.history_listbox_scrollbar.pack(fill=tk.X)
    self.history_listbox = Listbox(root, selectmode=SINGLE, height=10, width=250,
                                   xscrollcommand=self.history_listbox_scrollbar.set)
    self.history_listbox.pack(pady=10)
    self.history_listbox_scrollbar.config(command=self.history_listbox.xview)
 
    self.interval_label = Label(root, text="掃描間隔時間 (毫秒):")
    self.interval_label.pack(pady=10)
 
    self.interval_entry = Entry(root)
    self.interval_entry.pack(pady=10)
    self.interval_entry.insert(0, "500")
 
    self.generate_label = Label(root, text="輸入要生成的內(nèi)容:")
    self.generate_label.pack(pady=10)
 
    self.generate_entry = Entry(root)
    self.generate_entry.pack(pady=10)
 
    self.search_label = Label(root, text="搜索歷史:")
    self.search_label.pack(pady=10)
 
    self.search_entry = Entry(root)
    self.search_entry.pack(pady=10)
 
    # 其他屬性
    self.auto_scanning = False
    self.history = []
    self.history_counter = 0
    self.seen_qrcodes = set()
 
def clear_all_history(self):
    self.history.clear()
    self.history_listbox.delete(0, END)
 
# 添加搜索歷史記錄的函數(shù)
def search_history(self):
    query = self.search_entry.get().lower()
    self.history_listbox.delete(0, END)
    for item in self.history:
        if query in item.lower():
            self.history_listbox.insert(END, item)
 
def export_qr_code(self):
    selected_index = self.history_listbox.curselection()
    if selected_index:
        selected_data = self.history[int(selected_index[0])]
        content_without_number = selected_data.split(': ', 1)[-1]
 
        # 使用PIL庫創(chuàng)建QR碼圖像
        qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
        qr.add_data(content_without_number)
        qr.make(fit=True)
        qr_image = qr.make_image(fill_color="black", back_color="white")
 
        # 選擇保存路徑
        file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG Files", "*.png")])
 
        # 如果用戶選擇了路徑,則保存QR碼圖像
        if file_path:
            qr_image.save(file_path)
 
# 添加新的函數(shù)生成二維碼并在新窗口中顯示
def generate_qrcode(self):
    qr_data = self.generate_entry.get()
    if qr_data:
        qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
        qr.add_data(qr_data)
        qr.make(fit=True)
        img = qr.make_image(fill_color="black", back_color="white")
 
        # 在新窗口中顯示二維碼
        new_window = tk.Toplevel(self.root)
        new_window.title("生成的二維碼")
 
        qr_image = ImageTk.PhotoImage(img)  # 將PIL圖像轉(zhuǎn)換為Tkinter可使用的圖像
        qr_label = Label(new_window, image=qr_image)
        qr_label.image = qr_image  # 保存圖像的引用以防被垃圾收集器回收
        qr_label.pack()
 
def start_auto_scan(self):
    """開始自動掃描"""
    self.auto_scanning = True
    self.autoscan_button.config(state=tk.DISABLED)
    self.stop_button.config(state=tk.NORMAL)
    self.auto_scan()
 
def stop_scan(self):
    """停止自動掃描"""
    self.auto_scanning = False
    self.autoscan_button.config(state=tk.NORMAL)
    self.stop_button.config(state=tk.DISABLED)
 
def auto_scan(self):
    if self.auto_scanning:
        # 啟動一個線程來異步執(zhí)行屏幕捕捉和二維碼識別
        threading.Thread(target=self.capture_screen_and_recognize, daemon=True).start()
 
        try:
            interval = int(self.interval_entry.get())
        except ValueError:
            interval = 500
        self.root.after(interval, self.auto_scan)
 
def capture_screen_and_recognize(self):
    start_time = time.time()
    screenshot = pyautogui.screenshot()
    image = Image.frombytes('RGB', screenshot.size, screenshot.tobytes())
    decoded_objects = decode(image)
 
    new_qrcodes = []
 
    if decoded_objects:
        for obj in decoded_objects:
            data = obj.data.decode("utf-8")
            if data not in self.seen_qrcodes:  # 如果這是一個新的二維碼數(shù)據(jù)
                self.seen_qrcodes.add(data)
                new_qrcodes.append(data)
 
        if new_qrcodes:  # 如果有新的二維碼,清除當(dāng)前識別列表框
            self.current_listbox.delete(0, END)
 
        elapsed_time = time.time() - start_time  # 計算花費(fèi)的時間
        self.result_label.config(text=f"識別到 {len(new_qrcodes)} 個新二維碼,耗時 {elapsed_time:.3f} 秒")
 
        for i, data in enumerate(new_qrcodes):
            self.history_counter += 1
            self.current_listbox.insert(END, f"{self.history_counter}: {data}")
            self.history.append(f"{self.history_counter}: {data}")
            self.history_listbox.insert(END, f"{self.history_counter}: {data}")
    else:
        self.result_label.config(text="未找到二維碼")
 
def copy_selected_history(self):
    selected_index = self.history_listbox.curselection()
    if selected_index:
        selected_data = self.history[int(selected_index[0])]
        # 使用字符串切片去掉標(biāo)號
        content_without_number = selected_data.split(': ', 1)[-1]
        self.root.clipboard_clear()
        self.root.clipboard_append(content_without_number)
        self.root.update()
 
def save_history_to_txt(self):
    file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text Files", "*.txt")])
    if file_path:
        with open(file_path, "w") as file:
            for item in self.history:
                file.write(item + "\n")
 
# 添加新的函數(shù)來批量掃描圖片
def batch_scan(self):
    file_paths = filedialog.askopenfilenames(title="選擇圖片", filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")])
 
    for file_path in file_paths:
        image = cv2.imread(file_path)
 
        # 調(diào)整圖片大小,以適應(yīng)屏幕捕獲的分辨率
        scaled_image = cv2.resize(image, (self.root.winfo_screenwidth(), self.root.winfo_screenheight()))
        screenshot = Image.fromarray(cv2.cvtColor(scaled_image, cv2.COLOR_BGR2RGB))
 
        # 在屏幕截圖上識別二維碼
        decoded_objects = decode(screenshot)
 
        new_qrcodes = []
 
        if decoded_objects:
            for obj in decoded_objects:
                data = obj.data.decode("utf-8")
                if data not in self.seen_qrcodes:  # 如果這是一個新的二維碼數(shù)據(jù)
                    self.seen_qrcodes.add(data)
                    new_qrcodes.append(data)
 
            for i, data in enumerate(new_qrcodes):
                self.history_counter += 1
                self.current_listbox.insert(END, f"{self.history_counter}: {data}")
                self.history.append(f"{self.history_counter}: {data}")
                self.history_listbox.insert(END, f"{self.history_counter}: {data}")
        else:
            self.result_label.config(text="未找到二維碼")
 
        self.root.update()  # 更新GUI以顯示結(jié)果
 
# 添加刪除和清空歷史記錄的函數(shù)
def delete_selected_history(self):
    selected_index = self.history_listbox.curselection()
    if selected_index:
        del self.history[int(selected_index[0])]
        self.history_listbox.delete(selected_index)
        
if name == "main":
root = tk.Tk()
app = QRCodeScannerApp(root)
root.mainloop()

實(shí)現(xiàn)效果

到此這篇關(guān)于基于Python實(shí)現(xiàn)全自動二維碼識別的文章就介紹到這了,更多相關(guān)Python二維碼識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyCharm搭建Spark開發(fā)環(huán)境的實(shí)現(xiàn)步驟

    PyCharm搭建Spark開發(fā)環(huán)境的實(shí)現(xiàn)步驟

    這篇文章主要介紹了PyCharm搭建Spark開發(fā)環(huán)境的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Python自定義異常的全面指南(入門到實(shí)踐)

    Python自定義異常的全面指南(入門到實(shí)踐)

    想象你正在開發(fā)一個銀行系統(tǒng),用戶轉(zhuǎn)賬時余額不足,如果直接拋出ValueError,調(diào)用方很難區(qū)分是金額格式錯誤還是余額不足,這正是Python自定義異常的價值所在——它讓錯誤處理更精準(zhǔn)、代碼更易維護(hù)、調(diào)試更高效,本文將用最接地氣的方式,帶你掌握這個實(shí)用技能
    2025-08-08
  • 解決pycharm啟動后總是不停的updating indices...indexing的問題

    解決pycharm啟動后總是不停的updating indices...indexing的問題

    今天小編就為大家分享一篇解決pycharm啟動后總是不停的updating indices...indexing的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • tensorflow 自定義損失函數(shù)示例代碼

    tensorflow 自定義損失函數(shù)示例代碼

    這篇文章主要介紹了tensorflow 自定義損失函數(shù)示例,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • PyQt5使用QTimer實(shí)現(xiàn)電子時鐘

    PyQt5使用QTimer實(shí)現(xiàn)電子時鐘

    這篇文章主要為大家詳細(xì)介紹了PyQt5使用QTimer實(shí)現(xiàn)電子時鐘,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Python?Flask?實(shí)現(xiàn)?HTML?文件壓縮案例代碼(9?級壓縮)

    Python?Flask?實(shí)現(xiàn)?HTML?文件壓縮案例代碼(9?級壓縮)

    這篇文章主要介紹了Python?Flask?實(shí)現(xiàn)?HTML?文件壓縮案例代碼(9?級壓縮),本案例是基于?Python?Flask?進(jìn)行搭建,所以需要提前搭建一個?Flask?項目環(huán)境,有?app.py?文件和?templates/index.html?文件即可,需要的朋友可以參考下
    2023-01-01
  • 詳解Python 4.0 預(yù)計推出的新功能

    詳解Python 4.0 預(yù)計推出的新功能

    這篇文章主要介紹了詳解Python 4.0 預(yù)計推出的新功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-07-07
  • Python實(shí)現(xiàn)

    Python實(shí)現(xiàn)"驗(yàn)證回文串"的幾種方法

    這篇文章主要介紹了Python實(shí)現(xiàn)"驗(yàn)證回文串"的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Django基于Models定制Admin后臺實(shí)現(xiàn)過程解析

    Django基于Models定制Admin后臺實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Django基于Models定制Admin后臺實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • python?通過dict(zip)和{}的方式構(gòu)造字典的方法

    python?通過dict(zip)和{}的方式構(gòu)造字典的方法

    在python中,通常通過dict和zip組合來構(gòu)建鍵值對,這篇文章主要介紹了python?通過dict(zip)和{}的方式構(gòu)造字典的方法,需要的朋友可以參考下
    2022-07-07

最新評論

共和县| 承德县| 凭祥市| 台湾省| 高陵县| 浙江省| 安多县| 久治县| 兴义市| 曲麻莱县| 连州市| 枣强县| 红安县| 扎囊县| 县级市| 高阳县| 白朗县| 广饶县| 招远市| 乌审旗| 永善县| 丹江口市| 同江市| 紫阳县| 佛冈县| 通河县| 晋城| 汨罗市| 水富县| 武安市| 柳州市| 沙河市| 庐江县| 星座| 红原县| 河北区| 钟山县| 得荣县| 八宿县| 宽甸| 浦县|