示例代码
app_main.c
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "app_peripherals.h"
#include "esp_code_scanner.h"
static const char *TAG = "APP_CODE_SCANNER";
static void decode_task()
{
if(ESP_OK != app_camera_init()) {
vTaskDelete(NULL);
return;
}
#ifdef LCD_CONTROLLER
int USE_LCD = 0;
scr_driver_t g_lcd;
if (ESP_OK == app_lcd_init(&g_lcd))
USE_LCD = 1;
#endif
camera_fb_t *fb = NULL;
int64_t time1, time2;
while (1)
{
fb = esp_camera_fb_get();
if(fb == NULL){
ESP_LOGI(TAG, "camera get failed\n");
continue;
}
time1 = esp_timer_get_time();
// Decode Progress
esp_image_scanner_t *esp_scn = esp_code_scanner_create();
esp_code_scanner_config_t config = {ESP_CODE_SCANNER_MODE_FAST, ESP_CODE_SCANNER_IMAGE_RGB565, fb->width, fb->height};
esp_code_scanner_set_config(esp_scn, config);
int decoded_num = esp_code_scanner_scan_image(esp_scn, fb->buf);
if(decoded_num){
esp_code_scanner_symbol_t result = esp_code_scanner_result(esp_scn);
time2 = esp_timer_get_time();
ESP_LOGI(TAG, "Decode time in %lld ms.", (time2 - time1) / 1000);
ESP_LOGI(TAG, "Decoded %s symbol \"%s\"\n", result.type_name, result.data);
}
esp_code_scanner_destroy(esp_scn);
#ifdef LCD_CONTROLLER
if(USE_LCD){
g_lcd.draw_bitmap(0, 0, fb->width, fb->height, (uint16_t *)fb->buf);
}
#endif
esp_camera_fb_return(fb);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void app_main()
{
xTaskCreatePinnedToCore(decode_task, TAG, 4 * 1024, NULL, 6, NULL, 0);
}
app_peripherals.c
#include "app_peripherals.h"
#include "esp_log.h"
#include "esp_system.h"
#include "st7789.h"
static const char *TAG = "app_peripherals";
esp_err_t app_camera_init()
{
ESP_LOGI(TAG, "Camera module is %s", CAMERA_MODULE_NAME);
#if CONFIG_CAMERA_MODULE_ESP_EYE || CONFIG_CAMERA_MODULE_ESP32_CAM_BOARD
/* IO13, IO14 is designed for JTAG by default,
* to use it as generalized input,
* firstly declair it as pullup input */
gpio_config_t conf;
conf.mode = GPIO_MODE_INPUT;
conf.pull_up_en = GPIO_PULLUP_ENABLE;
conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
conf.intr_type = GPIO_INTR_DISABLE;
conf.pin_bit_mask = 1LL << 13;
gpio_config(&conf);
conf.pin_bit_mask = 1LL << 14;
gpio_config(&conf);
#endif
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = CAMERA_PIN_D0;
config.pin_d1 = CAMERA_PIN_D1;
config.pin_d2 = CAMERA_PIN_D2;
config.pin_d3 = CAMERA_PIN_D3;
config.pin_d4 = CAMERA_PIN_D4;
config.pin_d5 = CAMERA_PIN_D5;
config.pin_d6 = CAMERA_PIN_D6;
config.pin_d7 = CAMERA_PIN_D7;
config.pin_xclk = CAMERA_PIN_XCLK;
config.pin_pclk = CAMERA_PIN_PCLK;
config.pin_vsync = CAMERA_PIN_VSYNC;
config.pin_href = CAMERA_PIN_HREF;
config.pin_sscb_sda = CAMERA_PIN_SIOD;
config.pin_sscb_scl = CAMERA_PIN_SIOC;
config.pin_pwdn = CAMERA_PIN_PWDN;
config.pin_reset = CAMERA_PIN_RESET;
config.xclk_freq_hz = XCLK_FREQ_HZ;
config.pixel_format = CAMERA_PIXFORMAT;
config.frame_size = CAMERA_FRAME_SIZE;
config.jpeg_quality = 12;
config.fb_count = CAMERA_FB_COUNT;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
// camera init
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Camera init failed with error 0x%x", err);
return ESP_FAIL;
}
sensor_t *s = esp_camera_sensor_get();
if (s->id.PID == OV3660_PID || s->id.PID == OV2640_PID)
s->set_vflip(s, 1); //flip it back
else if (s->id.PID == GC0308_PID){
s->set_hmirror(s, 0);
}
else if (s->id.PID == GC032A_PID){
s->set_vflip(s, 1);
// s->set_hmirror(s, 0); //something wrong
}
if (s->id.PID == OV3660_PID)
{
s->set_brightness(s, 2);
s->set_contrast(s, 3);
}
return ESP_OK;
}
#ifdef LCD_CONTROLLER
esp_err_t app_lcd_init(scr_driver_t *g_lcd){
static scr_info_t g_lcd_info;
spi_config_t bus_conf = {
.miso_io_num = LCD_MISO,
.mosi_io_num = LCD_MOSI,
.sclk_io_num = LCD_SCLK,
.max_transfer_sz = 2 * LCD_WIDTH * LCD_HEIGHT + 10,
};
spi_bus_handle_t spi_bus = spi_bus_create(SPI2_HOST, &bus_conf);
scr_interface_spi_config_t spi_lcd_cfg = {
.spi_bus = spi_bus,
.pin_num_cs = LCD_CS,
.pin_num_dc = LCD_DC,
.clk_freq = 40 * 1000000,
.swap_data = 0,
};
scr_interface_driver_t *iface_drv;
scr_interface_create(SCREEN_IFACE_SPI, &spi_lcd_cfg, &iface_drv);
esp_err_t ret = scr_find_driver(LCD_CONTROLLER, g_lcd);
if (ESP_OK != ret)
{
ESP_LOGE(TAG, "screen find failed");
return ESP_FAIL;
}
scr_controller_config_t lcd_cfg = {
.interface_drv = iface_drv,
.pin_num_rst = LCD_RST,
.pin_num_bckl = LCD_BCKL,
.rst_active_level = 0,
.bckl_active_level = 0,
.offset_hor = 0,
.offset_ver = 0,
.width = LCD_WIDTH,
.height = LCD_HEIGHT,
.rotate = LCD_ROTATE,
};
ret = g_lcd->init(&lcd_cfg);
if (ESP_OK != ret)
{
ESP_LOGE(TAG, "screen initialize failed");
return ESP_FAIL;
}
#if CONFIG_CAMERA_MODULE_ESP_S2_KALUGA
lcd_st7789_set_invert(false);
#endif
g_lcd->get_info(&g_lcd_info);
ESP_LOGI(TAG, "Screen name:%s | width:%d | height:%d", g_lcd_info.name, g_lcd_info.width, g_lcd_info.height);
return ESP_OK;
}
#endif
app_peripherals.h
#pragma once
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "esp_camera.h"
#include "screen_driver.h"
// camera pins
#if CONFIG_CAMERA_MODULE_WROVER_KIT
#define CAMERA_MODULE_NAME "Wrover Kit"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 21
#define CAMERA_PIN_SIOD 26
#define CAMERA_PIN_SIOC 27
#define CAMERA_PIN_D7 35
#define CAMERA_PIN_D6 34
#define CAMERA_PIN_D5 39
#define CAMERA_PIN_D4 36
#define CAMERA_PIN_D3 19
#define CAMERA_PIN_D2 18
#define CAMERA_PIN_D1 5
#define CAMERA_PIN_D0 4
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 23
#define CAMERA_PIN_PCLK 22
#elif CONFIG_CAMERA_MODULE_ESP_EYE
#define CAMERA_MODULE_NAME "ESP-EYE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 4
#define CAMERA_PIN_SIOD 18
#define CAMERA_PIN_SIOC 23
#define CAMERA_PIN_D7 36
#define CAMERA_PIN_D6 37
#define CAMERA_PIN_D5 38
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 35
#define CAMERA_PIN_D2 14
#define CAMERA_PIN_D1 13
#define CAMERA_PIN_D0 34
#define CAMERA_PIN_VSYNC 5
#define CAMERA_PIN_HREF 27
#define CAMERA_PIN_PCLK 25
#elif CONFIG_CAMERA_MODULE_ESP_S2_KALUGA
#define CAMERA_MODULE_NAME "ESP-S2-KALUGA"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 1
#define CAMERA_PIN_SIOD 8
#define CAMERA_PIN_SIOC 7
#define CAMERA_PIN_D7 38
#define CAMERA_PIN_D6 21
#define CAMERA_PIN_D5 40
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 42
#define CAMERA_PIN_D2 41
#define CAMERA_PIN_D1 37
#define CAMERA_PIN_D0 36
#define CAMERA_PIN_VSYNC 2
#define CAMERA_PIN_HREF 3
#define CAMERA_PIN_PCLK 33
#elif CONFIG_CAMERA_MODULE_ESP_S3_EYE
#define CAMERA_MODULE_NAME "ESP-S3-EYE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_VSYNC 6
#define CAMERA_PIN_HREF 7
#define CAMERA_PIN_PCLK 13
#define CAMERA_PIN_XCLK 15
#define CAMERA_PIN_SIOD 4
#define CAMERA_PIN_SIOC 5
#define CAMERA_PIN_D0 11
#define CAMERA_PIN_D1 9
#define CAMERA_PIN_D2 8
#define CAMERA_PIN_D3 10
#define CAMERA_PIN_D4 12
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D6 17
#define CAMERA_PIN_D7 16
#elif CONFIG_CAMERA_MODULE_ESP32_CAM_BOARD
#define CAMERA_MODULE_NAME "ESP-DEVCAM"
#define CAMERA_PIN_PWDN 32
#define CAMERA_PIN_RESET 33
#define CAMERA_PIN_XCLK 4
#define CAMERA_PIN_SIOD 18
#define CAMERA_PIN_SIOC 23
#define CAMERA_PIN_D7 36
#define CAMERA_PIN_D6 19
#define CAMERA_PIN_D5 21
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 35
#define CAMERA_PIN_D2 14
#define CAMERA_PIN_D1 13
#define CAMERA_PIN_D0 34
#define CAMERA_PIN_VSYNC 5
#define CAMERA_PIN_HREF 27
#define CAMERA_PIN_PCLK 25
#elif CONFIG_CAMERA_MODULE_M5STACK_PSRAM
#define CAMERA_MODULE_NAME "M5STACK-PSRAM"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET 15
#define CAMERA_PIN_XCLK 27
#define CAMERA_PIN_SIOD 25
#define CAMERA_PIN_SIOC 23
#define CAMERA_PIN_D7 19
#define CAMERA_PIN_D6 36
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 5
#define CAMERA_PIN_D2 34
#define CAMERA_PIN_D1 35
#define CAMERA_PIN_D0 32
#define CAMERA_PIN_VSYNC 22
#define CAMERA_PIN_HREF 26
#define CAMERA_PIN_PCLK 21
#elif CONFIG_CAMERA_MODULE_M5STACK_WIDE
#define CAMERA_MODULE_NAME "M5STACK-WIDE"
#define CAMERA_PIN_PWDN -1
#define CAMERA_PIN_RESET 15
#define CAMERA_PIN_XCLK 27
#define CAMERA_PIN_SIOD 22
#define CAMERA_PIN_SIOC 23
#define CAMERA_PIN_D7 19
#define CAMERA_PIN_D6 36
#define CAMERA_PIN_D5 18
#define CAMERA_PIN_D4 39
#define CAMERA_PIN_D3 5
#define CAMERA_PIN_D2 34
#define CAMERA_PIN_D1 35
#define CAMERA_PIN_D0 32
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 26
#define CAMERA_PIN_PCLK 21
#elif CONFIG_CAMERA_MODULE_AI_THINKER
#define CAMERA_MODULE_NAME "AI-THINKER"
#define CAMERA_PIN_PWDN 32
#define CAMERA_PIN_RESET -1
#define CAMERA_PIN_XCLK 0
#define CAMERA_PIN_SIOD 26
#define CAMERA_PIN_SIOC 27
#define CAMERA_PIN_D7 35
#define CAMERA_PIN_D6 34
#define CAMERA_PIN_D5 39
#define CAMERA_PIN_D4 36
#define CAMERA_PIN_D3 21
#define CAMERA_PIN_D2 19
#define CAMERA_PIN_D1 18
#define CAMERA_PIN_D0 5
#define CAMERA_PIN_VSYNC 25
#define CAMERA_PIN_HREF 23
#define CAMERA_PIN_PCLK 22
#elif CONFIG_CAMERA_MODULE_CUSTOM
#define CAMERA_MODULE_NAME "CUSTOM"
#define CAMERA_PIN_PWDN CONFIG_CAMERA_PIN_PWDN
#define CAMERA_PIN_RESET CONFIG_CAMERA_PIN_RESET
#define CAMERA_PIN_XCLK CONFIG_CAMERA_PIN_XCLK
#define CAMERA_PIN_SIOD CONFIG_CAMERA_PIN_SIOD
#define CAMERA_PIN_SIOC CONFIG_CAMERA_PIN_SIOC
#define CAMERA_PIN_D7 CONFIG_CAMERA_PIN_Y9
#define CAMERA_PIN_D6 CONFIG_CAMERA_PIN_Y8
#define CAMERA_PIN_D5 CONFIG_CAMERA_PIN_Y7
#define CAMERA_PIN_D4 CONFIG_CAMERA_PIN_Y6
#define CAMERA_PIN_D3 CONFIG_CAMERA_PIN_Y5
#define CAMERA_PIN_D2 CONFIG_CAMERA_PIN_Y4
#define CAMERA_PIN_D1 CONFIG_CAMERA_PIN_Y3
#define CAMERA_PIN_D0 CONFIG_CAMERA_PIN_Y2
#define CAMERA_PIN_VSYNC CONFIG_CAMERA_PIN_VSYNC
#define CAMERA_PIN_HREF CONFIG_CAMERA_PIN_HREF
#define CAMERA_PIN_PCLK CONFIG_CAMERA_PIN_PCLK
#endif
#define XCLK_FREQ_HZ 20000000
#define CAMERA_PIXFORMAT PIXFORMAT_RGB565
#define CAMERA_FRAME_SIZE FRAMESIZE_240X240
#define CAMERA_FB_COUNT 2
// lcd pins and setting
#if CONFIG_CAMERA_MODULE_ESP_S3_EYE
#define LCD_CONTROLLER SCREEN_CONTROLLER_ST7789
#define LCD_MOSI 46
#define LCD_MISO -1
#define LCD_SCLK 21
#define LCD_CS 44
#define LCD_DC 47
#define LCD_RST -1
#define LCD_BCKL 48
#define LCD_WIDTH 240
#define LCD_HEIGHT 240
#define LCD_ROTATE 0
#elif CONFIG_CAMERA_MODULE_ESP_S2_KALUGA
#define LCD_CONTROLLER SCREEN_CONTROLLER_ST7789
#define LCD_MOSI 9
#define LCD_MISO -1
#define LCD_SCLK 15
#define LCD_CS 11
#define LCD_DC 13
#define LCD_RST 16
#define LCD_BCKL 6
// LCD display width and height
#define LCD_WIDTH 240
#define LCD_HEIGHT 320
#define LCD_ROTATE SCR_SWAP_XY|SCR_MIRROR_X
#endif
#ifdef __cplusplus
extern "C"
{
#endif
esp_err_t app_camera_init();
esp_err_t app_lcd_init();
#ifdef __cplusplus
}
#endif
调用xTaskCreatePinnedToCore()函数将decode_task函数创建任务,在freeRTOS实时性操作系统中运行
decode_task()任务函数中做了以下操作:
app_camera_init()初始化摄像头后,如果返回值不等于ESP_OK,说明摄像头初始化失败,调用vTaskDelete(NULL);删除任务并退出
摄像头初始化后会输出一个帧的输出流,调用esp_camera_fb_get()的函数,获取指向帧缓冲区的指针,如果帧缓冲区的指针指向NULL,则认为摄像头获取数据失败
如果帧缓冲区的指针不为空,说明了摄像头获取到了数据,此时开始进行二维码识别的相关操作
1.调用esp_code_scanner_create()函数返回一个指向二维码扫描的指针
2.esp_code_scanner_config_t给二维码扫描设置相关参数
3.调用esp_code_scanner_set_config()将二维码扫描的config传入
4.调用esp_code_scanner_scan_image()扫描图片,并且返回已解码符号的数目
5.调用esp_code_scanner_result()获取到扫描的结果,并打印输出
6.调用esp_code_scanner_destroy()将二维码扫描删除
7.调用esp_camera_fb_return()返回要再次重用的帧缓冲区
运行效果
识别到二维码