Skip to content

Commit 0ad3a85

Browse files
committed
added qrcode encoder and decoder miniproject
1 parent 406004d commit 0ad3a85

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import tkinter as tk
2+
from tkinter import messagebox, filedialog
3+
import qrcode
4+
from PIL import Image, ImageTk
5+
import cv2
6+
from pyzbar.pyzbar import decode
7+
8+
class QRCodeApp:
9+
def _init_(self, root):
10+
self.root = root
11+
self.root.title("QR Code Encoder and Decoder")
12+
13+
self.main_frame = tk.Frame(root)
14+
self.main_frame.pack(pady=20)
15+
self.main_frame.pack(padx=30)
16+
17+
self.button1 = tk.Button(self.main_frame, text="Encode Text to QR Code", command=self.show_encode_screen)
18+
self.button1.pack(pady=10)
19+
20+
self.button2 = tk.Button(self.main_frame, text="Decode QR Code from Camera", command=self.show_decode_screen)
21+
self.button2.pack(pady=10)
22+
23+
self.encode_frame = None
24+
self.decode_frame = None
25+
26+
def show_encode_screen(self):
27+
self.clear_frame()
28+
self.encode_frame = tk.Frame(self.root)
29+
self.encode_frame.pack(pady=20)
30+
self.encode_frame.pack(padx=30)
31+
32+
self.text_box = tk.Entry(self.encode_frame, width=50)
33+
self.text_box.pack(pady=10)
34+
35+
self.convert_button = tk.Button(self.encode_frame, text="Convert", command=self.convert_to_qr)
36+
self.convert_button.pack(pady=10)
37+
38+
self.back_button = tk.Button(self.encode_frame, text="Back", command=self.show_main_screen)
39+
self.back_button.pack(pady=10)
40+
41+
def convert_to_qr(self):
42+
text = self.text_box.get()
43+
if text:
44+
qr = qrcode.make(text)
45+
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
46+
if file_path:
47+
qr.save(file_path)
48+
messagebox.showinfo("Success", "QR Code saved successfully!")
49+
else:
50+
messagebox.showwarning("Input Error", "Please enter text to convert to QR Code.")
51+
52+
def show_decode_screen(self):
53+
self.clear_frame()
54+
self.decode_frame = tk.Frame(self.root)
55+
self.decode_frame.pack(pady=10)
56+
self.decode_frame.pack(padx=50)
57+
58+
self.start_camera_button = tk.Button(self.decode_frame, text="Start Camera", command=self.start_camera)
59+
self.start_camera_button.pack(pady=0)
60+
61+
self.back_button = tk.Button(self.decode_frame, text="Back", command=self.show_main_screen)
62+
self.back_button.pack(pady=10)
63+
64+
def start_camera(self):
65+
cap = cv2.VideoCapture(0)
66+
found_qr = False
67+
68+
while not found_qr:
69+
ret, frame = cap.read()
70+
if not ret:
71+
break
72+
73+
decoded_objects = decode(frame)
74+
for obj in decoded_objects:
75+
points = obj.polygon
76+
if len(points) > 4:
77+
hull = cv2.convexHull(points, returnPoints=True)
78+
else:
79+
hull = points
80+
81+
n = len(hull)
82+
for j in range(0, n):
83+
cv2.line(frame, tuple(hull[j]), tuple(hull[(j+1) % n]), (255, 0, 0), 3)
84+
85+
qr_text = obj.data.decode("utf-8")
86+
found_qr = True
87+
cv2.putText(frame, qr_text, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
88+
self.display_decoded_text(qr_text)
89+
90+
cv2.imshow("QR Code Scanner", frame)
91+
if cv2.waitKey(1) & 0xFF == ord('q'):
92+
break
93+
94+
cap.release()
95+
cv2.destroyAllWindows()
96+
97+
def display_decoded_text(self, text):
98+
messagebox.showinfo("Decoded Text", text)
99+
# create a new text file and write the decoded text to it
100+
file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
101+
if file_path:
102+
with open(file_path, "w") as f:
103+
f.write(text)
104+
messagebox.showinfo("Success", "Decoded text saved successfully!")
105+
106+
def show_main_screen(self):
107+
self.clear_frame()
108+
self.main_frame.pack(pady=20)
109+
self.main_frame.pack(padx=30)
110+
111+
# reset the encode and decode frames, basically re-run the _init_ method
112+
self.button1 = tk.Button(self.main_frame, text="Encode Text to QR Code", command=self.show_encode_screen)
113+
self.button1.pack(pady=10)
114+
115+
self.button2 = tk.Button(self.main_frame, text="Decode QR Code from Camera", command=self.show_decode_screen)
116+
self.button2.pack(pady=10)
117+
118+
self.encode_frame = None
119+
self.decode_frame = None
120+
121+
122+
123+
def clear_frame(self):
124+
for widget in self.root.winfo_children():
125+
widget.pack_forget()
126+
widget.destroy()
127+
self.main_frame = tk.Frame(self.root)
128+
self.main_frame.pack(pady=20)
129+
130+
if __name__ == "__main__":
131+
root = tk.Tk()
132+
app = QRCodeApp(root)
133+
root.mainloop()

0 commit comments

Comments
 (0)