ClassControllCamera.cpp 14 KB

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