|
| 1 | +#include "esp32_camera.h" |
| 2 | +#include "mcp_server.h" |
| 3 | +#include "display.h" |
| 4 | +#include "board.h" |
| 5 | + |
| 6 | +#include <esp_log.h> |
| 7 | +#include <esp_heap_caps.h> |
| 8 | +#include <img_converters.h> |
| 9 | +#include <cstring> |
| 10 | + |
| 11 | +#define TAG "Esp32Camera" |
| 12 | + |
| 13 | +Esp32Camera::Esp32Camera(const camera_config_t& config) { |
| 14 | + jpeg_queue_ = xQueueCreate(10, sizeof(JpegChunk)); |
| 15 | + if (jpeg_queue_ == nullptr) { |
| 16 | + ESP_LOGE(TAG, "Failed to create JPEG queue"); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // camera init |
| 21 | + esp_err_t err = esp_camera_init(&config); // 配置上面定义的参数 |
| 22 | + if (err != ESP_OK) { |
| 23 | + ESP_LOGE(TAG, "Camera init failed with error 0x%x", err); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + sensor_t *s = esp_camera_sensor_get(); // 获取摄像头型号 |
| 28 | + if (s->id.PID == GC0308_PID) { |
| 29 | + s->set_hmirror(s, 0); // 这里控制摄像头镜像 写1镜像 写0不镜像 |
| 30 | + } |
| 31 | + |
| 32 | + // 初始化预览图片的内存 |
| 33 | + memset(&preview_image_, 0, sizeof(preview_image_)); |
| 34 | + preview_image_.header.magic = LV_IMAGE_HEADER_MAGIC; |
| 35 | + preview_image_.header.cf = LV_COLOR_FORMAT_RGB565; |
| 36 | + preview_image_.header.flags = LV_IMAGE_FLAGS_ALLOCATED | LV_IMAGE_FLAGS_MODIFIABLE; |
| 37 | + |
| 38 | + if (config.frame_size == FRAMESIZE_VGA) { |
| 39 | + preview_image_.header.w = 640; |
| 40 | + preview_image_.header.h = 480; |
| 41 | + } else if (config.frame_size == FRAMESIZE_QVGA) { |
| 42 | + preview_image_.header.w = 320; |
| 43 | + preview_image_.header.h = 240; |
| 44 | + } |
| 45 | + preview_image_.header.stride = preview_image_.header.w * 2; |
| 46 | + preview_image_.data_size = preview_image_.header.w * preview_image_.header.h * 2; |
| 47 | + preview_image_.data = (uint8_t*)heap_caps_malloc(preview_image_.data_size, MALLOC_CAP_SPIRAM); |
| 48 | + if (preview_image_.data == nullptr) { |
| 49 | + ESP_LOGE(TAG, "Failed to allocate memory for preview image"); |
| 50 | + return; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +Esp32Camera::~Esp32Camera() { |
| 55 | + if (fb_) { |
| 56 | + esp_camera_fb_return(fb_); |
| 57 | + fb_ = nullptr; |
| 58 | + } |
| 59 | + if (preview_image_.data) { |
| 60 | + heap_caps_free((void*)preview_image_.data); |
| 61 | + preview_image_.data = nullptr; |
| 62 | + } |
| 63 | + esp_camera_deinit(); |
| 64 | + |
| 65 | + if (jpeg_queue_ != nullptr) { |
| 66 | + vQueueDelete(jpeg_queue_); |
| 67 | + jpeg_queue_ = nullptr; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void Esp32Camera::SetExplainUrl(const std::string& url, const std::string& token) { |
| 72 | + explain_url_ = url; |
| 73 | + explain_token_ = token; |
| 74 | +} |
| 75 | + |
| 76 | +bool Esp32Camera::Capture() { |
| 77 | + if (preview_thread_.joinable()) { |
| 78 | + preview_thread_.join(); |
| 79 | + } |
| 80 | + if (encoder_thread_.joinable()) { |
| 81 | + encoder_thread_.join(); |
| 82 | + } |
| 83 | + |
| 84 | + int frames_to_get = 2; |
| 85 | + // Try to get a stable frame |
| 86 | + for (int i = 0; i < frames_to_get; i++) { |
| 87 | + if (fb_ != nullptr) { |
| 88 | + esp_camera_fb_return(fb_); |
| 89 | + } |
| 90 | + fb_ = esp_camera_fb_get(); |
| 91 | + if (fb_ == nullptr) { |
| 92 | + ESP_LOGE(TAG, "Camera capture failed"); |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + preview_thread_ = std::thread([this]() { |
| 98 | + // 显示预览图片 |
| 99 | + auto display = Board::GetInstance().GetDisplay(); |
| 100 | + if (display != nullptr) { |
| 101 | + auto src = (uint16_t*)fb_->buf; |
| 102 | + auto dst = (uint16_t*)preview_image_.data; |
| 103 | + size_t pixel_count = fb_->len / 2; |
| 104 | + for (size_t i = 0; i < pixel_count; i++) { |
| 105 | + // 交换每个16位字内的字节 |
| 106 | + dst[i] = __builtin_bswap16(src[i]); |
| 107 | + } |
| 108 | + display->SetPreviewImage(&preview_image_); |
| 109 | + } |
| 110 | + }); |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +std::string Esp32Camera::Explain(const std::string& question) { |
| 115 | + if (explain_url_.empty() || explain_token_.empty()) { |
| 116 | + return "{\"success\": false, \"message\": \"Image explain URL or token is not set\"}"; |
| 117 | + } |
| 118 | + |
| 119 | + // We spawn a thread to encode the image to JPEG |
| 120 | + encoder_thread_ = std::thread([this]() { |
| 121 | + frame2jpg_cb(fb_, 80, [](void* arg, size_t index, const void* data, size_t len) -> unsigned int { |
| 122 | + auto jpeg_queue = (QueueHandle_t)arg; |
| 123 | + JpegChunk chunk = { |
| 124 | + .data = (uint8_t*)heap_caps_malloc(len, MALLOC_CAP_SPIRAM), |
| 125 | + .len = len |
| 126 | + }; |
| 127 | + memcpy(chunk.data, data, len); |
| 128 | + xQueueSend(jpeg_queue, &chunk, portMAX_DELAY); |
| 129 | + return len; |
| 130 | + }, jpeg_queue_); |
| 131 | + }); |
| 132 | + |
| 133 | + auto http = Board::GetInstance().CreateHttp(); |
| 134 | + // 构造multipart/form-data请求体 |
| 135 | + std::string boundary = "----ESP32_CAMERA_BOUNDARY"; |
| 136 | + |
| 137 | + // 构造question字段 |
| 138 | + std::string question_field; |
| 139 | + question_field += "--" + boundary + "\r\n"; |
| 140 | + question_field += "Content-Disposition: form-data; name=\"question\"\r\n"; |
| 141 | + question_field += "\r\n"; |
| 142 | + question_field += question + "\r\n"; |
| 143 | + |
| 144 | + // 构造文件字段头部 |
| 145 | + std::string file_header; |
| 146 | + file_header += "--" + boundary + "\r\n"; |
| 147 | + file_header += "Content-Disposition: form-data; name=\"file\"; filename=\"camera.jpg\"\r\n"; |
| 148 | + file_header += "Content-Type: image/jpeg\r\n"; |
| 149 | + file_header += "\r\n"; |
| 150 | + |
| 151 | + // 构造尾部 |
| 152 | + std::string multipart_footer; |
| 153 | + multipart_footer += "\r\n--" + boundary + "--\r\n"; |
| 154 | + |
| 155 | + // 配置HTTP客户端,使用分块传输编码 |
| 156 | + http->SetHeader("Authorization", "Bearer " + explain_token_); |
| 157 | + http->SetHeader("Content-Type", "multipart/form-data; boundary=" + boundary); |
| 158 | + http->SetHeader("Transfer-Encoding", "chunked"); |
| 159 | + if (!http->Open("POST", explain_url_)) { |
| 160 | + ESP_LOGE(TAG, "Failed to connect to explain URL"); |
| 161 | + // Clear the queue |
| 162 | + encoder_thread_.join(); |
| 163 | + JpegChunk chunk; |
| 164 | + while (xQueueReceive(jpeg_queue_, &chunk, portMAX_DELAY) == pdPASS) { |
| 165 | + if (chunk.data != nullptr) { |
| 166 | + heap_caps_free(chunk.data); |
| 167 | + } else { |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + return "{\"success\": false, \"message\": \"Failed to connect to explain URL\"}"; |
| 172 | + } |
| 173 | + |
| 174 | + // 第一块:question字段 |
| 175 | + http->Write(question_field.c_str(), question_field.size()); |
| 176 | + |
| 177 | + // 第二块:文件字段头部 |
| 178 | + http->Write(file_header.c_str(), file_header.size()); |
| 179 | + |
| 180 | + // 第三块:JPEG数据 |
| 181 | + size_t total_sent = 0; |
| 182 | + while (true) { |
| 183 | + JpegChunk chunk; |
| 184 | + if (xQueueReceive(jpeg_queue_, &chunk, portMAX_DELAY) != pdPASS) { |
| 185 | + ESP_LOGE(TAG, "Failed to receive JPEG chunk"); |
| 186 | + break; |
| 187 | + } |
| 188 | + if (chunk.data == nullptr) { |
| 189 | + break; // The last chunk |
| 190 | + } |
| 191 | + http->Write((const char*)chunk.data, chunk.len); |
| 192 | + total_sent += chunk.len; |
| 193 | + heap_caps_free(chunk.data); |
| 194 | + } |
| 195 | + |
| 196 | + // 第四块:multipart尾部 |
| 197 | + http->Write(multipart_footer.c_str(), multipart_footer.size()); |
| 198 | + |
| 199 | + // 结束块 |
| 200 | + http->Write("", 0); |
| 201 | + |
| 202 | + if (http->GetStatusCode() != 200) { |
| 203 | + ESP_LOGE(TAG, "Failed to upload photo, status code: %d", http->GetStatusCode()); |
| 204 | + return "{\"success\": false, \"message\": \"Failed to upload photo\"}"; |
| 205 | + } |
| 206 | + |
| 207 | + std::string result = http->ReadAll(); |
| 208 | + http->Close(); |
| 209 | + |
| 210 | + ESP_LOGI(TAG, "Explain image size=%dx%d, compressed size=%d, question=%s\n%s", fb_->width, fb_->height, total_sent, question.c_str(), result.c_str()); |
| 211 | + return result; |
| 212 | +} |
0 commit comments