-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_setup.cpp
More file actions
115 lines (102 loc) · 3.43 KB
/
camera_setup.cpp
File metadata and controls
115 lines (102 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Implémentation de la gestion caméra
*/
#include "camera_setup.h"
#include "esp_camera.h"
#include "config.h"
#include "base64.h"
#include "logger.h"
void Camera::init() {
Logger::log("Camera init...");
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sccb_sda = SIOD_GPIO_NUM;
config.pin_sccb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;
config.grab_mode = CAMERA_GRAB_LATEST;
if (psramFound()) {
config.frame_size = (framesize_t)cfg.camera.framesize;
config.jpeg_quality = cfg.camera.quality;
config.fb_count = 2;
config.fb_location = CAMERA_FB_IN_PSRAM;
Logger::printf("PSRAM detected - framesize: %d", cfg.camera.framesize);
} else {
config.frame_size = FRAMESIZE_QVGA;
config.jpeg_quality = 15;
config.fb_count = 1;
config.fb_location = CAMERA_FB_IN_DRAM;
Logger::log("No PSRAM - QVGA");
}
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Logger::printf("Camera init error: 0x%x", err);
return;
}
Logger::log("Camera ready");
}
void Camera::applySettings() {
sensor_t* s = esp_camera_sensor_get();
if (!s) {
Logger::log("Sensor unavailable");
return;
}
s->set_framesize(s, (framesize_t)cfg.camera.framesize);
s->set_quality(s, cfg.camera.quality);
s->set_brightness(s, cfg.camera.brightness);
s->set_contrast(s, cfg.camera.contrast);
s->set_saturation(s, cfg.camera.saturation);
s->set_sharpness(s, cfg.camera.sharpness);
s->set_hmirror(s, cfg.camera.hmirror ? 1 : 0);
s->set_vflip(s, cfg.camera.vflip ? 1 : 0);
s->set_whitebal(s, cfg.camera.awb ? 1 : 0);
s->set_awb_gain(s, cfg.camera.awb ? 1 : 0);
s->set_exposure_ctrl(s, cfg.camera.aec ? 1 : 0);
if (!cfg.camera.aec) s->set_aec_value(s, cfg.camera.aec_value);
s->set_gain_ctrl(s, cfg.camera.agc ? 1 : 0);
if (!cfg.camera.agc) s->set_agc_gain(s, cfg.camera.agc_gain);
s->set_gainceiling(s, (gainceiling_t)cfg.camera.gainceiling);
s->set_bpc(s, cfg.camera.bpc);
s->set_wpc(s, cfg.camera.wpc);
s->set_raw_gma(s, cfg.camera.raw_gma);
s->set_lenc(s, cfg.camera.lenc);
s->set_dcw(s, cfg.camera.dcw);
s->set_special_effect(s, cfg.camera.special_effect);
s->set_wb_mode(s, cfg.camera.wb_mode);
s->set_ae_level(s, cfg.camera.ae_level);
s->set_aec2(s, cfg.camera.aec2 ? 1 : 0);
s->set_colorbar(s, cfg.camera.colorbar ? 1 : 0);
Logger::log("Camera settings applied");
}
String Camera::captureBase64() {
camera_fb_t* fb = esp_camera_fb_get();
if (!fb) {
Logger::log("Capture error");
return "";
}
// Verifier que c'est un JPEG valide (commence par FFD8)
if (fb->len < 2 || fb->buf[0] != 0xFF || fb->buf[1] != 0xD8) {
Logger::log("Invalid JPEG header!");
esp_camera_fb_return(fb);
return "";
}
Logger::printf("JPEG: %d bytes, header: %02X%02X", fb->len, fb->buf[0], fb->buf[1]);
String base64Image = base64::encode(fb->buf, fb->len);
esp_camera_fb_return(fb);
return base64Image;
}