camera.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * This example takes a picture every 5s and print its size on serial monitor.
  3. */
  4. // =============================== SETUP ======================================
  5. // 1. Board setup (Uncomment):
  6. // #define BOARD_WROVER_KIT
  7. // #define BOARD_ESP32CAM_AITHINKER
  8. // #define BOARD_ESP32S3_WROOM
  9. /**
  10. * 2. Kconfig setup
  11. *
  12. * If you have a Kconfig file, copy the content from
  13. * https://github.com/espressif/esp32-camera/blob/master/Kconfig into it.
  14. * In case you haven't, copy and paste this Kconfig file inside the src directory.
  15. * This Kconfig file has definitions that allows more control over the camera and
  16. * how it will be initialized.
  17. */
  18. /**
  19. * 3. Enable PSRAM on sdkconfig:
  20. *
  21. * CONFIG_ESP32_SPIRAM_SUPPORT=y
  22. *
  23. * More info on
  24. * https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/kconfig.html#config-esp32-spiram-support
  25. */
  26. // ================================ CODE ======================================
  27. #include <esp_log.h>
  28. #include <esp_system.h>
  29. #include <nvs_flash.h>
  30. #include <sys/param.h>
  31. #include <string.h>
  32. #include "freertos/FreeRTOS.h"
  33. #include "freertos/task.h"
  34. // support IDF 5.x
  35. #ifndef portTICK_RATE_MS
  36. #define portTICK_RATE_MS portTICK_PERIOD_MS
  37. #endif
  38. #include "esp_camera.h"
  39. #define ESP32_S3_KORVO 1 //ESP32-S3-KORVO-2_V3.0配套的自定义引脚
  40. // WROVER-KIT PIN Map
  41. #ifdef BOARD_WROVER_KIT
  42. #define CAM_PIN_PWDN -1 //power down is not used
  43. #define CAM_PIN_RESET -1 //software reset will be performed
  44. #define CAM_PIN_XCLK 21
  45. #define CAM_PIN_SIOD 26
  46. #define CAM_PIN_SIOC 27
  47. #define CAM_PIN_D7 35
  48. #define CAM_PIN_D6 34
  49. #define CAM_PIN_D5 39
  50. #define CAM_PIN_D4 36
  51. #define CAM_PIN_D3 19
  52. #define CAM_PIN_D2 18
  53. #define CAM_PIN_D1 5
  54. #define CAM_PIN_D0 4
  55. #define CAM_PIN_VSYNC 25
  56. #define CAM_PIN_HREF 23
  57. #define CAM_PIN_PCLK 22
  58. #endif
  59. // ESP32Cam (AiThinker) PIN Map
  60. #ifdef BOARD_ESP32CAM_AITHINKER
  61. #define CAM_PIN_PWDN 32
  62. #define CAM_PIN_RESET -1 //software reset will be performed
  63. #define CAM_PIN_XCLK 0
  64. #define CAM_PIN_SIOD 26
  65. #define CAM_PIN_SIOC 27
  66. #define CAM_PIN_D7 35
  67. #define CAM_PIN_D6 34
  68. #define CAM_PIN_D5 39
  69. #define CAM_PIN_D4 36
  70. #define CAM_PIN_D3 21
  71. #define CAM_PIN_D2 19
  72. #define CAM_PIN_D1 18
  73. #define CAM_PIN_D0 5
  74. #define CAM_PIN_VSYNC 25
  75. #define CAM_PIN_HREF 23
  76. #define CAM_PIN_PCLK 22
  77. #endif
  78. // ESP32S3 (WROOM) PIN Map
  79. #ifdef BOARD_ESP32S3_WROOM
  80. #define CAM_PIN_PWDN 38
  81. #define CAM_PIN_RESET -1 //software reset will be performed
  82. #define CAM_PIN_VSYNC 6
  83. #define CAM_PIN_HREF 7
  84. #define CAM_PIN_PCLK 13
  85. #define CAM_PIN_XCLK 15
  86. #define CAM_PIN_SIOD 4
  87. #define CAM_PIN_SIOC 5
  88. #define CAM_PIN_D0 11
  89. #define CAM_PIN_D1 9
  90. #define CAM_PIN_D2 8
  91. #define CAM_PIN_D3 10
  92. #define CAM_PIN_D4 12
  93. #define CAM_PIN_D5 18
  94. #define CAM_PIN_D6 17
  95. #define CAM_PIN_D7 16
  96. #endif
  97. // ESP32-S3-KORVO-2_V3.0 音频开发版
  98. #ifdef ESP32_S3_KORVO
  99. #define CAM_PIN_PWDN -1 //没用直接接高
  100. #define CAM_PIN_RESET -1 //software reset will be performed
  101. #define CAM_PIN_VSYNC 21
  102. #define CAM_PIN_HREF 38
  103. #define CAM_PIN_PCLK 11
  104. #define CAM_PIN_XCLK 40
  105. #define CAM_PIN_SIOD 17
  106. #define CAM_PIN_SIOC 18
  107. #define CAM_PIN_D0 13
  108. #define CAM_PIN_D1 47
  109. #define CAM_PIN_D2 14
  110. #define CAM_PIN_D3 3
  111. #define CAM_PIN_D4 12
  112. #define CAM_PIN_D5 42
  113. #define CAM_PIN_D6 41
  114. #define CAM_PIN_D7 39
  115. #endif
  116. static const char *TAG = "example:take_picture";
  117. #if ESP_CAMERA_SUPPORTED
  118. static camera_config_t camera_config = {
  119. .pin_pwdn = CAM_PIN_PWDN,
  120. .pin_reset = CAM_PIN_RESET,
  121. .pin_xclk = CAM_PIN_XCLK,
  122. .pin_sccb_sda = CAM_PIN_SIOD,
  123. .pin_sccb_scl = CAM_PIN_SIOC,
  124. .pin_d7 = CAM_PIN_D7,
  125. .pin_d6 = CAM_PIN_D6,
  126. .pin_d5 = CAM_PIN_D5,
  127. .pin_d4 = CAM_PIN_D4,
  128. .pin_d3 = CAM_PIN_D3,
  129. .pin_d2 = CAM_PIN_D2,
  130. .pin_d1 = CAM_PIN_D1,
  131. .pin_d0 = CAM_PIN_D0,
  132. .pin_vsync = CAM_PIN_VSYNC,
  133. .pin_href = CAM_PIN_HREF,
  134. .pin_pclk = CAM_PIN_PCLK,
  135. //XCLK 20MHz or 10MHz for OV2640 double FPS (Experimental)
  136. .xclk_freq_hz = 20000000,
  137. .ledc_timer = LEDC_TIMER_0,
  138. .ledc_channel = LEDC_CHANNEL_0,
  139. .pixel_format = PIXFORMAT_RGB565, //YUV422,GRAYSCALE,RGB565,JPEG
  140. .frame_size = FRAMESIZE_QVGA, //QQVGA-UXGA, For ESP32, do not use sizes above QVGA when not JPEG. The performance of the ESP32-S series has improved a lot, but JPEG mode always gives better frame rates.
  141. .jpeg_quality = 12, //0-63, for OV series camera sensors, lower number means higher quality
  142. .fb_count = 1, //When jpeg mode is used, if fb_count more than one, the driver will work in continuous mode.
  143. .fb_location = CAMERA_FB_IN_DRAM,
  144. .grab_mode = CAMERA_GRAB_WHEN_EMPTY,
  145. };
  146. static esp_err_t init_camera(void)
  147. {
  148. //initialize the camera
  149. esp_err_t err = esp_camera_init(&camera_config);
  150. if (err != ESP_OK)
  151. {
  152. ESP_LOGE(TAG, "Camera Init Failed");
  153. return err;
  154. }
  155. return ESP_OK;
  156. }
  157. #endif
  158. void take_photo(void)
  159. {
  160. #if ESP_CAMERA_SUPPORTED
  161. if(ESP_OK != init_camera()) {
  162. return;
  163. }
  164. while (1)
  165. {
  166. ESP_LOGI(TAG, "Taking picture...");
  167. camera_fb_t *pic = esp_camera_fb_get();
  168. if ( pic == NULL)
  169. {
  170. /* code */
  171. ESP_LOGE(TAG, "Camera capture failed");
  172. return;
  173. }
  174. // use pic->buf to access the image
  175. ESP_LOGI(TAG, "Picture taken! Its size was: %zu bytes", pic->len);
  176. esp_camera_fb_return(pic);
  177. vTaskDelay(5000 / portTICK_RATE_MS);
  178. }
  179. #else
  180. ESP_LOGE(TAG, "Camera support is not available for this chip");
  181. return;
  182. #endif
  183. }