read-and-draw.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This example code reads frames from a camera and draws it to the screen.
  3. *
  4. * This is a very simple approach that is often Good Enough. You can get
  5. * fancier with this: multiple cameras, front/back facing cameras on phones,
  6. * color spaces, choosing formats and framerates...this just requests
  7. * _anything_ and goes with what it is handed.
  8. *
  9. * This code is public domain. Feel free to use it for any purpose!
  10. */
  11. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  12. #include <SDL3/SDL.h>
  13. #include <SDL3/SDL_main.h>
  14. /* We will use this renderer to draw into this window every frame. */
  15. static SDL_Window *window = NULL;
  16. static SDL_Renderer *renderer = NULL;
  17. static SDL_Camera *camera = NULL;
  18. static SDL_Texture *texture = NULL;
  19. /* This function runs once at startup. */
  20. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  21. {
  22. SDL_CameraID *devices = NULL;
  23. int devcount = 0;
  24. SDL_SetAppMetadata("Example Camera Read and Draw", "1.0", "com.example.camera-read-and-draw");
  25. if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_CAMERA)) {
  26. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  27. return SDL_APP_FAILURE;
  28. }
  29. if (!SDL_CreateWindowAndRenderer("examples/camera/read-and-draw", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  30. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  31. return SDL_APP_FAILURE;
  32. }
  33. devices = SDL_GetCameras(&devcount);
  34. if (devices == NULL) {
  35. SDL_Log("Couldn't enumerate camera devices: %s", SDL_GetError());
  36. return SDL_APP_FAILURE;
  37. } else if (devcount == 0) {
  38. SDL_Log("Couldn't find any camera devices! Please connect a camera and try again.");
  39. SDL_free(devices);
  40. return SDL_APP_FAILURE;
  41. }
  42. camera = SDL_OpenCamera(devices[0], NULL); // just take the first thing we see in any format it wants.
  43. SDL_free(devices);
  44. if (camera == NULL) {
  45. SDL_Log("Couldn't open camera: %s", SDL_GetError());
  46. return SDL_APP_FAILURE;
  47. }
  48. return SDL_APP_CONTINUE; /* carry on with the program! */
  49. }
  50. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  51. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  52. {
  53. if (event->type == SDL_EVENT_QUIT) {
  54. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  55. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) {
  56. SDL_Log("Camera use approved by user!");
  57. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) {
  58. SDL_Log("Camera use denied by user!");
  59. return SDL_APP_FAILURE;
  60. }
  61. return SDL_APP_CONTINUE; /* carry on with the program! */
  62. }
  63. /* This function runs once per frame, and is the heart of the program. */
  64. SDL_AppResult SDL_AppIterate(void *appstate)
  65. {
  66. Uint64 timestampNS = 0;
  67. SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS);
  68. if (frame != NULL) {
  69. /* Some platforms (like Emscripten) don't know _what_ the camera offers
  70. until the user gives permission, so we build the texture and resize
  71. the window when we get a first frame from the camera. */
  72. if (!texture) {
  73. SDL_SetWindowSize(window, frame->w, frame->h); /* Resize the window to match */
  74. SDL_SetRenderLogicalPresentation(renderer, frame->w, frame->h, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  75. texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h);
  76. }
  77. if (texture) {
  78. SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch);
  79. }
  80. SDL_ReleaseCameraFrame(camera, frame);
  81. }
  82. SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, SDL_ALPHA_OPAQUE);
  83. SDL_RenderClear(renderer);
  84. if (texture) { /* draw the latest camera frame, if available. */
  85. SDL_RenderTexture(renderer, texture, NULL, NULL);
  86. }
  87. SDL_RenderPresent(renderer);
  88. return SDL_APP_CONTINUE; /* carry on with the program! */
  89. }
  90. /* This function runs once at shutdown. */
  91. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  92. {
  93. SDL_CloseCamera(camera);
  94. SDL_DestroyTexture(texture);
  95. /* SDL will clean up the window/renderer for us. */
  96. }