ESP32 摄像头使用详解与例程分析
在 ESP32 开发中,摄像头的应用非常广泛,从简单的图像采集到复杂的计算机视觉应用都离不开它。本文将详细介绍 ESP32 可用的摄像头类型,并结合仓库中的例程来深入分析其使用方法。
一、ESP32 可用摄像头类型对比
型号 | 最大分辨率 | 颜色类型 | 输出格式 | 镜头尺寸 |
---|---|---|---|---|
OV2640 | 1600 x 1200 | color | YUV(422/420)/YCbCr422 RGB565/555 8-bit compressed data 8/10-bit Raw RGB data | 1/4" |
OV3660 | 2048 x 1536 | color | raw RGB data RGB565/555/444 CCIR656 YCbCr422 compression | 1/5" |
OV5640 | 2592 x 1944 | color | RAW RGB RGB565/555/444 CCIR656 YUV422/420 YCbCr422 compression | 1/4" |
OV7670 | 640 x 480 | color | Raw Bayer RGB Processed Bayer RGB YUV/YCbCr422 GRB422 RGB565/555 | 1/6" |
OV7725 | 640 x 480 | color | Raw RGB GRB 422 RGB565/555/444 YCbCr 422 | 1/4" |
NT99141 | 1280 x 720 | color | YCbCr 422 RGB565/555/444 Raw CCIR656 JPEG compression | 1/4" |
GC032A | 640 x 480 | color | YUV/YCbCr422 RAW Bayer RGB565 | 1/10" |
GC0308 | 640 x 480 | color | YUV/YCbCr422 RAW Bayer RGB565 Grayscale | 1/6.5" |
GC2145 | 1600 x 1200 | color | YUV/YCbCr422 RAW Bayer RGB565 | 1/5" |
BF3005 | 640 x 480 | color | YUV/YCbCr422 RAW Bayer RGB565 | 1/4" |
BF20A6 | 640 x 480 | color | YUV/YCbCr422 RAW Bayer Only Y | 1/10" |
SC101IOT | 1280 x 720 | color | YUV/YCbCr422 Raw RGB | 1/4.2" |
SC030IOT | 640 x 480 | color | YUV/YCbCr422 RAW Bayer | 1/6.5" |
SC031GS | 640 x 480 | monochrome | RAW MONO Grayscale | 1/6" |
二、摄像头初始化例程分析
下面是一个摄像头初始化的例程:
#include "esp_camera.h"
//WROVER-KIT PIN Map
#define CAM_PIN_PWDN -1 //power down is not used
#define CAM_PIN_RESET -1 //software reset will be performed
#define CAM_PIN_XCLK 21
#define CAM_PIN_SIOD 26
#define CAM_PIN_SIOC 27
#define CAM_PIN_D7 35
#define CAM_PIN_D6 34
#define CAM_PIN_D5 39
#define CAM_PIN_D4 36
#define CAM_PIN_D3 19
#define CAM_PIN_D2 18
#define CAM_PIN_D1 5
#define CAM_PIN_D0 4
#define CAM_PIN_VSYNC 25
#define CAM_PIN_HREF 23
#define CAM_PIN_PCLK 22
camera_config_t camera_config = {
.pin_pwdn = CAM_PIN_PWDN,
.pin_reset = CAM_PIN_RESET,
.pin_xclk = CAM_PIN_XCLK,
.pin_sscb_sda = CAM_PIN_SIOD,
.pin_sscb_scl = CAM_PIN_SIOC,
.pin_d7 = CAM_PIN_D7,
.pin_d6 = CAM_PIN_D6,
.pin_d5 = CAM_PIN_D5,
.pin_d4 = CAM_PIN_D4,
.pin_d3 = CAM_PIN_D3,
.pin_d2 = CAM_PIN_D2,
.pin_d1 = CAM_PIN_D1,
.pin_d0 = CAM_PIN_D0,
.pin_vsync = CAM_PIN_VSYNC,
.pin_href = CAM_PIN_HREF,
.pin_pclk = CAM_PIN_PCLK,
.xclk_freq_hz = 20000000,//EXPERIMENTAL: Set to 16MHz on ESP32-S2 or ESP32-S3 to enable EDMA mode
.ledc_timer = LEDC_TIMER_0,
.ledc_channel = LEDC_CHANNEL_0,
.pixel_format = PIXFORMAT_JPEG,//YUV422,GRAYSCALE,RGB565,JPEG
.frame_size = FRAMESIZE_UXGA,//QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
.jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality
.fb_count = 1, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
.grab_mode = CAMERA_GRAB_WHEN_EMPTY//CAMERA_GRAB_LATEST. Sets when buffers should be filled
};
esp_err_t camera_init(){
//power up the camera if PWDN pin is defined
if(CAM_PIN_PWDN != -1){
pinMode(CAM_PIN_PWDN, OUTPUT);
digitalWrite(CAM_PIN_PWDN, LOW);
}
//initialize the camera
esp_err_t err = esp_camera_init(&camera_config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Camera Init Failed");
return err;
}
return ESP_OK;
}
代码解释
- 引脚配置:通过
camera_config_t
结构体配置摄像头的各个引脚,如pin_pwdn
、pin_reset
等,确保摄像头与 ESP32 正确连接。 - 时钟频率:
xclk_freq_hz
设置为 20000000Hz,对于 ESP32-S2 或 ESP32-S3 可以设置为 16MHz 以启用 EDMA 模式。 - 像素格式和帧大小:
pixel_format
选择 JPEG 格式,frame_size
选择 UXGA 分辨率。需要注意的是,对于 ESP32,在非 JPEG 模式下不要使用高于 QVGA 的分辨率。 - JPEG 质量和缓冲区数量:
jpeg_quality
设置为 12,对于 OV 系列摄像头,数值越低质量越高。fb_count
为 1,表示使用一个帧缓冲区,当fb_count
大于 1 时,驱动将工作在连续模式。 - 抓取模式:
grab_mode
设置为CAMERA_GRAB_WHEN_EMPTY
,表示当缓冲区为空时填充缓冲区。
三、获取摄像头帧数据例程
以下是一个简单的获取摄像头帧数据的例程:
#include "esp_camera.h"
// 假设已经完成摄像头初始化
void get_camera_frame() {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
ESP_LOGE(TAG, "Camera capture failed");
return;
}
// 在这里可以处理帧数据,例如保存为文件或进行图像处理
// 示例:打印帧数据的长度
ESP_LOGI(TAG, "Frame length: %d", fb->len);
// 释放帧缓冲区
esp_camera_fb_return(fb);
}
代码解释
esp_camera_fb_get()
:获取摄像头的一帧数据,返回camera_fb_t
结构体指针,包含帧数据的信息。- 处理帧数据:在获取到帧数据后,可以根据需求进行处理,如保存为文件、进行图像处理等。
esp_camera_fb_return(fb)
:释放帧缓冲区,避免内存泄漏。
四、总结
通过上述的摄像头类型对比和例程分析,我们可以更好地了解 ESP32 摄像头的使用方法。在实际开发中,可以根据项目需求选择合适的摄像头型号,并参考这些例程进行摄像头的初始化和数据获取,为后续的图像应用开发打下坚实的基础。
希望本文能帮助你更好地使用 ESP32 摄像头,如果你有任何问题或建议,欢迎留言交流。