ClassControllCamera.cpp 13 KB

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