read-and-draw.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, 0, &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. return SDL_APP_FAILURE;
  40. }
  41. camera = SDL_OpenCamera(devices[0], NULL); // just take the first thing we see in any format it wants.
  42. SDL_free(devices);
  43. if (camera == NULL) {
  44. SDL_Log("Couldn't open camera: %s", SDL_GetError());
  45. return SDL_APP_FAILURE;
  46. }
  47. return SDL_APP_CONTINUE; /* carry on with the program! */
  48. }
  49. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  50. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  51. {
  52. if (event->type == SDL_EVENT_QUIT) {
  53. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  54. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_APPROVED) {
  55. SDL_Log("Camera use approved by user!");
  56. } else if (event->type == SDL_EVENT_CAMERA_DEVICE_DENIED) {
  57. SDL_Log("Camera use denied by user!");
  58. return SDL_APP_FAILURE;
  59. }
  60. return SDL_APP_CONTINUE; /* carry on with the program! */
  61. }
  62. /* This function runs once per frame, and is the heart of the program. */
  63. SDL_AppResult SDL_AppIterate(void *appstate)
  64. {
  65. Uint64 timestampNS = 0;
  66. SDL_Surface *frame = SDL_AcquireCameraFrame(camera, &timestampNS);
  67. if (frame != NULL) {
  68. /* Some platforms (like Emscripten) don't know _what_ the camera offers
  69. until the user gives permission, so we build the texture and resize
  70. the window when we get a first frame from the camera. */
  71. if (!texture) {
  72. SDL_SetWindowSize(window, frame->w, frame->h); /* Resize the window to match */
  73. texture = SDL_CreateTexture(renderer, frame->format, SDL_TEXTUREACCESS_STREAMING, frame->w, frame->h);
  74. }
  75. if (texture) {
  76. SDL_UpdateTexture(texture, NULL, frame->pixels, frame->pitch);
  77. }
  78. SDL_ReleaseCameraFrame(camera, frame);
  79. }
  80. SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, SDL_ALPHA_OPAQUE);
  81. SDL_RenderClear(renderer);
  82. if (texture) { /* draw the latest camera frame, if available. */
  83. SDL_RenderTexture(renderer, texture, NULL, NULL);
  84. }
  85. SDL_RenderPresent(renderer);
  86. return SDL_APP_CONTINUE; /* carry on with the program! */
  87. }
  88. /* This function runs once at shutdown. */
  89. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  90. {
  91. SDL_CloseCamera(camera);
  92. SDL_DestroyTexture(texture);
  93. /* SDL will clean up the window/renderer for us. */
  94. }