Skip to content

Commit f53ae98

Browse files
Other method also works fast now
1 parent 9c0d8f2 commit f53ae98

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,54 @@
7878
cam.set_vflip(True)
7979

8080

81-
buffer = bytearray(width * height * 2) # RGB565 uses 2 bytes per pixel
82-
81+
# Initialize LVGL image widget
8382
image = lv.image(cont)
8483
image.align(lv.ALIGN.LEFT_MID, 0, 0)
8584
image.set_rotation(900)
85+
86+
# Create image descriptor once
8687
image_dsc = lv.image_dsc_t({
87-
"header": { "magic": lv.IMAGE_HEADER_MAGIC, "w": width, "h": height, "stride": width * 2, "cf": lv.COLOR_FORMAT.RGB565 },
88-
'data_size': len(buffer),
89-
'data': buffer
88+
"header": {
89+
"magic": lv.IMAGE_HEADER_MAGIC,
90+
"w": width,
91+
"h": height,
92+
"stride": width * 2,
93+
"cf": lv.COLOR_FORMAT.RGB565
94+
},
95+
'data_size': width * height * 2,
96+
'data': None # Will be updated per frame
9097
})
98+
99+
# Set initial image source (optional, can be set in try_capture)
91100
image.set_src(image_dsc)
92101

102+
# Variable to hold the current memoryview to prevent garbage collection
103+
current_cam_buffer = None
104+
93105
def try_capture():
106+
global current_cam_buffer
94107
if cam.frame_available():
95-
buffer[:] = bytes(cam.capture())
96-
cam.free_buffer()
97-
# Swap bytes for each 16-bit pixel
98-
# This is no longer needed because the esp-camera driver does {FORMAT_CTRL00, 0x6F}, // RGB565 (RGB) instead of {FORMAT_CTRL00, 0x61}, // RGB565 (BGR) now
99-
#img_swapped = bytearray(len(img))
100-
#for i in range(0, len(img), 2):
101-
# img_swapped[i] = img[i+1] # Swap high and low bytes
102-
# img_swapped[i+1] = img[i]
103-
# This seems needed:
104-
#image.invalidate()
105-
108+
# Get new memoryview from camera
109+
new_cam_buffer = cam.capture() # Returns memoryview
110+
# Verify buffer size
111+
#if len(new_cam_buffer) != width * height * 2:
112+
# print("Invalid buffer size:", len(new_cam_buffer))
113+
# cam.free_buffer()
114+
# return
115+
# Update image descriptor with new memoryview
116+
image_dsc.data = new_cam_buffer
117+
# Set image source to update LVGL (implicitly invalidates widget)
118+
image.set_src(image_dsc)
119+
#image.invalidate() #does not work
120+
# Free the previous buffer (if any) after setting new data
121+
if current_cam_buffer is not None:
122+
cam.free_buffer() # Free the old buffer
123+
current_cam_buffer = new_cam_buffer # Store new buffer reference
124+
125+
# Initial capture
106126
try_capture()
107127

128+
108129
import time
109130

110131
try:
@@ -115,3 +136,4 @@ def try_capture():
115136
print("Canary died, deinitializing camera...")
116137
cam.deinit()
117138

139+
cam.deinit()

0 commit comments

Comments
 (0)