Skip to content

Commit f5da5ea

Browse files
committed
Validates frame size in camera configuration
Moves sensor frame size validation to a dedicated setter to ensure frame size does not exceed sensor limits before reconfiguration. Improves robustness by guaranteeing correct frame size is applied to both configuration and sensor state.
1 parent 16b2bfb commit f5da5ea

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

esp32-camera

src/modcamera.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,8 @@ void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t fram
192192
check_init(self);
193193
ESP_LOGI(TAG, "Reconfiguring camera with frame size: %d, pixel format: %d, grab mode: %d, fb count: %d", (int)frame_size, (int)pixel_format, (int)grab_mode, (int)fb_count);
194194

195-
sensor_t *sensor = esp_camera_sensor_get();
196-
camera_sensor_info_t *sensor_info = esp_camera_sensor_get_info(&sensor->id);
197-
if (frame_size > sensor_info->max_size) {
198-
mp_warning(NULL, "Frame size will be scaled down to maximal frame size supported by the camera sensor");
199-
self->camera_config.frame_size = sensor_info->max_size;
200-
} else {
201-
self->camera_config.frame_size = frame_size;
202-
}
203-
195+
// Set frame_size before deinit to ensure it's properly stored in camera_config and the sensor
196+
mp_camera_hal_set_frame_size(self, frame_size);
204197
set_check_pixel_format(self, pixel_format);
205198
set_check_grab_mode(self, grab_mode);
206199
set_check_fb_count(self, fb_count);
@@ -359,6 +352,13 @@ void mp_camera_hal_set_frame_size(mp_camera_obj_t * self, framesize_t value) {
359352
mp_raise_ValueError(MP_ERROR_TEXT("No attribute frame_size"));
360353
}
361354

355+
// Validate against sensor's maximum frame size
356+
camera_sensor_info_t *sensor_info = esp_camera_sensor_get_info(&sensor->id);
357+
if (value > sensor_info->max_size) {
358+
mp_warning(NULL, "Frame size will be scaled down to maximal frame size supported by the camera sensor");
359+
value = sensor_info->max_size;
360+
}
361+
362362
if (self->captured_buffer) {
363363
esp_camera_return_all();
364364
self->captured_buffer = NULL;

0 commit comments

Comments
 (0)