On this page:
20.1 Driver and Device Enumeration
camera-driver-count
camera-driver-name
current-camera-driver
get-cameras
camera-name
camera-position
camera-supported-formats
20.2 Camera Specs
camera-spec
make-camera-spec
20.3 Opening Cameras
open-camera
camera?
camera-ptr
camera-destroy!
20.4 Camera Information
camera-permission-state
camera-id
camera-properties
camera-format
20.5 Frame Capture
camera-acquire-frame
camera-frame?
camera-frame-release!
camera-frame->texture
camera-frame-surface
camera-frame-timestamp-ns
camera-frame-width
camera-frame-height
camera-frame-pitch
camera-frame-format
9.0.0.11

20 Camera🔗ℹ

This section covers camera (webcam) capture.

20.1 Driver and Device Enumeration🔗ℹ

Returns the number of available camera drivers.

procedure

(camera-driver-name index)  (or/c string? #f)

  index : exact-nonnegative-integer?
Returns the name of a camera driver.

procedure

(current-camera-driver)  (or/c string? #f)

Returns the name of the current camera driver.

Returns a list of available camera instance IDs.

(for ([id (get-cameras)])
  (printf "Camera: ~a (~a)~n"
          (camera-name id)
          (camera-position id)))

procedure

(camera-name instance-id)  (or/c string? #f)

  instance-id : exact-nonnegative-integer?
Returns the name of a camera.

procedure

(camera-position instance-id)  symbol?

  instance-id : exact-nonnegative-integer?
Returns the physical position of a camera.

Values: 'unknown, 'front-facing, 'back-facing.

procedure

(camera-supported-formats instance-id)  (listof camera-spec?)

  instance-id : exact-nonnegative-integer?
Returns a list of supported camera formats.

20.2 Camera Specs🔗ℹ

Camera specs describe the format and resolution of a camera.

struct

(struct camera-spec (format
    colorspace
    width
    height
    framerate-numerator
    framerate-denominator))
  format : exact-nonnegative-integer?
  colorspace : exact-nonnegative-integer?
  width : exact-nonnegative-integer?
  height : exact-nonnegative-integer?
  framerate-numerator : exact-nonnegative-integer?
  framerate-denominator : exact-nonnegative-integer?
A camera specification describing format and resolution.

procedure

(make-camera-spec format    
  width    
  height    
  [#:colorspace colorspace    
  #:framerate-numerator num    
  #:framerate-denominator den])  camera-spec?
  format : exact-nonnegative-integer?
  width : exact-nonnegative-integer?
  height : exact-nonnegative-integer?
  colorspace : exact-nonnegative-integer? = 0
  num : exact-nonnegative-integer? = 0
  den : exact-nonnegative-integer? = 0
Creates a camera spec.

20.3 Opening Cameras🔗ℹ

procedure

(open-camera instance-id    
  [#:spec spec    
  #:custodian cust])  camera?
  instance-id : exact-nonnegative-integer?
  spec : (or/c camera-spec? #f) = #f
  cust : custodian? = (current-custodian)
Opens a camera.

If spec is provided, requests that specific format.

(define cameras (get-cameras))
(when (pair? cameras)
  (define cam (open-camera (car cameras))))

procedure

(camera? v)  boolean?

  v : any/c
Returns #t if v is a camera.

procedure

(camera-ptr cam)  cpointer?

  cam : camera?
Returns the underlying SDL camera pointer.

procedure

(camera-destroy! cam)  void?

  cam : camera?
Closes a camera.

Note: Cameras are automatically closed when their custodian shuts down.

20.4 Camera Information🔗ℹ

procedure

(camera-permission-state cam)  symbol?

  cam : camera?
Returns the camera permission state.

Values: 'approved, 'denied, 'pending.

On some platforms, you must wait for permission before capturing frames.

procedure

(camera-id cam)  exact-nonnegative-integer?

  cam : camera?
Returns the camera’s instance ID.

procedure

(camera-properties cam)  exact-nonnegative-integer?

  cam : camera?
Returns the camera’s properties ID.

procedure

(camera-format cam)  (or/c camera-spec? #f)

  cam : camera?
Returns the actual format being used.

20.5 Frame Capture🔗ℹ

procedure

(camera-acquire-frame cam)  (or/c camera-frame? #f)

  cam : camera?
Acquires the next available frame.

Returns #f if no frame is available yet.

(let loop ()
  (define frame (camera-acquire-frame cam))
  (cond
    [frame
     ;; Process the frame
     (define tex (camera-frame->texture ren frame))
     (render-texture! ren tex 0 0)
     (camera-frame-release! frame)]
    [else
     (delay! 10)])
  (loop))

procedure

(camera-frame? v)  boolean?

  v : any/c
Returns #t if v is a camera frame.

procedure

(camera-frame-release! frame)  void?

  frame : camera-frame?
Releases a frame back to the camera.

You must release frames after processing to allow new frames to be captured.

procedure

(camera-frame->texture renderer    
  frame    
  [#:custodian cust])  texture?
  renderer : renderer?
  frame : camera-frame?
  cust : custodian? = (current-custodian)
Creates a texture from a camera frame.

procedure

(camera-frame-surface frame)  cpointer?

  frame : camera-frame?
Returns the underlying surface pointer.

Returns the frame timestamp in nanoseconds.

procedure

(camera-frame-width frame)  exact-integer?

  frame : camera-frame?
Returns the frame width in pixels.

procedure

(camera-frame-height frame)  exact-integer?

  frame : camera-frame?
Returns the frame height in pixels.

procedure

(camera-frame-pitch frame)  exact-integer?

  frame : camera-frame?
Returns the frame pitch (bytes per row).

procedure

(camera-frame-format frame)  exact-nonnegative-integer?

  frame : camera-frame?
Returns the frame pixel format.