ClassControllCamera.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include "ClassControllCamera.h"
  2. #include <stdio.h>
  3. #include "driver/gpio.h"
  4. #include "esp_timer.h"
  5. #include "esp_log.h"
  6. #include "Helper.h"
  7. #include "CFindTemplate.h"
  8. // #include "camera_define.h"
  9. /////////////////////////////////////////////////////////////////////////////////////////
  10. /////////////////////////////////////////////////////////////////////////////////////////
  11. #define BOARD_ESP32CAM_AITHINKER
  12. /**
  13. * 2. Kconfig setup
  14. *
  15. * If you have a Kconfig file, copy the content from
  16. * https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
  17. * In case you haven't, copy and paste this Kconfig file inside the src directory.
  18. * This Kconfig file has definitions that allows more control over the camera and
  19. * how it will be initialized.
  20. */
  21. /**
  22. * 3. Enable PSRAM on sdkconfig:
  23. *
  24. * CONFIG_ESP32_SPIRAM_SUPPORT=y
  25. *
  26. * More info on
  27. * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
  28. */
  29. // ================================ CODE ======================================
  30. #include <esp_event_loop.h>
  31. #include <esp_log.h>
  32. #include <esp_system.h>
  33. #include <nvs_flash.h>
  34. #include <sys/param.h>
  35. #include <string.h>
  36. #include "freertos/FreeRTOS.h"
  37. #include "freertos/task.h"
  38. #include "esp_camera.h"
  39. // WROVER-KIT PIN Map
  40. #ifdef BOARD_WROVER_KIT
  41. #define CAM_PIN_PWDN -1 //power down is not used
  42. #define CAM_PIN_RESET -1 //software reset will be performed
  43. #define CAM_PIN_XCLK 21
  44. #define CAM_PIN_SIOD 26
  45. #define CAM_PIN_SIOC 27
  46. #define CAM_PIN_D7 35
  47. #define CAM_PIN_D6 34
  48. #define CAM_PIN_D5 39
  49. #define CAM_PIN_D4 36
  50. #define CAM_PIN_D3 19
  51. #define CAM_PIN_D2 18
  52. #define CAM_PIN_D1 5
  53. #define CAM_PIN_D0 4
  54. #define CAM_PIN_VSYNC 25
  55. #define CAM_PIN_HREF 23
  56. #define CAM_PIN_PCLK 22
  57. #endif
  58. // ESP32Cam (AiThinker) PIN Map
  59. #ifdef BOARD_ESP32CAM_AITHINKER
  60. #define CAM_PIN_PWDN (gpio_num_t) 32
  61. #define CAM_PIN_RESET -1 //software reset will be performed
  62. #define CAM_PIN_XCLK 0
  63. #define CAM_PIN_SIOD 26
  64. #define CAM_PIN_SIOC 27
  65. #define CAM_PIN_D7 35
  66. #define CAM_PIN_D6 34
  67. #define CAM_PIN_D5 39
  68. #define CAM_PIN_D4 36
  69. #define CAM_PIN_D3 21
  70. #define CAM_PIN_D2 19
  71. #define CAM_PIN_D1 18
  72. #define CAM_PIN_D0 5
  73. #define CAM_PIN_VSYNC 25
  74. #define CAM_PIN_HREF 23
  75. #define CAM_PIN_PCLK 22
  76. #endif
  77. static const char *TAG = "example:take_picture";
  78. static camera_config_t camera_config = {
  79. .pin_pwdn = CAM_PIN_PWDN,
  80. .pin_reset = CAM_PIN_RESET,
  81. .pin_xclk = CAM_PIN_XCLK,
  82. .pin_sscb_sda = CAM_PIN_SIOD,
  83. .pin_sscb_scl = CAM_PIN_SIOC,
  84. .pin_d7 = CAM_PIN_D7,
  85. .pin_d6 = CAM_PIN_D6,
  86. .pin_d5 = CAM_PIN_D5,
  87. .pin_d4 = CAM_PIN_D4,
  88. .pin_d3 = CAM_PIN_D3,
  89. .pin_d2 = CAM_PIN_D2,
  90. .pin_d1 = CAM_PIN_D1,
  91. .pin_d0 = CAM_PIN_D0,
  92. .pin_vsync = CAM_PIN_VSYNC,
  93. .pin_href = CAM_PIN_HREF,
  94. .pin_pclk = CAM_PIN_PCLK,
  95. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  96. .xclk_freq_hz = 20000000,
  97. .ledc_timer = LEDC_TIMER_0,
  98. .ledc_channel = LEDC_CHANNEL_0,
  99. .pixel_format = PIXFORMAT_JPEG, //YUV422,GRAYSCALE,RGB565,JPEG
  100. .frame_size = FRAMESIZE_UXGA, //QQVGA-UXGA Do not use sizes above QVGA when not JPEG
  101. .jpeg_quality = 5, //0-63 lower number means higher quality
  102. .fb_count = 1 //if more than one, i2s runs in continuous mode. Use only with JPEG
  103. };
  104. /////////////////////////////////////////////////////////////////////////////////////////////////
  105. /////////////////////////////////////////////////////////////////////////////////////////////////
  106. #include "driver/ledc.h"
  107. CCamera Camera;
  108. #define FLASH_GPIO GPIO_NUM_4
  109. typedef struct {
  110. httpd_req_t *req;
  111. size_t len;
  112. } jpg_chunking_t;
  113. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  114. #define LEDC_LS_CH2_GPIO (4)
  115. #define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2
  116. #define LEDC_LS_TIMER LEDC_TIMER_1
  117. #define LEDC_LS_MODE LEDC_LOW_SPEED_MODE
  118. #define LEDC_TEST_DUTY (4000)
  119. void test(){
  120. ledc_channel_config_t ledc_channel = { };
  121. ledc_channel.channel = LEDC_LS_CH2_CHANNEL;
  122. ledc_channel.duty = 0;
  123. ledc_channel.gpio_num = FLASH_GPIO;
  124. ledc_channel.speed_mode = LEDC_LS_MODE;
  125. ledc_channel.hpoint = 0;
  126. ledc_channel.timer_sel = LEDC_LS_TIMER;
  127. ledc_channel_config(&ledc_channel);
  128. ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, LEDC_TEST_DUTY);
  129. ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel);
  130. vTaskDelay(1000 / portTICK_PERIOD_MS);
  131. };
  132. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. static size_t jpg_encode_stream(void * arg, size_t index, const void* data, size_t len){
  134. jpg_chunking_t *j = (jpg_chunking_t *)arg;
  135. if(!index){
  136. j->len = 0;
  137. }
  138. if(httpd_resp_send_chunk(j->req, (const char *)data, len) != ESP_OK){
  139. return 0;
  140. }
  141. j->len += len;
  142. return len;
  143. }
  144. void CCamera::SetQualitySize(int qual, framesize_t resol)
  145. {
  146. sensor_t * s = esp_camera_sensor_get();
  147. s->set_quality(s, qual);
  148. s->set_framesize(s, resol);
  149. ActualResolution = resol;
  150. ActualQuality = qual;
  151. }
  152. esp_err_t CCamera::CaptureToFile(std::string nm, int delay)
  153. {
  154. // nm = "/sdcard/josef_zw.bmp";
  155. string ftype;
  156. if (delay > 0)
  157. {
  158. LightOnOff(true);
  159. const TickType_t xDelay = delay / portTICK_PERIOD_MS;
  160. vTaskDelay( xDelay );
  161. }
  162. camera_fb_t * fb = esp_camera_fb_get();
  163. if (!fb) {
  164. ESP_LOGE(TAGCAMERACLASS, "Camera Capture Failed");
  165. return ESP_FAIL;
  166. }
  167. printf("w %d, h %d, size %d\n", fb->width, fb->height, fb->len);
  168. nm = FormatFileName(nm);
  169. printf("Save Camera to : %s\n", nm.c_str());
  170. ftype = toUpper(getFileType(nm));
  171. printf("Filetype: %s\n", ftype.c_str());
  172. uint8_t * buf = NULL;
  173. size_t buf_len = 0;
  174. bool converted = false;
  175. if (ftype.compare("BMP") == 0)
  176. {
  177. frame2bmp(fb, &buf, &buf_len);
  178. converted = true;
  179. }
  180. if (ftype.compare("JPG") == 0)
  181. {
  182. if(fb->format != PIXFORMAT_JPEG){
  183. bool jpeg_converted = frame2jpg(fb, ActualQuality, &buf, &buf_len);
  184. converted = true;
  185. if(!jpeg_converted){
  186. ESP_LOGE(TAGCAMERACLASS, "JPEG compression failed");
  187. }
  188. } else {
  189. buf_len = fb->len;
  190. buf = fb->buf;
  191. }
  192. }
  193. FILE * fp = fopen(nm.c_str(), "wb");
  194. if (fp == NULL) /* If an error occurs during the file creation */
  195. {
  196. fprintf(stderr, "fopen() failed for '%s'\n", nm.c_str());
  197. }
  198. else
  199. {
  200. fwrite(buf, sizeof(uint8_t), buf_len, fp);
  201. fclose(fp);
  202. }
  203. if (converted)
  204. free(buf);
  205. esp_camera_fb_return(fb);
  206. if (delay > 0)
  207. {
  208. LightOnOff(false);
  209. }
  210. return ESP_OK;
  211. }
  212. esp_err_t CCamera::CaptureToHTTP(httpd_req_t *req, int delay)
  213. {
  214. camera_fb_t * fb = NULL;
  215. esp_err_t res = ESP_OK;
  216. size_t fb_len = 0;
  217. int64_t fr_start = esp_timer_get_time();
  218. fb = esp_camera_fb_get();
  219. if (!fb) {
  220. ESP_LOGE(TAGCAMERACLASS, "Camera capture failed");
  221. httpd_resp_send_500(req);
  222. return ESP_FAIL;
  223. }
  224. res = httpd_resp_set_type(req, "image/jpeg");
  225. if(res == ESP_OK){
  226. res = httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
  227. }
  228. if(res == ESP_OK){
  229. if(fb->format == PIXFORMAT_JPEG){
  230. fb_len = fb->len;
  231. res = httpd_resp_send(req, (const char *)fb->buf, fb->len);
  232. } else {
  233. jpg_chunking_t jchunk = {req, 0};
  234. res = frame2jpg_cb(fb, 80, jpg_encode_stream, &jchunk)?ESP_OK:ESP_FAIL;
  235. httpd_resp_send_chunk(req, NULL, 0);
  236. fb_len = jchunk.len;
  237. }
  238. }
  239. esp_camera_fb_return(fb);
  240. int64_t fr_end = esp_timer_get_time();
  241. ESP_LOGI(TAGCAMERACLASS, "JPG: %uKB %ums", (uint32_t)(fb_len/1024), (uint32_t)((fr_end - fr_start)/1000));
  242. return res;
  243. }
  244. void CCamera::LightOnOff(bool status)
  245. {
  246. // Init the GPIO
  247. gpio_pad_select_gpio(FLASH_GPIO);
  248. /* Set the GPIO as a push/pull output */
  249. gpio_set_direction(FLASH_GPIO, GPIO_MODE_OUTPUT);
  250. if (status)
  251. gpio_set_level(FLASH_GPIO, 1);
  252. else
  253. gpio_set_level(FLASH_GPIO, 0);
  254. }
  255. void CCamera::GetCameraParameter(httpd_req_t *req, int &qual, framesize_t &resol)
  256. {
  257. char _query[100];
  258. char _qual[10];
  259. char _size[10];
  260. resol = ActualResolution;
  261. qual = ActualQuality;
  262. if (httpd_req_get_url_query_str(req, _query, 100) == ESP_OK)
  263. {
  264. printf("Query: "); printf(_query); printf("\n");
  265. if (httpd_query_key_value(_query, "size", _size, 10) == ESP_OK)
  266. {
  267. printf("Size: "); printf(_size); printf("\n");
  268. if (strcmp(_size, "QVGA") == 0)
  269. resol = FRAMESIZE_QVGA; // 320x240
  270. if (strcmp(_size, "VGA") == 0)
  271. resol = FRAMESIZE_VGA; // 640x480
  272. if (strcmp(_size, "SVGA") == 0)
  273. resol = FRAMESIZE_SVGA; // 800x600
  274. if (strcmp(_size, "XGA") == 0)
  275. resol = FRAMESIZE_XGA; // 1024x768
  276. if (strcmp(_size, "SXGA") == 0)
  277. resol = FRAMESIZE_SXGA; // 1280x1024
  278. if (strcmp(_size, "UXGA") == 0)
  279. resol = FRAMESIZE_UXGA; // 1600x1200
  280. }
  281. if (httpd_query_key_value(_query, "quality", _qual, 10) == ESP_OK)
  282. {
  283. printf("Quality: "); printf(_qual); printf("\n");
  284. qual = atoi(_qual);
  285. if (qual > 63)
  286. qual = 63;
  287. if (qual < 0)
  288. qual = 0;
  289. }
  290. };
  291. }
  292. framesize_t CCamera::TextToFramesize(const char * _size)
  293. {
  294. if (strcmp(_size, "QVGA") == 0)
  295. return FRAMESIZE_QVGA; // 320x240
  296. if (strcmp(_size, "VGA") == 0)
  297. return FRAMESIZE_VGA; // 640x480
  298. if (strcmp(_size, "SVGA") == 0)
  299. return FRAMESIZE_SVGA; // 800x600
  300. if (strcmp(_size, "XGA") == 0)
  301. return FRAMESIZE_XGA; // 1024x768
  302. if (strcmp(_size, "SXGA") == 0)
  303. return FRAMESIZE_SXGA; // 1280x1024
  304. if (strcmp(_size, "UXGA") == 0)
  305. return FRAMESIZE_UXGA; // 1600x1200
  306. return ActualResolution;
  307. }
  308. CCamera::CCamera()
  309. {
  310. printf("CreateClassCamera\n");
  311. }
  312. esp_err_t CCamera::InitCam()
  313. {
  314. printf("Init Flash\n");
  315. //power up the camera if PWDN pin is defined
  316. if(CAM_PIN_PWDN != -1){
  317. // Init the GPIO
  318. gpio_pad_select_gpio(CAM_PIN_PWDN);
  319. /* Set the GPIO as a push/pull output */
  320. gpio_set_direction(CAM_PIN_PWDN, GPIO_MODE_OUTPUT);
  321. gpio_set_level(CAM_PIN_PWDN, 0);
  322. }
  323. printf("Init Camera\n");
  324. ActualQuality = camera_config.jpeg_quality;
  325. ActualResolution = camera_config.frame_size;
  326. //initialize the camera
  327. esp_err_t err = esp_camera_init(&camera_config);
  328. if (err != ESP_OK) {
  329. ESP_LOGE(TAGCAMERACLASS, "Camera Init Failed");
  330. return err;
  331. }
  332. return ESP_OK;
  333. }