@@ -65,7 +65,6 @@ Esp32Camera::Esp32Camera(const camera_config_t& config) {
65
65
ESP_LOGE (TAG, " Failed to allocate memory for preview image" );
66
66
return ;
67
67
}
68
-
69
68
}
70
69
71
70
Esp32Camera::~Esp32Camera () {
@@ -116,27 +115,14 @@ bool Esp32Camera::Capture() {
116
115
// 显示预览图片
117
116
auto display = Board::GetInstance ().GetDisplay ();
118
117
if (display != nullptr ) {
119
- // 检查摄像头输出格式
120
- if (fb_->format == PIXFORMAT_JPEG) {
121
- // JPEG格式需要先解码为RGB565
122
- // 分配足够的缓冲区用于RGB565数据
123
- bool converted = jpg2rgb565 (fb_->buf , fb_->len , preview_image_.data , JPG_SCALE_NONE);
124
- if (converted) {
125
- display->SetPreviewImage (&preview_image_);
126
- } else {
127
- ESP_LOGE (TAG, " Failed to convert JPEG to RGB565 for preview" );
128
- }
129
- } else {
130
- // RGB565等格式需要进行字节交换处理
131
- auto src = (uint16_t *)fb_->buf ;
132
- auto dst = (uint16_t *)preview_image_.data ;
133
- size_t pixel_count = fb_->len / 2 ;
134
- for (size_t i = 0 ; i < pixel_count; i++) {
135
- // 交换每个16位字内的字节
136
- dst[i] = __builtin_bswap16 (src[i]);
137
- }
138
- display->SetPreviewImage (&preview_image_);
118
+ auto src = (uint16_t *)fb_->buf ;
119
+ auto dst = (uint16_t *)preview_image_.data ;
120
+ size_t pixel_count = fb_->len / 2 ;
121
+ for (size_t i = 0 ; i < pixel_count; i++) {
122
+ // 交换每个16位字内的字节
123
+ dst[i] = __builtin_bswap16 (src[i]);
139
124
}
125
+ display->SetPreviewImage (&preview_image_);
140
126
}
141
127
return true ;
142
128
}
@@ -202,56 +188,26 @@ std::string Esp32Camera::Explain(const std::string& question) {
202
188
return " {\" success\" : false, \" message\" : \" Image explain URL or token is not set\" }" ;
203
189
}
204
190
205
- QueueHandle_t jpeg_queue = nullptr ;
206
- // If the format is not JPEG, we need to convert it to JPEG first
207
- if (fb_->format != PIXFORMAT_JPEG) {
208
- // 创建局部的 JPEG 队列, 40 entries is about to store 512 * 40 = 20480 bytes of JPEG data
209
- jpeg_queue = xQueueCreate (40 , sizeof (JpegChunk));
210
- if (jpeg_queue == nullptr ) {
211
- ESP_LOGE (TAG, " Failed to create JPEG queue" );
212
- return " {\" success\" : false, \" message\" : \" Failed to create JPEG queue\" }" ;
213
- }
191
+ // 创建局部的 JPEG 队列, 40 entries is about to store 512 * 40 = 20480 bytes of JPEG data
192
+ QueueHandle_t jpeg_queue = xQueueCreate (40 , sizeof (JpegChunk));
193
+ if (jpeg_queue == nullptr ) {
194
+ ESP_LOGE (TAG, " Failed to create JPEG queue" );
195
+ return " {\" success\" : false, \" message\" : \" Failed to create JPEG queue\" }" ;
196
+ }
214
197
215
- // We spawn a thread to encode the image to JPEG
216
- encoder_thread_ = std::thread ([this , jpeg_queue]() {
217
- frame2jpg_cb (fb_, 80 , [](void * arg, size_t index, const void * data, size_t len) -> unsigned int {
218
- auto jpeg_queue = (QueueHandle_t)arg;
219
- JpegChunk chunk = {
220
- .data = (uint8_t *)heap_caps_aligned_alloc (16 , len, MALLOC_CAP_SPIRAM),
221
- .len = len
222
- };
223
- if (chunk.data != nullptr ) {
224
- memcpy (chunk.data , data, len);
225
- xQueueSend (jpeg_queue, &chunk, portMAX_DELAY);
226
- }
227
- return len;
228
- }, jpeg_queue);
229
- // Add a null chunk to indicate the end of the queue
230
- JpegChunk null_chunk = { .data = nullptr , .len = 0 };
231
- xQueueSend (jpeg_queue, &null_chunk, portMAX_DELAY);
232
- });
233
- } else {
234
- // JPEG format, we can send it directly
235
- // To keep the logic consistent, we still use a queue to send the data
236
- jpeg_queue = xQueueCreate (2 , sizeof (JpegChunk));
237
- if (jpeg_queue == nullptr ) {
238
- ESP_LOGE (TAG, " Failed to create JPEG queue" );
239
- return " {\" success\" : false, \" message\" : \" Failed to create JPEG queue\" }" ;
240
- }
241
- JpegChunk chunk = {
242
- .data = (uint8_t *)heap_caps_aligned_alloc (16 , fb_->len , MALLOC_CAP_SPIRAM),
243
- .len = fb_->len
244
- };
245
- if (chunk.data != nullptr ) {
246
- memcpy (chunk.data , fb_->buf , fb_->len );
198
+ // We spawn a thread to encode the image to JPEG
199
+ encoder_thread_ = std::thread ([this , jpeg_queue]() {
200
+ frame2jpg_cb (fb_, 80 , [](void * arg, size_t index, const void * data, size_t len) -> unsigned int {
201
+ auto jpeg_queue = (QueueHandle_t)arg;
202
+ JpegChunk chunk = {
203
+ .data = (uint8_t *)heap_caps_aligned_alloc (16 , len, MALLOC_CAP_SPIRAM),
204
+ .len = len
205
+ };
206
+ memcpy (chunk.data , data, len);
247
207
xQueueSend (jpeg_queue, &chunk, portMAX_DELAY);
248
- } else {
249
- ESP_LOGE (TAG, " Failed to allocate memory for JPEG chunk" );
250
- }
251
- // Add a null chunk to indicate the end of the queue
252
- JpegChunk null_chunk = { .data = nullptr , .len = 0 };
253
- xQueueSend (jpeg_queue, &null_chunk, portMAX_DELAY);
254
- }
208
+ return len;
209
+ }, jpeg_queue);
210
+ });
255
211
256
212
auto network = Board::GetInstance ().GetNetwork ();
257
213
auto http = network->CreateHttp (3 );
@@ -269,9 +225,7 @@ std::string Esp32Camera::Explain(const std::string& question) {
269
225
if (!http->Open (" POST" , explain_url_)) {
270
226
ESP_LOGE (TAG, " Failed to connect to explain URL" );
271
227
// Clear the queue
272
- if (encoder_thread_.joinable ()) {
273
- encoder_thread_.join ();
274
- }
228
+ encoder_thread_.join ();
275
229
JpegChunk chunk;
276
230
while (xQueueReceive (jpeg_queue, &chunk, portMAX_DELAY) == pdPASS) {
277
231
if (chunk.data != nullptr ) {
@@ -319,9 +273,7 @@ std::string Esp32Camera::Explain(const std::string& question) {
319
273
heap_caps_free (chunk.data );
320
274
}
321
275
// Wait for the encoder thread to finish
322
- if (encoder_thread_.joinable ()) {
323
- encoder_thread_.join ();
324
- }
276
+ encoder_thread_.join ();
325
277
// 清理队列
326
278
vQueueDelete (jpeg_queue);
327
279
0 commit comments