Trait nokhwa::camera_traits::CaptureBackendTrait
source · pub trait CaptureBackendTrait {
Show 24 methods
// Required methods
fn backend(&self) -> ApiBackend;
fn camera_info(&self) -> &CameraInfo;
fn refresh_camera_format(&mut self) -> Result<(), NokhwaError>;
fn camera_format(&self) -> CameraFormat;
fn set_camera_format(
&mut self,
new_fmt: CameraFormat
) -> Result<(), NokhwaError>;
fn compatible_list_by_resolution(
&mut self,
fourcc: FrameFormat
) -> Result<HashMap<Resolution, Vec<u32, Global>, RandomState>, NokhwaError>;
fn compatible_fourcc(
&mut self
) -> Result<Vec<FrameFormat, Global>, NokhwaError>;
fn resolution(&self) -> Resolution;
fn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError>;
fn frame_rate(&self) -> u32;
fn set_frame_rate(&mut self, new_fps: u32) -> Result<(), NokhwaError>;
fn frame_format(&self) -> FrameFormat;
fn set_frame_format(
&mut self,
fourcc: FrameFormat
) -> Result<(), NokhwaError>;
fn camera_control(
&self,
control: KnownCameraControl
) -> Result<CameraControl, NokhwaError>;
fn camera_controls(&self) -> Result<Vec<CameraControl, Global>, NokhwaError>;
fn set_camera_control(
&mut self,
id: KnownCameraControl,
value: ControlValueSetter
) -> Result<(), NokhwaError>;
fn open_stream(&mut self) -> Result<(), NokhwaError>;
fn is_stream_open(&self) -> bool;
fn frame(&mut self) -> Result<Buffer, NokhwaError>;
fn frame_raw(&mut self) -> Result<Cow<'_, [u8]>, NokhwaError>;
fn stop_stream(&mut self) -> Result<(), NokhwaError>;
// Provided methods
fn compatible_camera_formats(
&mut self
) -> Result<Vec<CameraFormat, Global>, NokhwaError> { ... }
fn decoded_buffer_size(&self, alpha: bool) -> usize { ... }
fn frame_texture<'a>(
&mut self,
device: &Device,
queue: &Queue,
label: Option<&'a str>
) -> Result<Texture, NokhwaError> { ... }
}
Expand description
This trait is for any backend that allows you to grab and take frames from a camera. Many of the backends are blocking, if the camera is occupied the library will block while it waits for it to become available.
Note:
- Backends, if not provided with a camera format, will be spawned with 640x480@15 FPS, MJPEG
CameraFormat
. - Behaviour can differ from backend to backend. While the Camera struct abstracts most of this away, if you plan to use the raw backend structs please read the
Quirks
section of each backend. - If you call
stop_stream()
, you will usually need to callopen_stream()
to get more frames from the camera.
Required Methods§
sourcefn backend(&self) -> ApiBackend
fn backend(&self) -> ApiBackend
Returns the current backend used.
sourcefn camera_info(&self) -> &CameraInfo
fn camera_info(&self) -> &CameraInfo
Gets the camera information such as Name and Index as a CameraInfo
.
sourcefn refresh_camera_format(&mut self) -> Result<(), NokhwaError>
fn refresh_camera_format(&mut self) -> Result<(), NokhwaError>
Forcefully refreshes the stored camera format, bringing it into sync with “reality” (current camera state)
Errors
If the camera can not get its most recent CameraFormat
. this will error.
sourcefn camera_format(&self) -> CameraFormat
fn camera_format(&self) -> CameraFormat
Gets the current CameraFormat
. This will force refresh to the current latest if it has changed.
sourcefn set_camera_format(
&mut self,
new_fmt: CameraFormat
) -> Result<(), NokhwaError>
fn set_camera_format( &mut self, new_fmt: CameraFormat ) -> Result<(), NokhwaError>
Will set the current CameraFormat
This will reset the current stream if used while stream is opened.
This will also update the cache.
Errors
If you started the stream and the camera rejects the new camera format, this will return an error.
sourcefn compatible_list_by_resolution(
&mut self,
fourcc: FrameFormat
) -> Result<HashMap<Resolution, Vec<u32, Global>, RandomState>, NokhwaError>
fn compatible_list_by_resolution( &mut self, fourcc: FrameFormat ) -> Result<HashMap<Resolution, Vec<u32, Global>, RandomState>, NokhwaError>
A hashmap of Resolution
s mapped to framerates. Not sorted!
Errors
This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a Unsupported Operation (UnsupportedOperationError
).
sourcefn compatible_fourcc(&mut self) -> Result<Vec<FrameFormat, Global>, NokhwaError>
fn compatible_fourcc(&mut self) -> Result<Vec<FrameFormat, Global>, NokhwaError>
A Vector of compatible FrameFormat
s. Will only return 2 elements at most.
Errors
This will error if the camera is not queryable or a query operation has failed. Some backends will error this out as a Unsupported Operation (UnsupportedOperationError
).
sourcefn resolution(&self) -> Resolution
fn resolution(&self) -> Resolution
Gets the current camera resolution (See: Resolution
, CameraFormat
). This will force refresh to the current latest if it has changed.
sourcefn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError>
fn set_resolution(&mut self, new_res: Resolution) -> Result<(), NokhwaError>
Will set the current Resolution
This will reset the current stream if used while stream is opened.
This will also update the cache.
Errors
If you started the stream and the camera rejects the new resolution, this will return an error.
sourcefn frame_rate(&self) -> u32
fn frame_rate(&self) -> u32
Gets the current camera framerate (See: CameraFormat
). This will force refresh to the current latest if it has changed.
sourcefn set_frame_rate(&mut self, new_fps: u32) -> Result<(), NokhwaError>
fn set_frame_rate(&mut self, new_fps: u32) -> Result<(), NokhwaError>
Will set the current framerate This will reset the current stream if used while stream is opened.
This will also update the cache.
Errors
If you started the stream and the camera rejects the new framerate, this will return an error.
sourcefn frame_format(&self) -> FrameFormat
fn frame_format(&self) -> FrameFormat
Gets the current camera’s frame format (See: FrameFormat
, CameraFormat
). This will force refresh to the current latest if it has changed.
sourcefn set_frame_format(&mut self, fourcc: FrameFormat) -> Result<(), NokhwaError>
fn set_frame_format(&mut self, fourcc: FrameFormat) -> Result<(), NokhwaError>
Will set the current FrameFormat
This will reset the current stream if used while stream is opened.
This will also update the cache.
Errors
If you started the stream and the camera rejects the new frame format, this will return an error.
sourcefn camera_control(
&self,
control: KnownCameraControl
) -> Result<CameraControl, NokhwaError>
fn camera_control( &self, control: KnownCameraControl ) -> Result<CameraControl, NokhwaError>
Gets the value of KnownCameraControl
.
Errors
If the control
is not supported or there is an error while getting the camera control values (e.g. unexpected value, too high, etc)
this will error.
sourcefn camera_controls(&self) -> Result<Vec<CameraControl, Global>, NokhwaError>
fn camera_controls(&self) -> Result<Vec<CameraControl, Global>, NokhwaError>
Gets the current supported list of KnownCameraControl
Errors
If the list cannot be collected, this will error. This can be treated as a “nothing supported”.
sourcefn set_camera_control(
&mut self,
id: KnownCameraControl,
value: ControlValueSetter
) -> Result<(), NokhwaError>
fn set_camera_control( &mut self, id: KnownCameraControl, value: ControlValueSetter ) -> Result<(), NokhwaError>
Sets the control to control
in the camera.
Usually, the pipeline is calling camera_control()
, getting a camera control that way
then calling value()
to get a ControlValueSetter
and setting the value that way.
Errors
If the control
is not supported, the value is invalid (less than min, greater than max, not in step), or there was an error setting the control,
this will error.
sourcefn open_stream(&mut self) -> Result<(), NokhwaError>
fn open_stream(&mut self) -> Result<(), NokhwaError>
Will open the camera stream with set parameters. This will be called internally if you try and call frame()
before you call open_stream()
.
Errors
If the specific backend fails to open the camera (e.g. already taken, busy, doesn’t exist anymore) this will error.
sourcefn is_stream_open(&self) -> bool
fn is_stream_open(&self) -> bool
Checks if stream if open. If it is, it will return true.
sourcefn frame(&mut self) -> Result<Buffer, NokhwaError>
fn frame(&mut self) -> Result<Buffer, NokhwaError>
Will get a frame from the camera as a Buffer
. Depending on the backend, if you have not called open_stream()
before you called this,
it will either return an error.
Errors
If the backend fails to get the frame (e.g. already taken, busy, doesn’t exist anymore), the decoding fails (e.g. MJPEG -> u8), or open_stream()
has not been called yet,
this will error.
sourcefn frame_raw(&mut self) -> Result<Cow<'_, [u8]>, NokhwaError>
fn frame_raw(&mut self) -> Result<Cow<'_, [u8]>, NokhwaError>
Will get a frame from the camera without any processing applied, meaning you will usually get a frame you need to decode yourself.
Errors
If the backend fails to get the frame (e.g. already taken, busy, doesn’t exist anymore), or open_stream()
has not been called yet, this will error.
sourcefn stop_stream(&mut self) -> Result<(), NokhwaError>
fn stop_stream(&mut self) -> Result<(), NokhwaError>
Provided Methods§
sourcefn compatible_camera_formats(
&mut self
) -> Result<Vec<CameraFormat, Global>, NokhwaError>
fn compatible_camera_formats( &mut self ) -> Result<Vec<CameraFormat, Global>, NokhwaError>
sourcefn decoded_buffer_size(&self, alpha: bool) -> usize
fn decoded_buffer_size(&self, alpha: bool) -> usize
The minimum buffer size needed to write the current frame. If alpha
is true, it will instead return the minimum size of the buffer with an alpha channel as well.
This assumes that you are decoding to RGB/RGBA for FrameFormat::MJPEG
or FrameFormat::YUYV
and Luma8/LumaA8 for FrameFormat::GRAY
sourcefn frame_texture<'a>(
&mut self,
device: &Device,
queue: &Queue,
label: Option<&'a str>
) -> Result<Texture, NokhwaError>
fn frame_texture<'a>( &mut self, device: &Device, queue: &Queue, label: Option<&'a str> ) -> Result<Texture, NokhwaError>
wgpu-types
only.Directly copies a frame to a Wgpu texture. This will automatically convert the frame into a RGBA frame.
Errors
If the frame cannot be captured or the resolution is 0 on any axis, this will error.