2828#include "modcamera.h"
2929#include "esp_err.h"
3030#include "esp_log.h"
31+ #include "img_converters.h"
3132
3233#define TAG "ESP32_MPY_CAMERA"
3334
@@ -240,8 +241,7 @@ void mp_camera_hal_reconfigure(mp_camera_obj_t *self, mp_camera_framesize_t fram
240241 }
241242}
242243
243- mp_obj_t mp_camera_hal_capture (mp_camera_obj_t * self , int timeout_ms ) {
244- // Timeout not used at the moment
244+ mp_obj_t mp_camera_hal_capture (mp_camera_obj_t * self , mp_camera_pixformat_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 }
@@ -252,20 +252,44 @@ mp_obj_t mp_camera_hal_capture(mp_camera_obj_t *self, int timeout_ms) {
252252 ESP_LOGI (TAG , "Capturing image" );
253253 self -> captured_buffer = esp_camera_fb_get ();
254254 if (self -> captured_buffer ) {
255- if (self -> camera_config .pixel_format == PIXFORMAT_JPEG ) {
256- ESP_LOGI (TAG , "Captured image in JPEG format" );
257- return mp_obj_new_memoryview ('b' , self -> captured_buffer -> len , self -> captured_buffer -> buf );
258- } else {
259- ESP_LOGI (TAG , "Captured image in raw format" );
260- return mp_obj_new_memoryview ('b' , self -> captured_buffer -> len , self -> captured_buffer -> buf );
261- // TODO: Stub at the moment in order to return raw data, but it sould be implemented to return a Bitmap, see following circuitpython example:
262- //
263- // int width = common_hal_espcamera_camera_get_width(self);
264- // int height = common_hal_espcamera_camera_get_height(self);
265- // displayio_bitmap_t *bitmap = m_new_obj(displayio_bitmap_t);
266- // bitmap->base.type = &displayio_bitmap_type;
267- // common_hal_displayio_bitmap_construct_from_buffer(bitmap, width, height, (format == PIXFORMAT_RGB565) ? 16 : 8, (uint32_t *)(void *)result->buf, true);
268- // return bitmap;
255+ switch (out_format ) {
256+ case PIXFORMAT_JPEG :
257+ return mp_const_none ;
258+
259+ 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 );
262+ if (!out_buf ) {
263+ ESP_LOGE (TAG , "out_buf malloc failed" );
264+ return mp_const_none ;
265+ }
266+ if (fmt2rgb888 (self -> captured_buffer -> buf , self -> captured_buffer -> len , self -> captured_buffer -> format , out_buf )){
267+ esp_camera_fb_return (self -> captured_buffer );
268+ mp_obj_t result = mp_obj_new_bytes (out_buf , out_len );
269+ free (out_buf );
270+ return result ;
271+ } else {
272+ free (out_buf );
273+ return mp_const_none ;
274+ }
275+
276+ 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 );
280+ } 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+ }
292+ }
269293 }
270294 } else {
271295 esp_camera_fb_return (self -> captured_buffer );
@@ -283,6 +307,7 @@ const mp_rom_map_elem_t mp_camera_hal_pixel_format_table[] = {
283307 { MP_ROM_QSTR (MP_QSTR_YUV422 ), MP_ROM_INT (PIXFORMAT_YUV422 ) },
284308 { MP_ROM_QSTR (MP_QSTR_GRAYSCALE ), MP_ROM_INT (PIXFORMAT_GRAYSCALE ) },
285309 { MP_ROM_QSTR (MP_QSTR_RGB565 ), MP_ROM_INT (PIXFORMAT_RGB565 ) },
310+ { MP_ROM_QSTR (MP_QSTR_RGB888 ), MP_ROM_INT (PIXFORMAT_RGB888 ) },
286311};
287312
288313const mp_rom_map_elem_t mp_camera_hal_frame_size_table [] = {
0 commit comments