Skip to content

Commit a8419cb

Browse files
Synchronize captureqr.py and camtest.py
1 parent 9ece189 commit a8419cb

2 files changed

Lines changed: 59 additions & 32 deletions

File tree

  • internal_filesystem/apps

internal_filesystem/apps/com.example.camtest/assets/camtest.py

Lines changed: 55 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,15 @@
55
# or in YUV format and discarding the U and V planes, but then the image will be gray (not great UX)
66
# and the performance impact of converting RGB565 to grayscale is probably minimal anyway.
77

8+
import lvgl as lv
89
import time
910

11+
try:
12+
import webcam
13+
except Exception as e:
14+
print(f"Info: could not import webcam module: {e}")
15+
16+
1017
import mpos.apps
1118
import mpos.ui
1219

@@ -17,20 +24,24 @@
1724
width = 240
1825
height = 240
1926

27+
cam = None
2028
# Variable to hold the current memoryview to prevent garbage collection
2129
current_cam_buffer = None
2230
image_dsc = None
2331
image = None
2432
qr_label = None
33+
scanqr_callback = None
2534

2635
use_webcam = False
2736
qr_button = None
2837
snap_button = None
2938

39+
capture_timer = None
40+
3041
status_label = None
3142
status_label_cont = None
3243
status_label_text = "No camera found."
33-
status_label_text_searching = "Searching QR codes..."
44+
status_label_text_searching = "Searching QR codes...\n\nHold still and make them big!\n10cm for simple QR codes,\n20cm for complex."
3445
status_label_text_found = "Decoding QR..."
3546

3647
def print_qr_buffer(buffer):
@@ -54,18 +65,23 @@ def remove_bom(buffer):
5465
return buffer
5566

5667
def qrdecode_one():
57-
global status_label, status_label_text
68+
global status_label, status_label_text, scanqr_callback
5869
try:
5970
import qrdecode
6071
result = qrdecode.qrdecode_rgb565(current_cam_buffer, width, height)
72+
#result = bytearray("INSERT_QR_HERE", "utf-8")
6173
if not result:
6274
status_label.set_text(status_label_text_searching)
6375
else:
76+
stop_qr_decoding()
6477
result = remove_bom(result)
6578
result = print_qr_buffer(result)
6679
print(f"QR decoding found: {result}")
67-
status_label.set_text(result)
68-
stop_qr_decoding()
80+
if scanqr_callback:
81+
scanqr_callback(True,result)
82+
mpos.ui.back_screen()
83+
else:
84+
status_label.set_text(result) # in the future, the status_label text should be copy-paste-able
6985
except ValueError as e:
7086
print("QR ValueError: ", e)
7187
status_label.set_text(status_label_text_searching)
@@ -129,7 +145,7 @@ def qr_button_click(e):
129145

130146
def try_capture(event):
131147
#print("capturing camera frame")
132-
global current_cam_buffer, image_dsc, image, use_webcam
148+
global current_cam_buffer, image_dsc, image, use_webcam, cam
133149
try:
134150
if use_webcam:
135151
current_cam_buffer = webcam.capture_frame(cam, "rgb565")
@@ -206,7 +222,6 @@ def build_ui():
206222
status_label.set_style_text_color(lv.color_white(), 0)
207223
status_label.set_width(lv.pct(100))
208224
status_label.center()
209-
mpos.ui.load_screen(main_screen)
210225

211226

212227
def init_cam():
@@ -254,27 +269,38 @@ def check_running(timer):
254269
print("camtest.py cleanup done.")
255270

256271

272+
def init(scanqr_cb=None):
273+
global scanqr_callback, cam, use_webcam, check_running_timer, status_label_cont, capture_timer, main_screen
274+
scanqr_callback = scanqr_cb
275+
build_ui()
276+
cam = init_cam()
277+
if cam:
278+
image.set_rotation(900) # internal camera is rotated 90 degrees
279+
else:
280+
print("camtest.py: no internal camera found, trying webcam on /dev/video0")
281+
try:
282+
cam = webcam.init("/dev/video0")
283+
use_webcam = True
284+
except Exception as e:
285+
print(f"camtest.py: webcam exception: {e}")
286+
if cam:
287+
print("Camera initialized, continuing...")
288+
check_running_timer = lv.timer_create(check_running, 500, None)
289+
capture_timer = lv.timer_create(try_capture, 100, None)
290+
status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
291+
if scanqr_callback:
292+
start_qr_decoding()
293+
else:
294+
qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
295+
snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
296+
return main_screen
297+
else:
298+
print("No camera found, stopping camtest.py")
299+
if scanqr_callback:
300+
scanqr_callback(False,"")
301+
return None
302+
257303

258-
build_ui()
259-
260-
cam = init_cam()
261-
if cam:
262-
image.set_rotation(900) # internal camera is rotated 90 degrees
263-
else:
264-
print("camtest.py: no internal camera found, trying webcam on /dev/video0")
265-
try:
266-
import webcam
267-
cam = webcam.init("/dev/video0")
268-
use_webcam = True
269-
except Exception as e:
270-
print(f"camtest.py: webcam exception: {e}")
271-
272-
if cam:
273-
print("Camera initialized, continuing...")
274-
check_running_timer = lv.timer_create(check_running, 500, None)
275-
qr_button.remove_flag(lv.obj.FLAG.HIDDEN)
276-
snap_button.remove_flag(lv.obj.FLAG.HIDDEN)
277-
status_label_cont.add_flag(lv.obj.FLAG.HIDDEN)
278-
capture_timer = lv.timer_create(try_capture, 100, None)
279-
else:
280-
print("No camera found, stopping camtest.py")
304+
if __name__ == '__main__':
305+
print("camera started as __main__")
306+
mpos.ui.load_screen(init())

internal_filesystem/apps/com.lightningpiggy.displaywallet/assets/captureqr.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ def qrdecode_one():
7373
if not result:
7474
status_label.set_text(status_label_text_searching)
7575
else:
76+
stop_qr_decoding()
7677
result = remove_bom(result)
7778
result = print_qr_buffer(result)
7879
print(f"QR decoding found: {result}")
79-
status_label.set_text(result)
80-
stop_qr_decoding()
81-
mpos.ui.back_screen()
8280
if scanqr_callback:
8381
scanqr_callback(True,result)
82+
mpos.ui.back_screen()
83+
else:
84+
status_label.set_text(result) # in the future, the status_label text should be copy-paste-able
8485
except ValueError as e:
8586
print("QR ValueError: ", e)
8687
status_label.set_text(status_label_text_searching)

0 commit comments

Comments
 (0)