基于ESP32视觉识别实战(一)
原创
大表哥吆
大表哥吆
kali笔记
2026年11月27日 12:34
甘肃
去阅读
手上有一块 ESP32-S3 N16R8,板上带一颗 OV3660 摄像头,怎么才能把画面显示出来?一起来看看吧!
; if (err != ESP_OK) { Serial.printf(\"摄像头初始化失败: 0x%x\
\", err); return false; } sensor_t* s = esp_camera_sensor_get(); if (s->id.PID == OV2640_PID || s->id.PID == OV3660_PID) { s->set_vflip(s, 1); s->set_hmirror(s, 0); } return true;} const char index_html[] PROGMEM = R\"rawliteral(<!DOCTYPE html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>ESP32-S3 摄像头</title></head><body style=\"margin:0;background:#111;text-align:center;\"><h2 style=\"color:#eee;\">ESP32-S3 实时画面</h2><img src=\"/stream\" style=\"width:100%;max-width:800px;\"></body></html>)rawliteral\"; void handleStream() { WiFiClient client = server.client(); client.println(\"HTTP/1.1 200 OK\"); client.println(\"Content-Type: multipart/x-mixed-replace; boundary=frame\"); client.println(\"Access-Control-Allow-Origin: *\"); client.println(); while (true) { camera_fb_t* fb = esp_camera_fb_get(); if (!fb) { delay(10); continue; } client.print(\"--frame\\r\
Content-Type: image/jpeg\\r\
Content-Length: \"); client.print(fb->len); client.print(\"\\r\
\\r\
\"); client.write(fb->buf, fb->len); client.print(\"\\r\
\"); esp_camera_fb_return(fb); if (!client.connected()) break; }} void setup() { Serial.begin(115200); delay(1000); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) delay(500); Serial.println(WiFi.localIP()); if (!initCamera()) while (1) delay(1000); server.on(\"/\", []() { server.send_P(200, \"text/html\", index_html); }); server.on(\"/stream\", handleStream); server.begin();} void loop() { server.handleClient(); delay(1);}
</code></pre></div>
<p>烧录后打开串口监视器(115200),看打印出来的 IP 地址,手机电脑连同一个 WiFi,浏览器打开 <code>http://这个IP</code> 就能看到实时画面了。</p>
<h1 id=)
屏幕显示
再来个把画面怼到 1.54 寸 ST7789 屏幕上。这需要装一个库:工具 → 管理库,搜 GFX Library for Arduino 装上。
接线表(8 根线,全部 3.3V,别接 5V,会烧屏):
| 屏幕引脚 | 接 ESP32-S3 |
|---|---|
| VCC | 3.3V |
| GND | GND |
| BLK(背光) | 3.3V |
| SCL(时钟) | GPIO 2 |
| SDA(数据) | GPIO 1 |
| RES(复位) | GPIO 38 |
| DC | GPIO 14 |
| CS | GPIO 21 |
代码的核心思路很妙:屏幕是 240×240,而摄像头恰好有个 FRAMESIZE_240X240 输出档位,分辨率一模一样,连缩放都省了。再把像素格式从 JPEG 换成 RGB565,直接怼屏幕:
#include \"esp_camera.h\"#include // 屏幕引脚#define TFT_SCK 2#define TFT_MOSI 1#define TFT_CS 21#define TFT_DC 14#define TFT_RST 38 // 摄像头引脚(同上,这里省略,见网页直播那份代码) Arduino_DataBus *bus = new Arduino_ESP32SPI(TFT_DC, TFT_CS, TFT_SCK, TFT_MOSI, GFX_NOT_DEFINED);Arduino_GFX *gfx = new Arduino_ST7789(bus, TFT_RST, 0, true, 240, 240, 0, 0); void setup() { Serial.begin(115200); camera_config_t config; config.ledc_channel = LEDC_CHANNEL_0; config.ledc_timer = LEDC_TIMER_0; config.pin_d0 = 11; config.pin_d1 = 9; config.pin_d2 = 8; config.pin_d3 = 10; config.pin_d4 = 12; config.pin_d5 = 18; config.pin_d6 = 17; config.pin_d7 = 16; config.pin_xclk = 15; config.pin_pclk = 13; config.pin_vsync = 6; config.pin_href = 7; config.pin_sccb_sda = 4; config.pin_sccb_scl = 5; config.pin_pwdn = -1; config.pin_reset = -1; config.xclk_freq_hz = 20000000; config.pixel_format = PIXFORMAT_RGB565; // 屏幕要 RGB565,不是 JPEG config.frame_size = FRAMESIZE_240X240; // 和屏幕分辨率一致 config.fb_count = 1; config.fb_location = CAMERA_FB_IN_PSRAM; config.grab_mode = CAMERA_GRAB_WHEN_EMPTY; if (esp_camera_init(&config) != ESP_OK) { Serial.println(\"摄像头初始化失败\"); while (1) delay(1000); } sensor_t* s = esp_camera_sensor_get(); if (s->id.PID == OV2640_PID || s->id.PID == OV3660_PID) { s->set_vflip(s, 1); s->set_hmirror(s, 0); } gfx->begin(); gfx->fillScreen(RGB565_BLACK);} void loop() { camera_fb_t* fb = esp_camera_fb_get(); if (fb) { uint16_t* buf16 = (uint16_t*)fb->buf; int n = 240 * 240; for (int i = 0; i < n; i++) { buf16[i] = (buf16[i] << 8) | (buf16[i] >> 8); // 字节序转换 } gfx->draw16bitRGBBitmap(0, 0, buf16, 240, 240); esp_camera_fb_return(fb); } else { delay(10); }}
实物效果
更多精彩文章 欢迎关注我们
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。










评论