ClassControllCamera.cpp 12 KB

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