ClassControllCamera.cpp 12 KB

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