Skip to content

Commit 1f35095

Browse files
committed
improveents, but still not so good
1 parent 3e7588d commit 1f35095

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
@@ -43,7 +43,7 @@
4343
#endif
4444

4545
// Supporting functions
46-
void raise_micropython_error_from_esp_err(esp_err_t err) {
46+
static void raise_micropython_error_from_esp_err(esp_err_t err) {
4747
switch (err) {
4848
case ESP_OK:
4949
return;
@@ -86,7 +86,7 @@ static int map(int value, int fromLow, int fromHigh, int toLow, int toHigh) {
8686
return (int)((int32_t)(value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow);
8787
}
8888

89-
static int get_mapped_jpeg_quality(int8_t quality) {
89+
static inline int get_mapped_jpeg_quality(int8_t quality) {
9090
return map(quality, 0, 100, 63, 0);
9191
}
9292

@@ -247,60 +247,80 @@ void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t fram
247247
}
248248
}
249249

250-
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, mp_camera_pixformat_t out_format) {
250+
mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int8_t out_format) {
251251
if (!self->initialized) {
252252
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Failed to capture image: Camera not initialized"));
253253
}
254254
if (self->captured_buffer) {
255255
esp_camera_fb_return(self->captured_buffer);
256256
self->captured_buffer = NULL;
257257
}
258+
259+
static size_t out_len = 0;
260+
static uint8_t *out_buf = NULL;
261+
if (out_len>0 || out_buf) {
262+
free(out_buf);
263+
out_len = 0;
264+
out_buf = NULL;
265+
}
266+
258267
ESP_LOGI(TAG, "Capturing image");
259268
self->captured_buffer = esp_camera_fb_get();
260-
if (self->captured_buffer) {
269+
if (!self->captured_buffer) {
270+
ESP_LOGE(TAG, "Failed to capture image");
271+
return mp_const_none;
272+
}
273+
274+
if (out_format >= 0 && (mp_camera_pixformat_t)out_format != self->camera_config.pixel_format) {
261275
switch (out_format) {
262276
case PIXFORMAT_JPEG:
263-
return mp_const_none;
277+
if (frame2jpg(self->captured_buffer, self->camera_config.jpeg_quality, &out_buf, &out_len)) {
278+
esp_camera_fb_return(self->captured_buffer);
279+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
280+
return result;
281+
} else {
282+
return mp_const_none;
283+
}
264284

265285
case PIXFORMAT_RGB888:
266-
size_t out_len = self->captured_buffer->width * self->captured_buffer->height * 3;
267-
uint8_t *out_buf = (uint8_t *)malloc(out_len);
286+
out_len = self->captured_buffer->width * self->captured_buffer->height * 3;
287+
out_buf = (uint8_t *)malloc(out_len);
268288
if (!out_buf) {
269289
ESP_LOGE(TAG, "out_buf malloc failed");
270290
return mp_const_none;
271291
}
272-
if (fmt2rgb888(self->captured_buffer->buf, self->captured_buffer->len, self->captured_buffer->format, out_buf)){
292+
if (fmt2rgb888(self->captured_buffer->buf, self->captured_buffer->len, self->captured_buffer->format, out_buf)) {
273293
esp_camera_fb_return(self->captured_buffer);
274-
mp_obj_t result = mp_obj_new_bytes(out_buf, out_len);
275-
free(out_buf);
294+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
276295
return result;
277296
} else {
278-
free(out_buf);
279297
return mp_const_none;
280298
}
281-
299+
282300
default:
283-
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
284-
ESP_LOGI(TAG, "Captured image in JPEG format");
285-
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
301+
ESP_LOGI(TAG, "Returning image as bitmap");
302+
if (frame2bmp(self->captured_buffer, &out_buf, &out_len)) {
303+
esp_camera_fb_return(self->captured_buffer);
304+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
305+
return result;
286306
} else {
287-
ESP_LOGI(TAG, "Returning image as bitmap");
288-
uint8_t *out = NULL;
289-
size_t out_len = 0;
290-
if (frame2bmp(self->captured_buffer, &out, &out_len)) {
291-
esp_camera_fb_return(self->captured_buffer);
292-
mp_obj_t result = mp_obj_new_bytes(out, out_len);
293-
free(out);
294-
return result;
295-
} else {
296-
return mp_const_none;
297-
}
307+
return mp_const_none;
298308
}
299309
}
310+
}
311+
312+
if (self->camera_config.pixel_format == PIXFORMAT_JPEG) {
313+
ESP_LOGI(TAG, "Captured image in JPEG format");
314+
return mp_obj_new_memoryview('b', self->captured_buffer->len, self->captured_buffer->buf);
300315
} else {
301-
esp_camera_fb_return(self->captured_buffer);
302-
self->captured_buffer = NULL;
303-
return mp_const_none;
316+
ESP_LOGI(TAG, "Returning image as bitmap");
317+
if (frame2bmp(self->captured_buffer, &out_buf, &out_len)) {
318+
esp_camera_fb_return(self->captured_buffer);
319+
mp_obj_t result = mp_obj_new_memoryview('b', out_len, out_buf);
320+
return result;
321+
} else {
322+
return mp_const_none;
323+
}
304324
}
305325
}
306326

@@ -453,6 +473,7 @@ void mp_camera_hal_set_frame_size(mp_camera_obj_t * self, framesize_t value) {
453473
self->camera_config.frame_size = value;
454474
}
455475
}
476+
456477
int mp_camera_hal_get_quality(mp_camera_obj_t * self) {
457478
if (!self->initialized) {
458479
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)