-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
237 lines (196 loc) · 7.82 KB
/
test.py
File metadata and controls
237 lines (196 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/env python3
"""
Simple QR Code and Data Matrix Scanner Application
A streamlined command-line application to detect and display QR codes and
Data Matrix codes using the CodeScanner library.
"""
import time
import sys
import signal
import os
import select
import termios
import tty
import atexit
from datetime import datetime
# Import the CodeScanner library
try:
from main import CodeScanner, DetectionMode, PYZBAR_AVAILABLE, DMTX_AVAILABLE
print("✓ Successfully imported CodeScanner library")
except ImportError as e:
print(f"✗ Error importing CodeScanner library: {e}")
print("Make sure code_scanner.py is in the same directory or PYTHONPATH")
sys.exit(1)
class ScannerApp:
"""
Simple application for testing code scanning capabilities.
"""
def __init__(self):
"""Initialize the scanner application."""
# Initialize state
self.running = True
self.scanner = None
self.codes_detected = 0
# Set up terminal for keyboard input
self.setup_terminal()
# Register signal handlers and cleanup
signal.signal(signal.SIGINT, self.handle_exit)
atexit.register(self.cleanup)
# Print header and capabilities
self.clear_screen()
print("-" * 50)
print(" CODE SCANNER APPLICATION ")
print("-" * 50)
print("Capabilities:")
print(f" QR Codes: {'ENABLED' if PYZBAR_AVAILABLE else 'DISABLED'}")
print(f" Data Matrix: {'ENABLED' if DMTX_AVAILABLE else 'DISABLED'}")
print("-" * 50)
# Initialize scanner
try:
self.scanner = CodeScanner()
print("Scanner initialized successfully")
except Exception as e:
print(f"Error initializing scanner: {e}")
sys.exit(1)
def setup_terminal(self):
"""Configure terminal for non-blocking input."""
try:
self.old_settings = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin.fileno())
except Exception as e:
print(f"Warning: Terminal setup failed: {e}")
print("You may need to use Ctrl+C to exit")
def restore_terminal(self):
"""Restore terminal to original settings."""
try:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings)
except Exception:
pass
def handle_exit(self, sig, frame):
"""Handle exit signals."""
print("\nExiting application...")
self.running = False
def cleanup(self):
"""Perform cleanup operations."""
self.restore_terminal()
if self.scanner:
self.scanner.stop()
print("Scanner stopped")
def clear_screen(self):
"""Clear the terminal screen."""
os.system('cls' if os.name == 'nt' else 'clear')
def show_menu(self):
"""Display the main menu options."""
print("\nSelect a detection mode:")
print(" 1: Single mode - Detect one code at a time")
print(" 2: Continuous mode - Detect all codes continuously")
print(" 3: Triggered mode - Scan only when triggered")
print(" q: Quit")
print("-" * 50)
def on_code_detected(self, code_info):
"""
Callback function for code detection events.
Args:
code_info: Information about the detected code, or None if removed
"""
if code_info is None:
# Code has been removed
print("\n✓ Code removed - Ready for next detection")
return
# Code detected
self.codes_detected += 1
now = datetime.now().strftime("%H:%M:%S")
# Print detection information
print("\n" + "-" * 50)
print(f" {code_info.type.upper()} CODE #{self.codes_detected}")
print("-" * 50)
print(f"Data: {code_info.data}")
print(f"Time: {now}")
print(f"Position: {code_info.rect}")
# Print controls reminder
if self.scanner.detection_mode == DetectionMode.TRIGGERED:
print("\nPress 't' to trigger another scan")
elif self.scanner.detection_mode == DetectionMode.SINGLE:
print("\nRemove code to detect another")
def start_detection(self, mode):
"""
Start code detection with the specified mode.
Args:
mode: The detection mode to use
"""
print(f"\nStarting detection in {mode.value} mode...")
# Configure scanner
self.scanner.set_mode(mode)
self.scanner.start(self.on_code_detected)
# Show controls
print("\nControls:")
print(" 1,2,3: Change mode")
print(" t: Trigger scan (in triggered mode)")
print(" q: Quit")
print(" h: Show help")
# Main detection loop
while self.running:
# Check for keyboard input
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
key = sys.stdin.read(1)
if key == 'q':
print("\nExiting...")
self.running = False
elif key == '1':
print("\nSwitching to SINGLE mode")
self.scanner.set_mode(DetectionMode.SINGLE)
elif key == '2':
print("\nSwitching to CONTINUOUS mode")
self.scanner.set_mode(DetectionMode.CONTINUOUS)
elif key == '3':
print("\nSwitching to TRIGGERED mode")
self.scanner.set_mode(DetectionMode.TRIGGERED)
elif key == 't':
if self.scanner.detection_mode == DetectionMode.TRIGGERED:
print("\nTriggering scan...")
self.scanner.trigger_scan()
else:
print("\nTriggering only works in TRIGGERED mode")
elif key == 'h':
print("\nControls:")
print(" 1,2,3: Change mode")
print(" t: Trigger scan (in triggered mode)")
print(" q: Quit")
print(" h: Show help")
# Sleep to reduce CPU usage
time.sleep(0.1)
# Stop scanner
self.scanner.stop()
def run(self):
"""Run the main application loop."""
self.show_menu()
while self.running:
# Wait for menu selection
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
choice = sys.stdin.read(1)
if choice == '1':
self.start_detection(DetectionMode.SINGLE)
# Show menu again after returning from detection
self.show_menu() if self.running else None
elif choice == '2':
self.start_detection(DetectionMode.CONTINUOUS)
self.show_menu() if self.running else None
elif choice == '3':
self.start_detection(DetectionMode.TRIGGERED)
self.show_menu() if self.running else None
elif choice == 'q':
print("\nExiting application...")
self.running = False
# Sleep to reduce CPU usage
time.sleep(0.1)
if __name__ == "__main__":
print("Starting Code Scanner Application...")
app = ScannerApp()
try:
app.run()
except Exception as e:
print(f"\nUnexpected error: {e}")
import traceback
traceback.print_exc()
finally:
app.cleanup()