Skip to content

Commit 57fc14d

Browse files
committed
improveents, but still not so good
1 parent a56a811 commit 57fc14d

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

src/modcamera.c

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#endif
3838

3939
// Supporting functions
40-
void raise_micropython_error_from_esp_err(esp_err_t err) {
40+
static void raise_micropython_error_from_esp_err(esp_err_t err) {
4141
switch (err) {
4242
case ESP_OK:
4343
return;
@@ -80,7 +80,7 @@ static int map(int value, int fromLow, int fromHigh, int toLow, int toHigh) {
8080
return (int)((int32_t)(value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow);
8181
}
8282

83-
static int get_mapped_jpeg_quality(int8_t quality) {
83+
static inline int get_mapped_jpeg_quality(int8_t quality) {
8484
return map(quality, 0, 100, 63, 0);
8585
}
8686

@@ -241,60 +241,80 @@ void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t fram
241241
}
242242
}
243243

244-
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, mp_camera_pixformat_t out_format) {
244+
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int8_t out_format) {
245245
if (!self->initialized) {
246246
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to capture image: Camera not initialized"));
247247
}
248248
if (self->captured_buffer) {
249249
esp_camera_fb_return(self->captured_buffer);
250250
self->captured_buffer = NULL;
251251
}
252+
253+
static size_t out_len = 0;
254+
static uint8_t *out_buf = NULL;
255+
if (out_len>0 || out_buf) {
256+
free(out_buf);
257+
out_len = 0;
258+
out_buf = NULL;
259+
}
260+
252261
ESP_LOGI(TAG, "Capturing image");
253262
self->captured_buffer = esp_camera_fb_get();
254-
if (self->captured_buffer) {
263+
if (!self->captured_buffer) {
264+
ESP_LOGE(TAG, "Failed to capture image");
265+
return mp_const_none;
266+
}
267+
268+
if (out_format >= 0 && (mp_camera_pixformat_t)out_format != self->camera_config.pixel_format) {
255269
switch (out_format) {
256270
case PIXFORMAT_JPEG:
257-
return mp_const_none;
271+
if (frame2jpg(self->captured_buffer, self->camera_config.jpeg_quality, &out_buf, &out_len)) {
272+
esp_camera_fb_return(self->captured_buffer);
273+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
274+
return result;
275+
} else {
276+
return mp_const_none;
277+
}
258278

259279
case PIXFORMAT_RGB888:
260-
size_t out_len = self->captured_buffer->width * self->captured_buffer->height * 3;
261-
uint8_t *out_buf = (uint8_t *)malloc(out_len);
280+
out_len = self->captured_buffer->width * self->captured_buffer->height * 3;
281+
out_buf = (uint8_t *)malloc(out_len);
262282
if (!out_buf) {
263283
ESP_LOGE(TAG, "out_buf malloc failed");
264284
return mp_const_none;
265285
}
266-
if (fmt2rgb888(self->captured_buffer->buf, self->captured_buffer->len, self->captured_buffer->format, out_buf)){
286+
if (fmt2rgb888(self->captured_buffer->buf, self->captured_buffer->len, self->captured_buffer->format, out_buf)) {
267287
esp_camera_fb_return(self->captured_buffer);
268-
mp_obj_t result = mp_obj_new_bytes(out_buf, out_len);
269-
free(out_buf);
288+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
270289
return result;
271290
} else {
272-
free(out_buf);
273291
return mp_const_none;
274292
}
275-
293+
276294
default:
277-
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
278-
ESP_LOGI(TAG, "Captured image in JPEG format");
279-
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
295+
ESP_LOGI(TAG, "Returning image as bitmap");
296+
if (frame2bmp(self->captured_buffer, &out_buf, &out_len)) {
297+
esp_camera_fb_return(self->captured_buffer);
298+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
299+
return result;
280300
} else {
281-
ESP_LOGI(TAG, "Returning image as bitmap");
282-
uint8_t *out = NULL;
283-
size_t out_len = 0;
284-
if (frame2bmp(self->captured_buffer, &out, &out_len)) {
285-
esp_camera_fb_return(self->captured_buffer);
286-
mp_obj_t result = mp_obj_new_bytes(out, out_len);
287-
free(out);
288-
return result;
289-
} else {
290-
return mp_const_none;
291-
}
301+
return mp_const_none;
292302
}
293303
}
304+
}
305+
306+
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
307+
ESP_LOGI(TAG, "Captured image in JPEG format");
308+
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
294309
} else {
295-
esp_camera_fb_return(self->captured_buffer);
296-
self->captured_buffer = NULL;
297-
return mp_const_none;
310+
ESP_LOGI(TAG, "Returning image as bitmap");
311+
if (frame2bmp(self->captured_buffer, &out_buf, &out_len)) {
312+
esp_camera_fb_return(self->captured_buffer);
313+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
314+
return result;
315+
} else {
316+
return mp_const_none;
317+
}
298318
}
299319
}
300320

@@ -447,6 +467,7 @@ void mp_camera_hal_set_frame_size(mp_camera_obj_t * self, framesize_t value) {
447467
self->camera_config.frame_size = value;
448468
}
449469
}
470+
450471
int mp_camera_hal_get_quality(mp_camera_obj_t * self) {
451472
if (!self->initialized) {
452473
mp_raise_ValueError(MP_ERROR_TEXT("Camera not initialized"));

src/modcamera.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ extern void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize
197197
* @param out_format Output pixelformat format.
198198
* @return Captured image as micropython object.
199199
*/
200-
extern mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, hal_camera_pixformat_t out_format);
200+
extern mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int8_t out_format);
201201

202202
/**
203203
* @brief Table mapping pixel formats API to their corresponding values at HAL.

src/modcamera_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static mp_obj_t mp_camera_make_new(const mp_obj_type_t *type, size_t n_args, siz
134134

135135
mp_camera_hal_init(self);
136136

137-
if (mp_camera_hal_capture(self, 100) == mp_const_none){
137+
if (mp_camera_hal_capture(self, -1) == mp_const_none){
138138
mp_camera_hal_deinit(self);
139139
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to capture initial frame. \
140140
Run reconfigure method or construct a new object with appropriate configuration (e.g. FrameSize)."));
@@ -150,7 +150,7 @@ static mp_obj_t mp_camera_make_new(const mp_obj_type_t *type, size_t n_args, siz
150150
// Main methods
151151
static mp_obj_t camera_capture(size_t n_args, const mp_obj_t *args){
152152
mp_camera_obj_t *self = MP_OBJ_TO_PTR(args[0]);
153-
uint8_t out_format = n_args < 2 ? MP_ROM_NONE : mp_obj_get_int(args[1]);
153+
int8_t out_format = n_args < 2 ? -1 : mp_obj_get_int(args[1]);
154154
return mp_camera_hal_capture(self, out_format);
155155
}
156156
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(camera_capture_obj, 1, 2, camera_capture);

0 commit comments

Comments
 (0)