From b468635ba1a8c3de7ec87c31eac4b3767ce521da Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Fri, 16 Oct 2020 12:36:08 +0300 Subject: [PATCH] Add support for downloading BMPs through the web server --- .../camera_web_server/main/app_httpd.c | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/examples/single_chip/camera_web_server/main/app_httpd.c b/examples/single_chip/camera_web_server/main/app_httpd.c index 801e97e..c4fa886 100644 --- a/examples/single_chip/camera_web_server/main/app_httpd.c +++ b/examples/single_chip/camera_web_server/main/app_httpd.c @@ -289,6 +289,44 @@ void enable_led(bool en) } #endif +static esp_err_t bmp_handler(httpd_req_t *req) +{ + camera_fb_t *fb = NULL; + esp_err_t res = ESP_OK; + uint64_t fr_start = esp_timer_get_time(); + fb = esp_camera_fb_get(); + if (!fb) + { + ESP_LOGE(TAG, "Camera capture failed"); + httpd_resp_send_500(req); + return ESP_FAIL; + } + + httpd_resp_set_type(req, "image/x-windows-bmp"); + httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.bmp"); + httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*"); + + char ts[32]; + snprintf(ts, 32, "%ld.%06ld", fb->timestamp.tv_sec, fb->timestamp.tv_usec); + httpd_resp_set_hdr(req, "X-Timestamp", (const char *)ts); + + + uint8_t * buf = NULL; + size_t buf_len = 0; + bool converted = frame2bmp(fb, &buf, &buf_len); + esp_camera_fb_return(fb); + if(!converted){ + ESP_LOGE(TAG, "BMP Conversion failed"); + httpd_resp_send_500(req); + return ESP_FAIL; + } + res = httpd_resp_send(req, (const char *)buf, buf_len); + free(buf); + uint64_t fr_end = esp_timer_get_time(); + ESP_LOGI(TAG, "BMP: %llums, %uB", (uint64_t)((fr_end - fr_start) / 1000), buf_len); + return res; +} + static size_t jpg_encode_stream(void *arg, size_t index, const void *data, size_t len) { jpg_chunking_t *j = (jpg_chunking_t *)arg; @@ -761,10 +799,11 @@ static esp_err_t cmd_handler(httpd_req_t *req) #endif #endif else { + ESP_LOGI(TAG, "Unknown command: %s", variable); res = -1; } - if (res) { + if (res < 0) { return httpd_resp_send_500(req); } @@ -807,7 +846,7 @@ static esp_err_t status_handler(httpd_req_t *req) p+=print_reg(p, s, reg, 0xFF); } p+=print_reg(p, s, 0x558a, 0x1FF);//9 bit - } else { + } else if(s->id.PID == OV2640_PID){ p+=print_reg(p, s, 0xd3, 0xFF); p+=print_reg(p, s, 0x111, 0xFF); p+=print_reg(p, s, 0x132, 0xFF); @@ -1075,7 +1114,7 @@ static esp_err_t monitor_handler(httpd_req_t *req) void app_httpd_main() { httpd_config_t config = HTTPD_DEFAULT_CONFIG(); - config.max_uri_handlers = 12; + config.max_uri_handlers = 16; httpd_uri_t index_uri = { .uri = "/", @@ -1107,6 +1146,12 @@ void app_httpd_main() .handler = stream_handler, .user_ctx = NULL}; + httpd_uri_t bmp_uri = { + .uri = "/bmp", + .method = HTTP_GET, + .handler = bmp_handler, + .user_ctx = NULL}; + httpd_uri_t xclk_uri = { .uri = "/xclk", .method = HTTP_GET, @@ -1179,6 +1224,7 @@ void app_httpd_main() httpd_register_uri_handler(camera_httpd, &cmd_uri); httpd_register_uri_handler(camera_httpd, &status_uri); httpd_register_uri_handler(camera_httpd, &capture_uri); + httpd_register_uri_handler(camera_httpd, &bmp_uri); httpd_register_uri_handler(camera_httpd, &xclk_uri); httpd_register_uri_handler(camera_httpd, ®_uri);