1414
1515from mpos .apps import Activity
1616
17- width = 240
18- height = 240
17+ class Camera (Activity ):
1918
20- status_label_text = "No camera found."
21- status_label_text_searching = "Searching QR codes...\n \n Hold still and make them big!\n 10cm for simple QR codes,\n 20cm for complex."
22- status_label_text_found = "Decoding QR..."
19+ width = 240
20+ height = 240
2321
24- class Camera (Activity ):
22+ status_label_text = "No camera found."
23+ status_label_text_searching = "Searching QR codes...\n \n Hold still and make them big!\n 10cm for simple QR codes,\n 20cm for complex."
24+ status_label_text_found = "Decoding QR..."
2525
26- def __init__ (self ):
27- super ().__init__ ()
28- self .cam = None
29- self .current_cam_buffer = None # Variable to hold the current memoryview to prevent garbage collection
30- self .image_dsc = None
31- self .image = None
32- self .qr_label = None
33- self .scanqr_callback = None
34- self .use_webcam = False
35- self .qr_button = None
36- self .snap_button = None
37- self .capture_timer = None
38- self .status_label = None
39- self .status_label_cont = None
40- self .keepliveqrdecoding = False
26+ cam = None
27+ current_cam_buffer = None # Holds the current memoryview to prevent garbage collection
28+
29+ image = None
30+ image_dsc = None
31+ scanqr_mode = None
32+ use_webcam = False
33+ keepliveqrdecoding = False
34+
35+ capture_timer = None
36+
37+ # Widgets:
38+ qr_label = None
39+ qr_button = None
40+ snap_button = None
41+ status_label = None
42+ status_label_cont = None
4143
4244 def onCreate (self ):
43- self .scanqr_callback = self .getIntent ().extras .get ("scanqr_callback " )
45+ self .scanqr_mode = self .getIntent ().extras .get ("scanqr_mode " )
4446 main_screen = lv .obj ()
4547 main_screen .set_style_pad_all (0 , 0 )
4648 main_screen .set_style_border_width (0 , 0 )
@@ -76,13 +78,13 @@ def onCreate(self):
7678 self .image_dsc = lv .image_dsc_t ({
7779 "header" : {
7880 "magic" : lv .IMAGE_HEADER_MAGIC ,
79- "w" : width ,
80- "h" : height ,
81- "stride" : width * 2 ,
81+ "w" : self . width ,
82+ "h" : self . height ,
83+ "stride" : self . width * 2 ,
8284 "cf" : lv .COLOR_FORMAT .RGB565
8385 #"cf": lv.COLOR_FORMAT.L8
8486 },
85- 'data_size' : width * height * 2 ,
87+ 'data_size' : self . width * self . height * 2 ,
8688 'data' : None # Will be updated per frame
8789 })
8890 self .image .set_src (self .image_dsc )
@@ -115,15 +117,16 @@ def onResume(self, screen):
115117 print ("Camera initialized, continuing..." )
116118 self .capture_timer = lv .timer_create (self .try_capture , 100 , None )
117119 self .status_label_cont .add_flag (lv .obj .FLAG .HIDDEN )
118- if self .scanqr_callback :
120+ if self .scanqr_mode :
119121 self .start_qr_decoding ()
120122 else :
121123 self .qr_button .remove_flag (lv .obj .FLAG .HIDDEN )
122124 self .snap_button .remove_flag (lv .obj .FLAG .HIDDEN )
123125 else :
124126 print ("No camera found, stopping camtest.py" )
125- if self .scanqr_callback :
126- self .scanqr_callback (False ,"" )
127+ if self .scanqr_mode :
128+ self .finish ()
129+
127130
128131 def onStop (self , screen ):
129132 print ("camtest.py backgrounded, cleaning up..." )
@@ -138,26 +141,26 @@ def onStop(self, screen):
138141 def qrdecode_one (self ):
139142 try :
140143 import qrdecode
141- result = qrdecode .qrdecode_rgb565 (self .current_cam_buffer , width , height )
144+ result = qrdecode .qrdecode_rgb565 (self .current_cam_buffer , self . width , self . height )
142145 #result = bytearray("INSERT_QR_HERE", "utf-8")
143146 if not result :
144- self .status_label .set_text (status_label_text_searching )
147+ self .status_label .set_text (self . status_label_text_searching )
145148 else :
146149 self .stop_qr_decoding ()
147150 result = remove_bom (result )
148151 result = print_qr_buffer (result )
149152 print (f"QR decoding found: { result } " )
150- if self .scanqr_callback :
151- self .scanqr_callback (True ,result )
153+ if self .scanqr_mode :
154+ self .setResult (True , result )
152155 self .finish ()
153156 else :
154157 self .status_label .set_text (result ) # in the future, the status_label text should be copy-paste-able
155158 except ValueError as e :
156159 print ("QR ValueError: " , e )
157- self .status_label .set_text (status_label_text_searching )
160+ self .status_label .set_text (self . status_label_text_searching )
158161 except TypeError as e :
159162 print ("QR TypeError: " , e )
160- self .status_label .set_text (status_label_text_found )
163+ self .status_label .set_text (self . status_label_text_found )
161164 except Exception as e :
162165 print ("QR got other error: " , e )
163166
@@ -186,14 +189,14 @@ def start_qr_decoding(self):
186189 self .keepliveqrdecoding = True
187190 self .qr_label .set_text (lv .SYMBOL .EYE_CLOSE )
188191 self .status_label_cont .remove_flag (lv .obj .FLAG .HIDDEN )
189- self .status_label .set_text (status_label_text_searching )
192+ self .status_label .set_text (self . status_label_text_searching )
190193
191194 def stop_qr_decoding (self ):
192195 print ("Deactivating live QR decoding..." )
193196 self .keepliveqrdecoding = False
194197 self .qr_label .set_text (lv .SYMBOL .EYE_OPEN )
195- status_label_text = self .status_label .get_text ()
196- if status_label_text == status_label_text_searching or status_label_text == status_label_text_found : # if it found a QR code, leave it
198+ self . status_label_text = self .status_label .get_text ()
199+ if self . status_label_text in ( self . status_label_text_searching or self . status_label_text_found ) : # if it found a QR code, leave it
197200 self .status_label_cont .add_flag (lv .obj .FLAG .HIDDEN )
198201
199202 def qr_button_click (self , e ):
@@ -220,6 +223,8 @@ def try_capture(self, event):
220223 except Exception as e :
221224 print (f"Camera capture exception: { e } " )
222225
226+
227+
223228# Non-class functions:
224229def init_internal_cam ():
225230 try :
0 commit comments