scaner 6 months ago
parent 425e8aa9d3
commit aecd2576b5

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