ClassControllCamera.cpp 14 KB

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