pub struct JSCamera { /* private fields */ }
input-jscam
only.Expand description
Implementations§
source§impl JSCamera
impl JSCamera
sourcepub fn constraints(&self) -> JSCameraConstraints
pub fn constraints(&self) -> JSCameraConstraints
Gets the internal JSCameraConstraints
.
Most likely, you will edit this value by taking ownership of it, then feed it back into set_constraints
.
JS-WASM
This is exported as get_Constraints
.
sourcepub fn resolution(&self) -> Resolution
pub fn resolution(&self) -> Resolution
Gets the internal Resolution
.
Note: This value is only updated after you call measure_resolution
JS-WASM
This is exported as get_Resolution
.
sourcepub fn media_stream(&self) -> MediaStream
pub fn media_stream(&self) -> MediaStream
sourcepub fn js_frame_raw(&mut self) -> Result<Box<[u8]>, JsValue>
pub fn js_frame_raw(&mut self) -> Result<Box<[u8]>, JsValue>
Creates an off-screen canvas and a <video>
element (if not already attached) and returns a raw Cow<[u8]>
RGBA frame.
Errors
If a cast fails, the camera fails to attach, the currently attached node is invalid, or writing/reading from the canvas fails, this will error.
JS-WASM
This is exported as captureFrameRawData
. This may throw an error.
source§impl JSCamera
impl JSCamera
sourcepub async fn new(constraints: JSCameraConstraints) -> Result<Self, NokhwaError>
pub async fn new(constraints: JSCameraConstraints) -> Result<Self, NokhwaError>
Creates a new JSCamera
using JSCameraConstraints
.
Errors
This may error if permission is not granted, or the constraints are invalid.
sourcepub fn apply_constraints(&mut self) -> Result<(), NokhwaError>
pub fn apply_constraints(&mut self) -> Result<(), NokhwaError>
Applies any modified constraints.
Errors
This function may return an error on failing to measure the resolution. Please check measure_resolution()
for details.
sourcepub fn set_constraints(
&mut self,
constraints: JSCameraConstraints
) -> Result<(), NokhwaError>
pub fn set_constraints( &mut self, constraints: JSCameraConstraints ) -> Result<(), NokhwaError>
Sets the JSCameraConstraints
. This calls apply_constraints
internally.
Errors
See apply_constraints
.
sourcepub fn measure_resolution(&mut self) -> Result<(), NokhwaError>
pub fn measure_resolution(&mut self) -> Result<(), NokhwaError>
Measures the Resolution
of the internal stream. You usually do not need to call this.
Errors
If the camera fails to attach to the created <video>
, this will error.
sourcepub fn attach(
&mut self,
html_id: &str,
generate_new: bool
) -> Result<(), NokhwaError>
pub fn attach( &mut self, html_id: &str, generate_new: bool ) -> Result<(), NokhwaError>
Attaches camera to a html_id
(by-id).
If generate_new
is true, the generated element will have an Id of html_id
+-video
. For example, if you pass “nokhwaisbest” for html_id
, the new <video>
’s ID will be “nokhwaisbest-video”.
Errors
If the camera fails to attach, fails to generate the video element, or a cast fails, this will error.
sourcepub fn detach(&mut self) -> Result<(), NokhwaError>
pub fn detach(&mut self) -> Result<(), NokhwaError>
Detaches the camera from the <video>
node.
Errors
If the casting fails (the stored node is not a <video>
) this will error.
sourcepub fn frame_canvas_copy(
&mut self,
html_id: &str,
generate_new: bool
) -> Result<(HtmlCanvasElement, CanvasRenderingContext2d), NokhwaError>
pub fn frame_canvas_copy( &mut self, html_id: &str, generate_new: bool ) -> Result<(HtmlCanvasElement, CanvasRenderingContext2d), NokhwaError>
Copies camera frame to a html_id
(by-id, canvas).
If generate_new
is true, the generated element will have an Id of html_id
+-canvas
. For example, if you pass “nokhwaisbest” for html_id
, the new <canvas>
’s ID will be “nokhwaisbest-canvas”.
Errors
If the internal canvas is not here, drawing fails, or a cast fails, this will error.
sourcepub fn frame_image_data(&mut self) -> Result<ImageData, NokhwaError>
pub fn frame_image_data(&mut self) -> Result<ImageData, NokhwaError>
sourcepub fn frame_uri(
&mut self,
mime_type: Option<&str>,
image_quality: Option<f64>
) -> Result<String, NokhwaError>
pub fn frame_uri( &mut self, mime_type: Option<&str>, image_quality: Option<f64> ) -> Result<String, NokhwaError>
Captures an ImageData
MDN
and then returns its URL
as a string.
mime_type
: The mime type of the resulting URI. It isimage/png
by default (lossless) but can be set toimage/jpeg
orimage/webp
(lossy). Anything else is ignored.image_quality
: A number between0
and1
indicating the resulting image quality in case you are using a lossy image mime type. The default value is 0.92, and all other values are ignored.
Errors
If drawing to the canvas fails or URI generation is not supported or fails this will error.
sourcepub fn frame_raw(&mut self) -> Result<Cow<'_, [u8]>, NokhwaError>
pub fn frame_raw(&mut self) -> Result<Cow<'_, [u8]>, NokhwaError>
Creates an off-screen canvas and a <video>
element (if not already attached) and returns a raw Cow<[u8]>
RGBA frame.
Errors
If a cast fails, the camera fails to attach, the currently attached node is invalid, or writing/reading from the canvas fails, this will error.
sourcepub fn frame(&mut self) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, NokhwaError>
pub fn frame(&mut self) -> Result<ImageBuffer<Rgb<u8>, Vec<u8>>, NokhwaError>
This takes the output from frame_raw()
and turns it into an ImageBuffer<Rgb<u8>, Vec<u8>>
.
Errors
This will error if the frame vec is too small(this is probably a bug, please report it!) or if the frame fails to capture. See frame_raw()
.
sourcepub fn rgba_frame(
&mut self
) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, NokhwaError>
pub fn rgba_frame( &mut self ) -> Result<ImageBuffer<Rgba<u8>, Vec<u8>>, NokhwaError>
This takes the output from frame_raw()
and turns it into an ImageBuffer<Rgba<u8>, Vec<u8>>
.
Errors
This will error if the frame vec is too small(this is probably a bug, please report it!) or if the frame fails to capture. See frame_raw()
.
sourcepub fn min_buffer_size(&self, use_rgba: bool) -> usize
pub fn min_buffer_size(&self, use_rgba: bool) -> usize
The minimum buffer size needed to write the current frame (RGB24). If use_rgba
is true, it will instead return the minimum size of the RGBA buffer needed.
sourcepub fn write_frame_to_buffer(
&mut self,
buffer: &mut [u8],
convert_rgba: bool
) -> Result<usize, NokhwaError>
pub fn write_frame_to_buffer( &mut self, buffer: &mut [u8], convert_rgba: bool ) -> Result<usize, NokhwaError>
Directly writes the current frame(RGB24) into said buffer
. If convert_rgba
is true, the buffer written will be written as an RGBA frame instead of a RGB frame. Returns the amount of bytes written on successful capture.
Errors
If reading the frame fails, this will error. See frame_raw()
.
sourcepub fn frame_texture<'a>(
&mut self,
device: &Device,
queue: &Queue,
label: Option<&'a str>
) -> Result<Texture, NokhwaError>
pub fn frame_texture<'a>( &mut self, device: &Device, queue: &Queue, label: Option<&'a str> ) -> Result<Texture, NokhwaError>
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.
Methods from Deref<Target = MediaStream>§
sourcepub fn id(&self) -> String
pub fn id(&self) -> String
Getter for the id
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn active(&self) -> bool
pub fn active(&self) -> bool
Getter for the active
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn onaddtrack(&self) -> Option<Function>
pub fn onaddtrack(&self) -> Option<Function>
Getter for the onaddtrack
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn set_onaddtrack(&self, value: Option<&Function>)
pub fn set_onaddtrack(&self, value: Option<&Function>)
Setter for the onaddtrack
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn onremovetrack(&self) -> Option<Function>
pub fn onremovetrack(&self) -> Option<Function>
Getter for the onremovetrack
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn set_onremovetrack(&self, value: Option<&Function>)
pub fn set_onremovetrack(&self, value: Option<&Function>)
Setter for the onremovetrack
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn current_time(&self) -> f64
pub fn current_time(&self) -> f64
Getter for the currentTime
field of this object.
This API requires the following crate features to be activated: MediaStream
sourcepub fn add_track(&self, track: &MediaStreamTrack)
pub fn add_track(&self, track: &MediaStreamTrack)
The addTrack()
method.
This API requires the following crate features to be activated: MediaStream
, MediaStreamTrack
sourcepub fn clone(&self) -> MediaStream
pub fn clone(&self) -> MediaStream
The clone()
method.
This API requires the following crate features to be activated: MediaStream
sourcepub fn get_audio_tracks(&self) -> Array
pub fn get_audio_tracks(&self) -> Array
The getAudioTracks()
method.
This API requires the following crate features to be activated: MediaStream
sourcepub fn get_track_by_id(&self, track_id: &str) -> Option<MediaStreamTrack>
pub fn get_track_by_id(&self, track_id: &str) -> Option<MediaStreamTrack>
The getTrackById()
method.
This API requires the following crate features to be activated: MediaStream
, MediaStreamTrack
sourcepub fn get_tracks(&self) -> Array
pub fn get_tracks(&self) -> Array
The getTracks()
method.
This API requires the following crate features to be activated: MediaStream
sourcepub fn get_video_tracks(&self) -> Array
pub fn get_video_tracks(&self) -> Array
The getVideoTracks()
method.
This API requires the following crate features to be activated: MediaStream
sourcepub fn remove_track(&self, track: &MediaStreamTrack)
pub fn remove_track(&self, track: &MediaStreamTrack)
The removeTrack()
method.
This API requires the following crate features to be activated: MediaStream
, MediaStreamTrack
Methods from Deref<Target = EventTarget>§
sourcepub fn add_event_listener_with_callback(
&self,
type_: &str,
listener: &Function
) -> Result<(), JsValue>
pub fn add_event_listener_with_callback( &self, type_: &str, listener: &Function ) -> Result<(), JsValue>
The addEventListener()
method.
This API requires the following crate features to be activated: EventTarget
sourcepub fn add_event_listener_with_callback_and_bool(
&self,
type_: &str,
listener: &Function,
options: bool
) -> Result<(), JsValue>
pub fn add_event_listener_with_callback_and_bool( &self, type_: &str, listener: &Function, options: bool ) -> Result<(), JsValue>
The addEventListener()
method.
This API requires the following crate features to be activated: EventTarget
sourcepub fn add_event_listener_with_callback_and_bool_and_wants_untrusted(
&self,
type_: &str,
listener: &Function,
options: bool,
wants_untrusted: Option<bool>
) -> Result<(), JsValue>
pub fn add_event_listener_with_callback_and_bool_and_wants_untrusted( &self, type_: &str, listener: &Function, options: bool, wants_untrusted: Option<bool> ) -> Result<(), JsValue>
The addEventListener()
method.
This API requires the following crate features to be activated: EventTarget
sourcepub fn remove_event_listener_with_callback(
&self,
type_: &str,
listener: &Function
) -> Result<(), JsValue>
pub fn remove_event_listener_with_callback( &self, type_: &str, listener: &Function ) -> Result<(), JsValue>
The removeEventListener()
method.
This API requires the following crate features to be activated: EventTarget
Methods from Deref<Target = Object>§
sourcepub fn constructor(&self) -> Function
pub fn constructor(&self) -> Function
The constructor property returns a reference to the Object
constructor
function that created the instance object.
sourcepub fn has_own_property(&self, property: &JsValue) -> bool
pub fn has_own_property(&self, property: &JsValue) -> bool
The hasOwnProperty()
method returns a boolean indicating whether the
object has the specified property as its own property (as opposed to
inheriting it).
sourcepub fn is_prototype_of(&self, value: &JsValue) -> bool
pub fn is_prototype_of(&self, value: &JsValue) -> bool
The isPrototypeOf()
method checks if an object exists in another
object’s prototype chain.
sourcepub fn property_is_enumerable(&self, property: &JsValue) -> bool
pub fn property_is_enumerable(&self, property: &JsValue) -> bool
The propertyIsEnumerable()
method returns a Boolean indicating
whether the specified property is enumerable.
sourcepub fn to_locale_string(&self) -> JsString
pub fn to_locale_string(&self) -> JsString
The toLocaleString()
method returns a string representing the object.
This method is meant to be overridden by derived objects for
locale-specific purposes.
sourcepub fn to_string(&self) -> JsString
pub fn to_string(&self) -> JsString
The toString()
method returns a string representing the object.
Methods from Deref<Target = JsValue>§
pub const NULL: JsValue = JsValue{ idx: JSIDX_NULL, _marker: marker::PhantomData,}
pub const UNDEFINED: JsValue = JsValue{ idx: JSIDX_UNDEFINED, _marker: marker::PhantomData,}
pub const TRUE: JsValue = JsValue{ idx: JSIDX_TRUE, _marker: marker::PhantomData,}
pub const FALSE: JsValue = JsValue{ idx: JSIDX_FALSE, _marker: marker::PhantomData,}
sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the f64
value of this JS value if it’s an instance of a
number.
If this JS value is not an instance of a number then this returns
None
.
sourcepub fn as_string(&self) -> Option<String>
pub fn as_string(&self) -> Option<String>
If this JS value is a string value, this function copies the JS string
value into wasm linear memory, encoded as UTF-8, and returns it as a
Rust String
.
To avoid the copying and re-encoding, consider the
JsString::try_from()
function from js-sys
instead.
If this JS value is not an instance of a string or if it’s not valid
utf-8 then this returns None
.
UTF-16 vs UTF-8
JavaScript strings in general are encoded as UTF-16, but Rust strings
are encoded as UTF-8. This can cause the Rust string to look a bit
different than the JS string sometimes. For more details see the
documentation about the str
type which contains a few
caveats about the encodings.
sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the bool
value of this JS value if it’s an instance of a
boolean.
If this JS value is not an instance of a boolean then this returns
None
.
sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Tests whether this JS value is undefined
sourcepub fn is_function(&self) -> bool
pub fn is_function(&self) -> bool
Tests whether the type of this JS value is function
.
sourcepub fn js_in(&self, obj: &JsValue) -> bool
pub fn js_in(&self, obj: &JsValue) -> bool
Applies the binary in
JS operator on the two JsValue
s.
sourcepub fn loose_eq(&self, other: &JsValue) -> bool
pub fn loose_eq(&self, other: &JsValue) -> bool
Compare two JsValue
s for equality, using the ==
operator in JS.
sourcepub fn unsigned_shr(&self, rhs: &JsValue) -> u32
pub fn unsigned_shr(&self, rhs: &JsValue) -> u32
Applies the binary >>>
JS operator on the two JsValue
s.
sourcepub fn checked_div(&self, rhs: &JsValue) -> JsValue
pub fn checked_div(&self, rhs: &JsValue) -> JsValue
Applies the binary /
JS operator on two JsValue
s, catching and returning any RangeError
thrown.
sourcepub fn pow(&self, rhs: &JsValue) -> JsValue
pub fn pow(&self, rhs: &JsValue) -> JsValue
Applies the binary **
JS operator on the two JsValue
s.
sourcepub fn lt(&self, other: &JsValue) -> bool
pub fn lt(&self, other: &JsValue) -> bool
Applies the binary <
JS operator on the two JsValue
s.
sourcepub fn le(&self, other: &JsValue) -> bool
pub fn le(&self, other: &JsValue) -> bool
Applies the binary <=
JS operator on the two JsValue
s.
sourcepub fn ge(&self, other: &JsValue) -> bool
pub fn ge(&self, other: &JsValue) -> bool
Applies the binary >=
JS operator on the two JsValue
s.
sourcepub fn gt(&self, other: &JsValue) -> bool
pub fn gt(&self, other: &JsValue) -> bool
Applies the binary >
JS operator on the two JsValue
s.
sourcepub fn unchecked_into_f64(&self) -> f64
pub fn unchecked_into_f64(&self) -> f64
Applies the unary +
JS operator on a JsValue
. Can throw.