Skip to content

Commit 4ef4f66

Browse files
Camera app: simplify
1 parent d4239b6 commit 4ef4f66

File tree

1 file changed

+28
-35
lines changed
  • internal_filesystem/apps/com.micropythonos.camera/assets

1 file changed

+28
-35
lines changed

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

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

3030
cam = None
31-
current_cam_buffer = None # Holds the current memoryview to prevent garbage collection
3231
width = None
3332
height = None
3433

@@ -171,6 +170,7 @@ def onPause(self, screen):
171170
i2c.writeto(camera_addr, bytes([reg_high, reg_low, power_off_command]))
172171
except Exception as e:
173172
print(f"Warning: powering off camera got exception: {e}")
173+
self.image_dsc.data = None
174174
print("camera app cleanup done.")
175175

176176
def parse_camera_init_preferences(self):
@@ -212,11 +212,14 @@ def update_preview_image(self):
212212
self.image.set_scale(min(scale_factor_w,scale_factor_h))
213213

214214
def qrdecode_one(self):
215+
if self.image_dsc.data is None:
216+
print("qrdecode_one: can't decode empty image")
217+
return
215218
try:
216219
import qrdecode
217220
import utime
218221
before = time.ticks_ms()
219-
result = qrdecode.qrdecode(self.current_cam_buffer, self.width, self.height)
222+
result = qrdecode.qrdecode(self.image_dsc.data, self.width, self.height)
220223
after = time.ticks_ms()
221224
#result = bytearray("INSERT_QR_HERE", "utf-8")
222225
if not result:
@@ -252,15 +255,17 @@ def snap_button_click(self, e):
252255
os.mkdir("data/images")
253256
except OSError:
254257
pass
255-
if self.current_cam_buffer is not None:
256-
colorname = "RGB565" if self.colormode else "GRAY"
257-
filename=f"data/images/camera_capture_{mpos.time.epoch_seconds()}_{self.width}x{self.height}_{colorname}.raw"
258-
try:
259-
with open(filename, 'wb') as f:
260-
f.write(self.current_cam_buffer)
261-
print(f"Successfully wrote current_cam_buffer to {filename}")
262-
except OSError as e:
263-
print(f"Error writing to file: {e}")
258+
if self.image_dsc.data is None:
259+
print("snap_button_click: won't save empty image")
260+
return
261+
colorname = "RGB565" if self.colormode else "GRAY"
262+
filename=f"data/images/camera_capture_{mpos.time.epoch_seconds()}_{self.width}x{self.height}_{colorname}.raw"
263+
try:
264+
with open(filename, 'wb') as f:
265+
f.write(self.image_dsc.data)
266+
print(f"Successfully wrote image to {filename}")
267+
except OSError as e:
268+
print(f"Error writing to file: {e}")
264269

265270
def start_qr_decoding(self):
266271
print("Activating live QR decoding...")
@@ -305,40 +310,28 @@ def zoom_button_click(self, e):
305310
print(f"self.cam.set_res_raw returned {result}")
306311

307312
def open_settings(self):
308-
self.image_dsc.data = None
309-
self.current_cam_buffer = None
310313
intent = Intent(activity_class=CameraSettingsActivity, extras={"prefs": self.prefs, "use_webcam": self.use_webcam, "scanqr_mode": self.scanqr_mode})
311314
self.startActivity(intent)
312315

313316
def try_capture(self, event):
314317
#print("capturing camera frame")
315318
try:
316319
if self.use_webcam:
317-
self.current_cam_buffer = webcam.capture_frame(self.cam, "rgb565" if self.colormode else "grayscale")
320+
self.image_dsc.data = webcam.capture_frame(self.cam, "rgb565" if self.colormode else "grayscale")
318321
elif self.cam.frame_available():
319-
self.current_cam_buffer = self.cam.capture()
320-
321-
if self.current_cam_buffer and len(self.current_cam_buffer):
322-
# Defensive check: verify buffer size matches expected dimensions
323-
expected_size = self.width * self.height * (2 if self.colormode else 1)
324-
actual_size = len(self.current_cam_buffer)
325-
326-
if actual_size == expected_size:
327-
self.image_dsc.data = self.current_cam_buffer
328-
#self.image.invalidate() # does not work so do this:
329-
self.image.set_src(self.image_dsc)
330-
if not self.use_webcam:
331-
self.cam.free_buffer() # Free the old buffer, otherwise the camera doesn't provide a new one
332-
try:
333-
if self.keepliveqrdecoding:
334-
self.qrdecode_one()
335-
except Exception as qre:
336-
print(f"try_capture: qrdecode_one got exception: {qre}")
337-
else:
338-
print(f"Warning: Buffer size mismatch! Expected {expected_size} bytes, got {actual_size} bytes")
339-
print(f" Resolution: {self.width}x{self.height}, discarding frame")
322+
self.image_dsc.data = self.cam.capture()
340323
except Exception as e:
341324
print(f"Camera capture exception: {e}")
325+
# Display the image:
326+
#self.image.invalidate() # does not work so do this:
327+
self.image.set_src(self.image_dsc)
328+
if not self.use_webcam:
329+
self.cam.free_buffer() # Free the old buffer, otherwise the camera doesn't provide a new one
330+
try:
331+
if self.keepliveqrdecoding:
332+
self.qrdecode_one()
333+
except Exception as qre:
334+
print(f"try_capture: qrdecode_one got exception: {qre}")
342335

343336

344337
# Non-class functions:

0 commit comments

Comments
 (0)