Skip to content

Commit 9e598d7

Browse files
Fix QR scanning
1 parent 3bc5151 commit 9e598d7

File tree

1 file changed

+10
-11
lines changed
  • internal_filesystem/apps/com.micropythonos.camera/assets

1 file changed

+10
-11
lines changed

internal_filesystem/apps/com.micropythonos.camera/assets/camera_app.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class CameraApp(Activity):
2929
status_label_text_found = "Found QR, trying to decode... hold still..."
3030

3131
cam = None
32+
current_cam_buffer = None # Holds the current memoryview to prevent garba
3233
width = None
3334
height = None
3435

@@ -171,7 +172,6 @@ def onPause(self, screen):
171172
i2c.writeto(camera_addr, bytes([reg_high, reg_low, power_off_command]))
172173
except Exception as e:
173174
print(f"Warning: powering off camera got exception: {e}")
174-
self.image_dsc.data = None
175175
print("camera app cleanup done.")
176176

177177
def parse_camera_init_preferences(self):
@@ -213,19 +213,16 @@ def update_preview_image(self):
213213
self.image.set_scale(min(scale_factor_w,scale_factor_h))
214214

215215
def qrdecode_one(self):
216-
if self.image_dsc.data is None:
217-
print("qrdecode_one: can't decode empty image")
218-
return
219216
try:
220217
import qrdecode
221218
import utime
222219
before = time.ticks_ms()
223220
if self.colormode:
224-
result = qrdecode.qrdecode_rgb565(self.image_dsc.data, self.width, self.height)
221+
result = qrdecode.qrdecode_rgb565(self.current_cam_buffer, self.width, self.height)
225222
else:
226-
result = qrdecode.qrdecode(self.image_dsc.data, self.width, self.height)
223+
result = qrdecode.qrdecode(self.current_cam_buffer, self.width, self.height)
227224
after = time.ticks_ms()
228-
#result = bytearray("INSERT_QR_HERE", "utf-8")
225+
#result = bytearray("INSERT_TEST_QR_DATA_HERE", "utf-8")
229226
if not result:
230227
self.status_label.set_text(self.status_label_text_searching)
231228
else:
@@ -259,14 +256,14 @@ def snap_button_click(self, e):
259256
os.mkdir("data/images")
260257
except OSError:
261258
pass
262-
if self.image_dsc.data is None:
259+
if self.current_cam_buffer is None:
263260
print("snap_button_click: won't save empty image")
264261
return
265262
colorname = "RGB565" if self.colormode else "GRAY"
266263
filename=f"data/images/camera_capture_{mpos.time.epoch_seconds()}_{self.width}x{self.height}_{colorname}.raw"
267264
try:
268265
with open(filename, 'wb') as f:
269-
f.write(self.image_dsc.data)
266+
f.write(self.current_cam_buffer)
270267
print(f"Successfully wrote image to {filename}")
271268
except OSError as e:
272269
print(f"Error writing to file: {e}")
@@ -321,12 +318,14 @@ def try_capture(self, event):
321318
#print("capturing camera frame")
322319
try:
323320
if self.use_webcam:
324-
self.image_dsc.data = webcam.capture_frame(self.cam, "rgb565" if self.colormode else "grayscale")
321+
self.current_cam_buffer = webcam.capture_frame(self.cam, "rgb565" if self.colormode else "grayscale")
325322
elif self.cam.frame_available():
326-
self.image_dsc.data = self.cam.capture()
323+
self.current_cam_buffer = self.cam.capture()
327324
except Exception as e:
328325
print(f"Camera capture exception: {e}")
326+
return
329327
# Display the image:
328+
self.image_dsc.data = self.current_cam_buffer
330329
#self.image.invalidate() # does not work so do this:
331330
self.image.set_src(self.image_dsc)
332331
if not self.use_webcam:

0 commit comments

Comments
 (0)