ClassControllCamera.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #include "ClassControllCamera.h"
  2. #include "ClassLogFile.h"
  3. #include <stdio.h>
  4. #include "driver/gpio.h"
  5. #include "esp_timer.h"
  6. #include "esp_log.h"
  7. #include "Helper.h"
  8. #include "CImageBasis.h"
  9. #define BOARD_ESP32CAM_AITHINKER
  10. #include <esp_event_loop.h>
  11. #include <esp_log.h>
  12. #include <esp_system.h>
  13. #include <nvs_flash.h>
  14. #include <sys/param.h>
  15. #include <string.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "esp_camera.h"
  19. // #define DEBUG_DETAIL_ON
  20. // ESP32Cam (AiThinker) PIN Map
  21. #define CAM_PIN_PWDN (gpio_num_t) 32
  22. #define CAM_PIN_RESET -1 //software reset will be performed
  23. #define CAM_PIN_XCLK 0
  24. #define CAM_PIN_SIOD 26
  25. #define CAM_PIN_SIOC 27
  26. #define CAM_PIN_D7 35
  27. #define CAM_PIN_D6 34
  28. #define CAM_PIN_D5 39
  29. #define CAM_PIN_D4 36
  30. #define CAM_PIN_D3 21
  31. #define CAM_PIN_D2 19
  32. #define CAM_PIN_D1 18
  33. #define CAM_PIN_D0 5
  34. #define CAM_PIN_VSYNC 25
  35. #define CAM_PIN_HREF 23
  36. #define CAM_PIN_PCLK 22
  37. static const char *TAG = "example:take_picture";
  38. static camera_config_t camera_config = {
  39. .pin_pwdn = CAM_PIN_PWDN,
  40. .pin_reset = CAM_PIN_RESET,
  41. .pin_xclk = CAM_PIN_XCLK,
  42. .pin_sscb_sda = CAM_PIN_SIOD,
  43. .pin_sscb_scl = CAM_PIN_SIOC,
  44. .pin_d7 = CAM_PIN_D7,
  45. .pin_d6 = CAM_PIN_D6,
  46. .pin_d5 = CAM_PIN_D5,
  47. .pin_d4 = CAM_PIN_D4,
  48. .pin_d3 = CAM_PIN_D3,
  49. .pin_d2 = CAM_PIN_D2,
  50. .pin_d1 = CAM_PIN_D1,
  51. .pin_d0 = CAM_PIN_D0,
  52. .pin_vsync = CAM_PIN_VSYNC,
  53. .pin_href = CAM_PIN_HREF,
  54. .pin_pclk = CAM_PIN_PCLK,
  55. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  56. // .xclk_freq_hz = 20000000, // Orginalwert
  57. .xclk_freq_hz = 5000000, // Test, um die Bildfehler los zu werden !!!!
  58. .ledc_timer = LEDC_TIMER_0,
  59. .ledc_channel = LEDC_CHANNEL_0,
  60. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  61. .frame_size = FRAMESIZE_VGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  62. // .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  63. .jpeg_quality = 5, //0-63 lower number means higher quality
  64. .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
  65. };
  66. #include "driver/ledc.h"
  67. CCamera Camera;
  68. #define FLASH_GPIO GPIO_NUM_4
  69. #define BLINK_GPIO GPIO_NUM_33
  70. typedef struct {
  71. httpd_req_t *req;
  72. size_t len;
  73. } jpg_chunking_t;
  74. #define LEDC_LS_CH2_GPIO (4)
  75. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  76. #define LEDC_LS_TIMER LEDC_TIMER_1
  77. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  78. #define LEDC_TEST_DUTY (4000)
  79. void test(){
  80. ledc_channel_config_t ledc_channel = { };
  81. ledc_channel.channel = LEDC_LS_CH2_CHANNEL;
  82. ledc_channel.duty = 0;
  83. ledc_channel.gpio_num = FLASH_GPIO;
  84. ledc_channel.speed_mode = LEDC_LS_MODE;
  85. ledc_channel.hpoint = 0;
  86. ledc_channel.timer_sel = LEDC_LS_TIMER;
  87. ledc_channel_config(&ledc_channel);
  88. ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, LEDC_TEST_DUTY);
  89. ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel);
  90. vTaskDelay(1000 / portTICK_PERIOD_MS);
  91. };
  92. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
  93. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  94. if(!index){
  95. j->len = 0;
  96. }
  97. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
  98. return 0;
  99. }
  100. j->len += len;
  101. return len;
  102. }
  103. bool CCamera::SetBrightnessContrastSaturation(int _brightness, int _contrast, int _saturation)
  104. {
  105. bool result = false;
  106. sensor_t * s = esp_camera_sensor_get();
  107. if (_brightness > -100)
  108. _brightness = min(2, max(-2, _brightness));
  109. if (_contrast > -100)
  110. _contrast = min(2, max(-2, _contrast));
  111. // _saturation = min(2, max(-2, _saturation));
  112. // s->set_saturation(s, _saturation);
  113. if (_contrast > -100)
  114. s->set_contrast(s, _contrast);
  115. if (_brightness > -100)
  116. s->set_brightness(s, _brightness);
  117. if ((_brightness != brightness) && (_brightness > -100))
  118. result = true;
  119. if ((_contrast != contrast) && (_contrast > -100))
  120. result = true;
  121. if ((_saturation != saturation) && (_saturation > -100))
  122. result = true;
  123. if (_brightness > -100)
  124. brightness = _brightness;
  125. if (_contrast > -100)
  126. contrast = _contrast;
  127. if (_saturation > -100)
  128. saturation = _saturation;
  129. if (result && isFixedExposure)
  130. EnableAutoExposure(waitbeforepicture_org);
  131. return result;
  132. }
  133. void CCamera::SetQualitySize(int qual, framesize_t resol)
  134. {
  135. sensor_t * s = esp_camera_sensor_get();
  136. s->set_quality(s, qual);
  137. s->set_framesize(s, resol);
  138. ActualResolution = resol;
  139. ActualQuality = qual;
  140. if (resol == FRAMESIZE_QVGA)
  141. {
  142. image_height = 240;
  143. image_width = 320;
  144. }
  145. if (resol == FRAMESIZE_VGA)
  146. {
  147. image_height = 480;
  148. image_width = 640;
  149. }
  150. // No higher Mode than VGA, damit der Kameraspeicher ausreicht.
  151. /*
  152. if (resol == FRAMESIZE_SVGA)
  153. {
  154. image_height = 600;
  155. image_width = 800;
  156. }
  157. if (resol == FRAMESIZE_XGA)
  158. {
  159. image_height = 768;
  160. image_width = 1024;
  161. }
  162. if (resol == FRAMESIZE_SXGA)
  163. {
  164. image_height = 1024;
  165. image_width = 1280;
  166. }
  167. if (resol == FRAMESIZE_UXGA)
  168. {
  169. image_height = 1200;
  170. image_width = 1600;
  171. }
  172. */
  173. }
  174. void CCamera::EnableAutoExposure(int flashdauer)
  175. {
  176. LEDOnOff(true);
  177. LightOnOff(true);
  178. const TickType_t xDelay = flashdauer / portTICK_PERIOD_MS;
  179. vTaskDelay( xDelay );
  180. camera_fb_t * fb = esp_camera_fb_get();
  181. if (!fb) {
  182. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  183. }
  184. esp_camera_fb_return(fb);
  185. sensor_t * s = esp_camera_sensor_get();
  186. s->set_gain_ctrl(s, 0);
  187. s->set_exposure_ctrl(s, 0);
  188. LEDOnOff(false);
  189. LightOnOff(false);
  190. isFixedExposure = true;
  191. waitbeforepicture_org = flashdauer;
  192. }
  193. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  194. {
  195. string ftype;
  196. uint8_t *zwischenspeicher = NULL;
  197. LEDOnOff(true);
  198. #ifdef DEBUG_DETAIL_ON
  199. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  200. #endif
  201. if (delay > 0)
  202. {
  203. LightOnOff(true);
  204. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  205. vTaskDelay( xDelay );
  206. }
  207. #ifdef DEBUG_DETAIL_ON
  208. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  209. #endif
  210. camera_fb_t * fb = esp_camera_fb_get();
  211. if (!fb) {
  212. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  213. LEDOnOff(false);
  214. return ESP_FAIL;
  215. }
  216. int _size = fb->len;
  217. zwischenspeicher = (uint8_t*) malloc(_size);
  218. for (int i = 0; i < _size; ++i)
  219. *(zwischenspeicher + i) = *(fb->buf + i);
  220. esp_camera_fb_return(fb);
  221. #ifdef DEBUG_DETAIL_ON
  222. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  223. #endif
  224. LEDOnOff(false);
  225. if (delay > 0)
  226. LightOnOff(false);
  227. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  228. // vTaskDelay( xDelay ); // wait for power to recover
  229. uint8_t * buf = NULL;
  230. CImageBasis _zwImage;
  231. _zwImage.LoadFromMemory(zwischenspeicher, _size);
  232. free(zwischenspeicher);
  233. #ifdef DEBUG_DETAIL_ON
  234. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  235. #endif
  236. stbi_uc* p_target;
  237. stbi_uc* p_source;
  238. int channels = 3;
  239. int width = image_width;
  240. int height = image_height;
  241. #ifdef DEBUG_DETAIL_ON
  242. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  243. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage.rgb_image) + " Size: " + std::to_string(_zwImage.width) + ", " + std::to_string(_zwImage.height);
  244. LogFile.WriteToFile(_zw);
  245. #endif
  246. for (int x = 0; x < width; ++x)
  247. for (int y = 0; y < height; ++y)
  248. {
  249. p_target = _Image->rgb_image + (channels * (y * width + x));
  250. p_source = _zwImage.rgb_image + (channels * (y * width + x));
  251. p_target[0] = p_source[0];
  252. p_target[1] = p_source[1];
  253. p_target[2] = p_source[2];
  254. }
  255. #ifdef DEBUG_DETAIL_ON
  256. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  257. #endif
  258. free(buf);
  259. #ifdef DEBUG_DETAIL_ON
  260. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  261. #endif
  262. return ESP_OK;
  263. }
  264. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  265. {
  266. string ftype;
  267. LEDOnOff(true); // Abgeschaltet, um Strom zu sparen !!!!!!
  268. if (delay > 0)
  269. {
  270. LightOnOff(true);
  271. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  272. vTaskDelay( xDelay );
  273. }
  274. camera_fb_t * fb = esp_camera_fb_get();
  275. if (!fb) {
  276. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  277. LEDOnOff(false);
  278. return ESP_FAIL;
  279. }
  280. LEDOnOff(false);
  281. #ifdef DEBUG_DETAIL_ON
  282. printf("w %d, h %d, size %d\n", fb->width, fb->height, fb->len);
  283. #endif
  284. nm = FormatFileName(nm);
  285. #ifdef DEBUG_DETAIL_ON
  286. printf("Save Camera to : %s\n", nm.c_str());
  287. #endif
  288. ftype = toUpper(getFileType(nm));
  289. #ifdef DEBUG_DETAIL_ON
  290. printf("Filetype: %s\n", ftype.c_str());
  291. #endif
  292. uint8_t * buf = NULL;
  293. size_t buf_len = 0;
  294. bool converted = false;
  295. if (ftype.compare("BMP") == 0)
  296. {
  297. frame2bmp(fb, &buf, &buf_len);
  298. converted = true;
  299. }
  300. if (ftype.compare("JPG") == 0)
  301. {
  302. if(fb->format != PIXFORMAT_JPEG){
  303. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  304. converted = true;
  305. if(!jpeg_converted){
  306. ESP_LOGE(TAGCAMERACLASS, "JPEG compression failed");
  307. }
  308. } else {
  309. buf_len = fb->len;
  310. buf = fb->buf;
  311. }
  312. }
  313. FILE * fp = OpenFileAndWait(nm.c_str(), "wb");
  314. if (fp == NULL) /* If an error occurs during the file creation */
  315. {
  316. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  317. }
  318. else
  319. {
  320. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  321. fclose(fp);
  322. }
  323. if (converted)
  324. free(buf);
  325. esp_camera_fb_return(fb);
  326. if (delay > 0)
  327. {
  328. LightOnOff(false);
  329. }
  330. return ESP_OK;
  331. }
  332. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  333. {
  334. camera_fb_t * fb = NULL;
  335. esp_err_t res = ESP_OK;
  336. size_t fb_len = 0;
  337. int64_t fr_start = esp_timer_get_time();
  338. LEDOnOff(true);
  339. if (delay > 0)
  340. {
  341. LightOnOff(true);
  342. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  343. vTaskDelay( xDelay );
  344. }
  345. fb = esp_camera_fb_get();
  346. if (!fb) {
  347. ESP_LOGE(TAGCAMERACLASS, "Camera capture failed");
  348. httpd_resp_send_500(req);
  349. return ESP_FAIL;
  350. }
  351. LEDOnOff(false);
  352. res = httpd_resp_set_type(req, "image/jpeg");
  353. if(res == ESP_OK){
  354. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  355. }
  356. if(res == ESP_OK){
  357. if(fb->format == PIXFORMAT_JPEG){
  358. fb_len = fb->len;
  359. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  360. } else {
  361. jpg_chunking_t jchunk = {req, 0};
  362. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  363. httpd_resp_send_chunk(req, NULL, 0);
  364. fb_len = jchunk.len;
  365. }
  366. }
  367. esp_camera_fb_return(fb);
  368. int64_t fr_end = esp_timer_get_time();
  369. ESP_LOGI(TAGCAMERACLASS, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  370. if (delay > 0)
  371. {
  372. LightOnOff(false);
  373. }
  374. return res;
  375. }
  376. void CCamera::LightOnOff(bool status)
  377. {
  378. // Init the GPIO
  379. gpio_pad_select_gpio(FLASH_GPIO);
  380. /* Set the GPIO as a push/pull output */
  381. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  382. if (status)
  383. gpio_set_level(FLASH_GPIO, 1);
  384. else
  385. gpio_set_level(FLASH_GPIO, 0);
  386. }
  387. void CCamera::LEDOnOff(bool status)
  388. {
  389. // Init the GPIO
  390. gpio_pad_select_gpio(BLINK_GPIO);
  391. /* Set the GPIO as a push/pull output */
  392. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  393. if (!status)
  394. gpio_set_level(BLINK_GPIO, 1);
  395. else
  396. gpio_set_level(BLINK_GPIO, 0);
  397. }
  398. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  399. {
  400. char _query[100];
  401. char _qual[10];
  402. char _size[10];
  403. resol = ActualResolution;
  404. qual = ActualQuality;
  405. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  406. {
  407. printf("Query: "); printf(_query); printf("\n");
  408. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  409. {
  410. #ifdef DEBUG_DETAIL_ON
  411. printf("Size: "); printf(_size); printf("\n");
  412. #endif
  413. if (strcmp(_size, "QVGA") == 0)
  414. resol = FRAMESIZE_QVGA; // 320x240
  415. if (strcmp(_size, "VGA") == 0)
  416. resol = FRAMESIZE_VGA; // 640x480
  417. if (strcmp(_size, "SVGA") == 0)
  418. resol = FRAMESIZE_SVGA; // 800x600
  419. if (strcmp(_size, "XGA") == 0)
  420. resol = FRAMESIZE_XGA; // 1024x768
  421. if (strcmp(_size, "SXGA") == 0)
  422. resol = FRAMESIZE_SXGA; // 1280x1024
  423. if (strcmp(_size, "UXGA") == 0)
  424. resol = FRAMESIZE_UXGA; // 1600x1200
  425. }
  426. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  427. {
  428. #ifdef DEBUG_DETAIL_ON
  429. printf("Quality: "); printf(_qual); printf("\n");
  430. #endif
  431. qual = atoi(_qual);
  432. if (qual > 63)
  433. qual = 63;
  434. if (qual < 0)
  435. qual = 0;
  436. }
  437. };
  438. }
  439. framesize_t CCamera::TextToFramesize(const char * _size)
  440. {
  441. if (strcmp(_size, "QVGA") == 0)
  442. return FRAMESIZE_QVGA; // 320x240
  443. if (strcmp(_size, "VGA") == 0)
  444. return FRAMESIZE_VGA; // 640x480
  445. if (strcmp(_size, "SVGA") == 0)
  446. return FRAMESIZE_SVGA; // 800x600
  447. if (strcmp(_size, "XGA") == 0)
  448. return FRAMESIZE_XGA; // 1024x768
  449. if (strcmp(_size, "SXGA") == 0)
  450. return FRAMESIZE_SXGA; // 1280x1024
  451. if (strcmp(_size, "UXGA") == 0)
  452. return FRAMESIZE_UXGA; // 1600x1200
  453. return ActualResolution;
  454. }
  455. CCamera::CCamera()
  456. {
  457. #ifdef DEBUG_DETAIL_ON
  458. printf("CreateClassCamera\n");
  459. #endif
  460. brightness = -5;
  461. contrast = -5;
  462. saturation = -5;
  463. isFixedExposure = false;
  464. }
  465. esp_err_t CCamera::InitCam()
  466. {
  467. if(CAM_PIN_PWDN != -1){
  468. // Init the GPIO
  469. gpio_pad_select_gpio(CAM_PIN_PWDN);
  470. /* Set the GPIO as a push/pull output */
  471. gpio_set_direction(CAM_PIN_PWDN, GPIO_MODE_OUTPUT);
  472. gpio_set_level(CAM_PIN_PWDN, 0);
  473. }
  474. printf("Init Camera\n");
  475. ActualQuality = camera_config.jpeg_quality;
  476. ActualResolution = camera_config.frame_size;
  477. //initialize the camera
  478. esp_err_t err = esp_camera_init(&camera_config);
  479. if (err != ESP_OK) {
  480. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  481. return err;
  482. }
  483. return ESP_OK;
  484. }