ClassControllCamera.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. _brightness = min(2, max(-2, _brightness));
  108. _contrast = min(2, max(-2, _contrast));
  109. // _saturation = min(2, max(-2, _saturation));
  110. // s->set_saturation(s, _saturation);
  111. s->set_contrast(s, _contrast);
  112. s->set_brightness(s, _brightness);
  113. if (_brightness != brightness)
  114. result = true;
  115. if (_contrast != contrast)
  116. result = true;
  117. if (_saturation != saturation)
  118. result = true;
  119. brightness = _brightness;
  120. contrast = _contrast;
  121. saturation = _saturation;
  122. if (result && isFixedExposure)
  123. EnableAutoExposure(waitbeforepicture_org);
  124. return result;
  125. }
  126. void CCamera::SetQualitySize(int qual, framesize_t resol)
  127. {
  128. sensor_t * s = esp_camera_sensor_get();
  129. s->set_quality(s, qual);
  130. s->set_framesize(s, resol);
  131. ActualResolution = resol;
  132. ActualQuality = qual;
  133. if (resol == FRAMESIZE_QVGA)
  134. {
  135. image_height = 240;
  136. image_width = 320;
  137. }
  138. if (resol == FRAMESIZE_VGA)
  139. {
  140. image_height = 480;
  141. image_width = 640;
  142. }
  143. // No higher Mode than VGA, damit der Kameraspeicher ausreicht.
  144. /*
  145. if (resol == FRAMESIZE_SVGA)
  146. {
  147. image_height = 600;
  148. image_width = 800;
  149. }
  150. if (resol == FRAMESIZE_XGA)
  151. {
  152. image_height = 768;
  153. image_width = 1024;
  154. }
  155. if (resol == FRAMESIZE_SXGA)
  156. {
  157. image_height = 1024;
  158. image_width = 1280;
  159. }
  160. if (resol == FRAMESIZE_UXGA)
  161. {
  162. image_height = 1200;
  163. image_width = 1600;
  164. }
  165. */
  166. }
  167. void CCamera::EnableAutoExposure(int flashdauer)
  168. {
  169. LEDOnOff(true);
  170. LightOnOff(true);
  171. const TickType_t xDelay = flashdauer / portTICK_PERIOD_MS;
  172. vTaskDelay( xDelay );
  173. camera_fb_t * fb = esp_camera_fb_get();
  174. if (!fb) {
  175. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  176. }
  177. esp_camera_fb_return(fb);
  178. sensor_t * s = esp_camera_sensor_get();
  179. s->set_gain_ctrl(s, 0);
  180. s->set_exposure_ctrl(s, 0);
  181. LEDOnOff(false);
  182. LightOnOff(false);
  183. isFixedExposure = true;
  184. waitbeforepicture_org = flashdauer;
  185. }
  186. esp_err_t CCamera::CaptureToBasisImage(CImageBasis *_Image, int delay)
  187. {
  188. string ftype;
  189. uint8_t *zwischenspeicher = NULL;
  190. LEDOnOff(true);
  191. #ifdef DEBUG_DETAIL_ON
  192. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Start");
  193. #endif
  194. if (delay > 0)
  195. {
  196. LightOnOff(true);
  197. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  198. vTaskDelay( xDelay );
  199. }
  200. #ifdef DEBUG_DETAIL_ON
  201. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LightOn");
  202. #endif
  203. camera_fb_t * fb = esp_camera_fb_get();
  204. if (!fb) {
  205. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  206. LEDOnOff(false);
  207. return ESP_FAIL;
  208. }
  209. int _size = fb->len;
  210. zwischenspeicher = (uint8_t*) malloc(_size);
  211. for (int i = 0; i < _size; ++i)
  212. *(zwischenspeicher + i) = *(fb->buf + i);
  213. esp_camera_fb_return(fb);
  214. #ifdef DEBUG_DETAIL_ON
  215. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After fb_get");
  216. #endif
  217. LEDOnOff(false);
  218. if (delay > 0)
  219. LightOnOff(false);
  220. // TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
  221. // vTaskDelay( xDelay ); // wait for power to recover
  222. uint8_t * buf = NULL;
  223. CImageBasis _zwImage;
  224. _zwImage.LoadFromMemory(zwischenspeicher, _size);
  225. free(zwischenspeicher);
  226. #ifdef DEBUG_DETAIL_ON
  227. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After LoadFromMemory");
  228. #endif
  229. stbi_uc* p_target;
  230. stbi_uc* p_source;
  231. int channels = 3;
  232. int width = image_width;
  233. int height = image_height;
  234. #ifdef DEBUG_DETAIL_ON
  235. std::string _zw = "Targetimage: " + std::to_string((int) _Image->rgb_image) + " Size: " + std::to_string(_Image->width) + ", " + std::to_string(_Image->height);
  236. _zw = _zw + " _zwImage: " + std::to_string((int) _zwImage.rgb_image) + " Size: " + std::to_string(_zwImage.width) + ", " + std::to_string(_zwImage.height);
  237. LogFile.WriteToFile(_zw);
  238. #endif
  239. for (int x = 0; x < width; ++x)
  240. for (int y = 0; y < height; ++y)
  241. {
  242. p_target = _Image->rgb_image + (channels * (y * width + x));
  243. p_source = _zwImage.rgb_image + (channels * (y * width + x));
  244. p_target[0] = p_source[0];
  245. p_target[1] = p_source[1];
  246. p_target[2] = p_source[2];
  247. }
  248. #ifdef DEBUG_DETAIL_ON
  249. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - After Copy To Target");
  250. #endif
  251. free(buf);
  252. #ifdef DEBUG_DETAIL_ON
  253. LogFile.WriteHeapInfo("CCamera::CaptureToBasisImage - Done");
  254. #endif
  255. return ESP_OK;
  256. }
  257. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  258. {
  259. string ftype;
  260. LEDOnOff(true); // Abgeschaltet, um Strom zu sparen !!!!!!
  261. if (delay > 0)
  262. {
  263. LightOnOff(true);
  264. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  265. vTaskDelay( xDelay );
  266. }
  267. camera_fb_t * fb = esp_camera_fb_get();
  268. if (!fb) {
  269. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  270. LEDOnOff(false);
  271. return ESP_FAIL;
  272. }
  273. LEDOnOff(false);
  274. #ifdef DEBUG_DETAIL_ON
  275. printf("w %d, h %d, size %d\n", fb->width, fb->height, fb->len);
  276. #endif
  277. nm = FormatFileName(nm);
  278. #ifdef DEBUG_DETAIL_ON
  279. printf("Save Camera to : %s\n", nm.c_str());
  280. #endif
  281. ftype = toUpper(getFileType(nm));
  282. #ifdef DEBUG_DETAIL_ON
  283. printf("Filetype: %s\n", ftype.c_str());
  284. #endif
  285. uint8_t * buf = NULL;
  286. size_t buf_len = 0;
  287. bool converted = false;
  288. if (ftype.compare("BMP") == 0)
  289. {
  290. frame2bmp(fb, &buf, &buf_len);
  291. converted = true;
  292. }
  293. if (ftype.compare("JPG") == 0)
  294. {
  295. if(fb->format != PIXFORMAT_JPEG){
  296. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  297. converted = true;
  298. if(!jpeg_converted){
  299. ESP_LOGE(TAGCAMERACLASS, "JPEG compression failed");
  300. }
  301. } else {
  302. buf_len = fb->len;
  303. buf = fb->buf;
  304. }
  305. }
  306. FILE * fp = OpenFileAndWait(nm.c_str(), "wb");
  307. if (fp == NULL) /* If an error occurs during the file creation */
  308. {
  309. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  310. }
  311. else
  312. {
  313. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  314. fclose(fp);
  315. }
  316. if (converted)
  317. free(buf);
  318. esp_camera_fb_return(fb);
  319. if (delay > 0)
  320. {
  321. LightOnOff(false);
  322. }
  323. return ESP_OK;
  324. }
  325. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  326. {
  327. camera_fb_t * fb = NULL;
  328. esp_err_t res = ESP_OK;
  329. size_t fb_len = 0;
  330. int64_t fr_start = esp_timer_get_time();
  331. LEDOnOff(true);
  332. if (delay > 0)
  333. {
  334. LightOnOff(true);
  335. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  336. vTaskDelay( xDelay );
  337. }
  338. fb = esp_camera_fb_get();
  339. if (!fb) {
  340. ESP_LOGE(TAGCAMERACLASS, "Camera capture failed");
  341. httpd_resp_send_500(req);
  342. return ESP_FAIL;
  343. }
  344. LEDOnOff(false);
  345. res = httpd_resp_set_type(req, "image/jpeg");
  346. if(res == ESP_OK){
  347. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=raw.jpg");
  348. }
  349. if(res == ESP_OK){
  350. if(fb->format == PIXFORMAT_JPEG){
  351. fb_len = fb->len;
  352. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  353. } else {
  354. jpg_chunking_t jchunk = {req, 0};
  355. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  356. httpd_resp_send_chunk(req, NULL, 0);
  357. fb_len = jchunk.len;
  358. }
  359. }
  360. esp_camera_fb_return(fb);
  361. int64_t fr_end = esp_timer_get_time();
  362. ESP_LOGI(TAGCAMERACLASS, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  363. if (delay > 0)
  364. {
  365. LightOnOff(false);
  366. }
  367. return res;
  368. }
  369. void CCamera::LightOnOff(bool status)
  370. {
  371. // Init the GPIO
  372. gpio_pad_select_gpio(FLASH_GPIO);
  373. /* Set the GPIO as a push/pull output */
  374. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  375. if (status)
  376. gpio_set_level(FLASH_GPIO, 1);
  377. else
  378. gpio_set_level(FLASH_GPIO, 0);
  379. }
  380. void CCamera::LEDOnOff(bool status)
  381. {
  382. // Init the GPIO
  383. gpio_pad_select_gpio(BLINK_GPIO);
  384. /* Set the GPIO as a push/pull output */
  385. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  386. if (!status)
  387. gpio_set_level(BLINK_GPIO, 1);
  388. else
  389. gpio_set_level(BLINK_GPIO, 0);
  390. }
  391. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  392. {
  393. char _query[100];
  394. char _qual[10];
  395. char _size[10];
  396. resol = ActualResolution;
  397. qual = ActualQuality;
  398. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  399. {
  400. printf("Query: "); printf(_query); printf("\n");
  401. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  402. {
  403. #ifdef DEBUG_DETAIL_ON
  404. printf("Size: "); printf(_size); printf("\n");
  405. #endif
  406. if (strcmp(_size, "QVGA") == 0)
  407. resol = FRAMESIZE_QVGA; // 320x240
  408. if (strcmp(_size, "VGA") == 0)
  409. resol = FRAMESIZE_VGA; // 640x480
  410. if (strcmp(_size, "SVGA") == 0)
  411. resol = FRAMESIZE_SVGA; // 800x600
  412. if (strcmp(_size, "XGA") == 0)
  413. resol = FRAMESIZE_XGA; // 1024x768
  414. if (strcmp(_size, "SXGA") == 0)
  415. resol = FRAMESIZE_SXGA; // 1280x1024
  416. if (strcmp(_size, "UXGA") == 0)
  417. resol = FRAMESIZE_UXGA; // 1600x1200
  418. }
  419. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  420. {
  421. #ifdef DEBUG_DETAIL_ON
  422. printf("Quality: "); printf(_qual); printf("\n");
  423. #endif
  424. qual = atoi(_qual);
  425. if (qual > 63)
  426. qual = 63;
  427. if (qual < 0)
  428. qual = 0;
  429. }
  430. };
  431. }
  432. framesize_t CCamera::TextToFramesize(const char * _size)
  433. {
  434. if (strcmp(_size, "QVGA") == 0)
  435. return FRAMESIZE_QVGA; // 320x240
  436. if (strcmp(_size, "VGA") == 0)
  437. return FRAMESIZE_VGA; // 640x480
  438. if (strcmp(_size, "SVGA") == 0)
  439. return FRAMESIZE_SVGA; // 800x600
  440. if (strcmp(_size, "XGA") == 0)
  441. return FRAMESIZE_XGA; // 1024x768
  442. if (strcmp(_size, "SXGA") == 0)
  443. return FRAMESIZE_SXGA; // 1280x1024
  444. if (strcmp(_size, "UXGA") == 0)
  445. return FRAMESIZE_UXGA; // 1600x1200
  446. return ActualResolution;
  447. }
  448. CCamera::CCamera()
  449. {
  450. #ifdef DEBUG_DETAIL_ON
  451. printf("CreateClassCamera\n");
  452. #endif
  453. brightness = -5;
  454. contrast = -5;
  455. saturation = -5;
  456. isFixedExposure = false;
  457. }
  458. esp_err_t CCamera::InitCam()
  459. {
  460. if(CAM_PIN_PWDN != -1){
  461. // Init the GPIO
  462. gpio_pad_select_gpio(CAM_PIN_PWDN);
  463. /* Set the GPIO as a push/pull output */
  464. gpio_set_direction(CAM_PIN_PWDN, GPIO_MODE_OUTPUT);
  465. gpio_set_level(CAM_PIN_PWDN, 0);
  466. }
  467. printf("Init Camera\n");
  468. ActualQuality = camera_config.jpeg_quality;
  469. ActualResolution = camera_config.frame_size;
  470. //initialize the camera
  471. esp_err_t err = esp_camera_init(&camera_config);
  472. if (err != ESP_OK) {
  473. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  474. return err;
  475. }
  476. return ESP_OK;
  477. }