From e913d228c925046d9289e61ce719d0588b7e4ffb Mon Sep 17 00:00:00 2001 From: Ye Hang Yang Date: Fri, 30 Jul 2021 20:31:06 +0800 Subject: [PATCH] :zap: app_camera_init, app_camera_decode --- components/common/app_camera.cpp | 67 +++++++------------- components/common/include/app_camera.hpp | 32 +++++----- components/common/include/app_define.h | 26 ++++++++ examples/camera_terminal/main/app_main.cpp | 2 +- examples/camera_web_server/main/app_main.cpp | 4 +- 5 files changed, 68 insertions(+), 63 deletions(-) diff --git a/components/common/app_camera.cpp b/components/common/app_camera.cpp index d8af9f8..65823a1 100644 --- a/components/common/app_camera.cpp +++ b/components/common/app_camera.cpp @@ -8,7 +8,7 @@ static const char *TAG = "app_camera"; -void app_camera_init(framesize_t frame_size, uint8_t jpeg_quality, uint8_t fb_count) +void app_camera_init(const pixformat_t pixel_fromat, const framesize_t frame_size, const uint8_t fb_count, const uint8_t jpeg_quality) { ESP_LOGI(TAG, "Camera module is %s", CAMERA_MODULE_NAME); @@ -47,30 +47,7 @@ void app_camera_init(framesize_t frame_size, uint8_t jpeg_quality, uint8_t fb_co config.pin_pwdn = CAMERA_PIN_PWDN; config.pin_reset = CAMERA_PIN_RESET; config.xclk_freq_hz = XCLK_FREQ_HZ; -#if CONFIG_CAMERA_PIXEL_FORMAT_RGB565 - config.pixel_format = PIXFORMAT_RGB565; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_YUV422 - config.pixel_format = PIXFORMAT_YUV422; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_GRAYSCALE - config.pixel_format = PIXFORMAT_GRAYSCALE; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_JPEG - config.pixel_format = PIXFORMAT_JPEG; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_RGB888 - config.pixel_format = PIXFORMAT_RGB888; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_RAW - config.pixel_format = PIXFORMAT_RAW; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_RGB444 - config.pixel_format = PIXFORMAT_RGB444; -#endif -#if CONFIG_CAMERA_PIXEL_FORMAT_RGB555 - config.pixel_format = PIXFORMAT_RGB555; -#endif + config.pixel_format = pixel_fromat; config.frame_size = frame_size; config.jpeg_quality = jpeg_quality; config.fb_count = fb_count; @@ -94,27 +71,31 @@ void app_camera_init(framesize_t frame_size, uint8_t jpeg_quality, uint8_t fb_co } } -bool app_camera_decode(camera_fb_t *fb, uint16_t **image_ptr) +void *app_camera_decode(camera_fb_t *fb) { - assert(fb->format == PIXFORMAT_RGB565); - *image_ptr = (uint16_t *)fb->buf; - return true; -} - -bool app_camera_decode(camera_fb_t *fb, uint8_t **image_ptr) -{ - *image_ptr = (uint8_t *)dl::tool::malloc_aligned(fb->height * fb->width * 3, sizeof(uint8_t)); - if (!*image_ptr) + if (fb->format == PIXFORMAT_RGB565) { - ESP_LOGE(TAG, "malloc memory for image rgb888 failed"); - return false; + return (void *)fb->buf; } - - if (!fmt2rgb888(fb->buf, fb->len, fb->format, *image_ptr)) + else { - ESP_LOGE(TAG, "fmt2rgb888 failed"); - dl::tool::free_aligned(*image_ptr); - return false; + uint8_t *image_ptr = (uint8_t *)malloc(fb->height * fb->width * 3 * sizeof(uint8_t)); + if (image_ptr) + { + if (fmt2rgb888(fb->buf, fb->len, fb->format, image_ptr)) + { + return (void *)image_ptr; + } + else + { + ESP_LOGE(TAG, "fmt2rgb888 failed"); + dl::tool::free_aligned(image_ptr); + } + } + else + { + ESP_LOGE(TAG, "malloc memory for image rgb888 failed"); + } } - return true; + return NULL; } diff --git a/components/common/include/app_camera.hpp b/components/common/include/app_camera.hpp index 767070d..2f23e17 100644 --- a/components/common/include/app_camera.hpp +++ b/components/common/include/app_camera.hpp @@ -6,6 +6,15 @@ /** * @brief Initialize camera * + * @param pixformat One of + * - PIXFORMAT_RGB565 + * - PIXFORMAT_YUV422 + * - PIXFORMAT_GRAYSC + * - PIXFORMAT_JPEG + * - PIXFORMAT_RGB888 + * - PIXFORMAT_RAW + * - PIXFORMAT_RGB444 + * - PIXFORMAT_RGB555 * @param frame_size One of * - FRAMESIZE_96X96, // 96x96 * - FRAMESIZE_QQVGA, // 160x120 @@ -29,27 +38,16 @@ * - FRAMESIZE_WQXGA, // 2560x1600 * - FRAMESIZE_P_FHD, // 1080x1920 * - FRAMESIZE_QSXGA, // 2560x1920 - * @param jpeg_quality Quality of JPEG output. 0-63 lower means higher quality * @param fb_count Number of frame buffers to be allocated. If more than one, then each frame will be acquired (double speed) + * @param jpeg_quality Quality of JPEG output. 0-63 lower means higher quality */ -void app_camera_init(framesize_t frame_size, uint8_t jpeg_quality, uint8_t fb_count); +void app_camera_init(const pixformat_t pixel_fromat, const framesize_t frame_size, const uint8_t fb_count, const uint8_t jpeg_quality = 12); /** - * @brief Decode fb into RGB565 + * @brief Decode fb , + * - if fb->format == PIXFORMAT_RGB565, then return fb->buf + * - else, then return a new memory with RGB888, don't forget to free it * * @param fb - * @param image_ptr - * @return true - * @return false */ -bool app_camera_decode(camera_fb_t *fb, uint16_t **image_ptr); - -/** - * @brief Decode fb into RGB888 - * - * @param fb - * @param image_ptr - * @return true - * @return false - */ -bool app_camera_decode(camera_fb_t *fb, uint8_t **image_ptr); +void *app_camera_decode(camera_fb_t *fb); diff --git a/components/common/include/app_define.h b/components/common/include/app_define.h index 0119df6..277498b 100644 --- a/components/common/include/app_define.h +++ b/components/common/include/app_define.h @@ -1,6 +1,7 @@ #pragma once #include +#include "esp_camera.h" #include "sdkconfig.h" #if CONFIG_CAMERA_PIXEL_FORMAT_RGB565 @@ -181,3 +182,28 @@ #endif #define XCLK_FREQ_HZ 20000000 + +#if CONFIG_CAMERA_PIXEL_FORMAT_RGB565 +#define CAMERA_PIXEL_FORMAT PIXFORMAT_RGB565 +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_YUV422 +#define CAMERA_PIXEL_FORMAT PIXFORMAT_YUV422 +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_GRAYSCALE +#define CAMERA_PIXEL_FORMAT PIXFORMAT_GRAYSCALE +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_JPEG +#define CAMERA_PIXEL_FORMAT PIXFORMAT_JPEG +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_RGB888 +#define CAMERA_PIXEL_FORMAT PIXFORMAT_RGB888 +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_RAW +#define CAMERA_PIXEL_FORMAT PIXFORMAT_RAW +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_RGB444 +#define CAMERA_PIXEL_FORMAT PIXFORMAT_RGB444 +#endif +#if CONFIG_CAMERA_PIXEL_FORMAT_RGB555 +#define CAMERA_PIXEL_FORMAT PIXFORMAT_RGB555 +#endif diff --git a/examples/camera_terminal/main/app_main.cpp b/examples/camera_terminal/main/app_main.cpp index ad6016e..e2e9d5e 100644 --- a/examples/camera_terminal/main/app_main.cpp +++ b/examples/camera_terminal/main/app_main.cpp @@ -3,6 +3,6 @@ extern "C" void app_main() { - app_camera_init(FRAMESIZE_QVGA, 12, 2); + app_camera_init(CAMERA_PIXEL_FORMAT, FRAMESIZE_QVGA, 2); app_dl_init(); } diff --git a/examples/camera_web_server/main/app_main.cpp b/examples/camera_web_server/main/app_main.cpp index 98d117d..184f88e 100755 --- a/examples/camera_web_server/main/app_main.cpp +++ b/examples/camera_web_server/main/app_main.cpp @@ -11,9 +11,9 @@ extern "C" void app_main() app_wifi_main(); #if CONFIG_CAMERA_PIXEL_FORMAT_RGB565 - app_camera_init(FRAMESIZE_QVGA, 12, 2); + app_camera_init(CAMERA_PIXEL_FORMAT, FRAMESIZE_QVGA, 2); #else - app_camera_init(FRAMESIZE_UXGA, 12, 2); + app_camera_init(CAMERA_PIXEL_FORMAT, FRAMESIZE_UXGA, 2); #endif #if CONFIG_LED_ILLUMINATOR_ENABLED