scaner 6 months ago
parent 425e8aa9d3
commit aecd2576b5

@ -1,92 +1,123 @@
from tkinter import *
import tkinter as tk import tkinter as tk
from tkinter import messagebox from tkinter import filedialog
import pymysql import pymysql
from flask import Flask, render_template, request, redirect, url_for
class Application(tk.Frame):
def __init__(self, master=None): conn = pymysql.connect(host='localhost',
super().__init__(master) user='root',
self.master = master password='123456',
self.pack() database='scaner',
self.create_widgets() port=3306,
charset='utf8')
def create_widgets(self):
self.label_username = tk.Label(self, text="Username:") # 使用 cursor() 方法创建一个游标对象 cursor
self.label_username.grid(row=0, column=0) cursor = conn.cursor()
print("数据库连接成功!")
self.entry_username = tk.Entry(self)
self.entry_username.grid(row=0, column=1)
# 查询用户信息
self.label_password = tk.Label(self, text="Password:") def userinfo_query(username, password):
self.label_password.grid(row=1, column=0) cursor.execute("select * from `user`")
data = cursor.fetchall()
self.entry_password = tk.Entry(self, show="*")
self.entry_password.grid(row=1, column=1) username_list_length = len(data)
self.register_button = tk.Button(self, text="Register", command=self.register_user) for i in range(username_list_length):
self.register_button.grid(row=2, column=1) username.append(data[i][0])
password.append(data[i][1])
self.login_button = tk.Button(self, text="Login", command=self.login_user)
self.login_button.grid(row=3, column=1)
def user_register():
def register_user(self): deng = Tk()
username = self.entry_username.get() deng.title("注册")
password = self.entry_password.get() deng.geometry("300x200")
self.save_to_db(username, password) # 在GUI中添加新的输入字段和按钮
self.clear_fields() label_new_username = Label(deng, text="新账号:")
label_new_username.pack()
def login_user(self): entry_new_username = Entry(deng)
username = self.entry_username.get() entry_new_username.pack()
password = self.entry_password.get() label_new_password = Label(deng, text="新密码:")
label_new_password.pack()
self.validate_credentials(username, password) entry_new_password = Entry(deng, show="*")
self.clear_fields() entry_new_password.pack()
def save_to_db(self, username, password): btn_register = Button(deng, text="注册", command=lambda: register_action(entry_new_username, entry_new_password))
# 连接数据库 btn_register.pack(side='right', padx=50)
db = pymysql.connect(host='localhost',
user='your_username',
password='your_password', def register_action(entry_new_username, entry_new_password):
database='your_database', cursor.execute("INSERT INTO user (`username`, `password`) VALUES (%s, %s)",
charset='utf8mb4') (entry_new_username.get(), entry_new_password.get()))
conn.commit()
try: print("注册成功!")
with db.cursor() as cursor:
sql = "INSERT INTO users (username, password) VALUES (%s, %s)"
cursor.execute(sql, (username, password)) def user_login():
db.commit() global count
messagebox.showinfo("Registration", "User registered successfully!") count = 2
except Exception as e: username_list = []
messagebox.showerror("Registration", f"Error: {e}") password_list = []
finally: userinfo_query(username_list, password_list)
db.close()
username_input = entry_username.get()
def validate_credentials(self, username, password): password_input = entry_password.get()
# 连接数据库
db = pymysql.connect(host='localhost', if username_input not in username_list:
user='your_username', label_message.config(text="账号错误!")
password='your_password', else:
database='your_database', if password_input in password_list:
charset='utf8mb4') def choose_folde():
root.destroy()
try: root5 = tk.Tk()
with db.cursor() as cursor: root5.title('选择图片')
sql = "SELECT * FROM users WHERE username=%s AND password=%s" root5.geometry('300x300')
cursor.execute(sql, (username, password)) folder_path = filedialog.askdirectory()
result = cursor.fetchone() if folder_path:
if result: print(f"您选择了文件夹: {folder_path}")
messagebox.showinfo("Login", "Login successful!")
else: root.mainloop()
messagebox.showerror("Login", "Invalid credentials.") def callback():
except Exception as e: rootback = tk.Tk()
messagebox.showerror("Login", f"Error: {e}") rootback.title('用户界面')
finally: rootback.geometry('300x300')
db.close() tk.Label(rootback, text='请选择你的功能').pack()
tk.Button(rootback, text="选择需要扫描的图片", command=choose_folde).pack()
def clear_fields(self): rootback.mainloop()
self.entry_username.delete(0, tk.END)
self.entry_password.delete(0, tk.END) # windows.destroy()
root = tk.Tk()
root = tk.Tk() root.title('用户界面')
app = Application(master=root) root.geometry('300x300')
app.mainloop() tk.Label(root, text='请选择你的功能').pack()
tk.Button(root, text="选择需要扫描的图片", command=choose_folde).pack()
tk.mainloop()
else:
label_message.config(text="密码错误!还可以尝试{}".format(count))
count += 1
if count == 3:
label_message.config(text="登录失败!")
btn_login.config(state='disabled')
windows = Tk()
windows.title("登录")
windows.geometry("300x200")
label_username = Label(windows, text="账号:")
label_username.pack()
entry_username = Entry(windows)
entry_username.pack()
label_password = Label(windows, text="密码:")
label_password.pack()
entry_password = Entry(windows, show="*")
entry_password.pack()
btn_login = Button(windows, text="登录", command=user_login)
btn_login.pack(side='left', padx=50)
entry_password.pack()
btn_login = Button(windows, text='注册', command=user_register)
btn_login.pack(side='right', padx=50)
label_message = Label(windows, text="")
label_message.pack()
windows.mainloop()

@ -0,0 +1,33 @@
# https://digi.bib.uni-mannheim.de/tesseract/
# 配置环境变量如E:\Program Files (x86)\Tesseract-OCR
# tesseract -v进行测试
# tesseract XXX.png 得到结果
# pip install pytesseract
# anaconda lib site-packges pytesseract pytesseract.py
# tesseract_cmd 修改为绝对路径即可
from PIL import Image
import pytesseract
import cv2
import os
preprocess = 'blur' #thresh
image = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":
gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
if preprocess == "blur":
gray = cv2.medianBlur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
Loading…
Cancel
Save