Struct nokhwa::js_camera::JSCamera

source ·
pub struct JSCamera { /* private fields */ }
Available on crate feature input-jscam only.
Expand description

A wrapper around a MediaStream

JS-WASM

This is exported as NokhwaCamera.

Implementations§

source§

impl JSCamera

source

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.

source

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.

source

pub fn media_stream(&self) -> MediaStream

Gets the internal MediaStream MDN

JS-WASM

This is exported as MediaStream.

source

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

source

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.

source

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.

source

pub fn set_constraints( &mut self, constraints: JSCameraConstraints ) -> Result<(), NokhwaError>

Sets the JSCameraConstraints. This calls apply_constraints internally.

Errors

See apply_constraints.

source

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.

source

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.

source

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.

source

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.

source

pub fn frame_image_data(&mut self) -> Result<ImageData, NokhwaError>

Captures an ImageData MDN by drawing the image to a non-existent canvas. It is greatly advised to call this after calling attach to reduce DOM overhead.

Errors

If drawing to the canvas fails this will error.

source

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 is image/png by default (lossless) but can be set to image/jpeg or image/webp (lossy). Anything else is ignored.
  • image_quality: A number between 0 and 1 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.

source

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.

source

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().

source

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().

source

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.

source

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().

source

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.

source

pub fn is_open(&self) -> bool

Checks if the stream is open.

source

pub async fn restart(&mut self) -> Result<(), NokhwaError>

Restarts the stream.

Errors

There may be errors when re-creating the camera, such as permission errors.

source

pub fn stop_all(&mut self) -> Result<(), NokhwaError>

Stops all streams and detaches the camera.

Errors

There may be an error while detaching the camera. Please see detach() for more details.

Methods from Deref<Target = MediaStream>§

source

pub fn id(&self) -> String

Getter for the id field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn active(&self) -> bool

Getter for the active field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn onaddtrack(&self) -> Option<Function>

Getter for the onaddtrack field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn set_onaddtrack(&self, value: Option<&Function>)

Setter for the onaddtrack field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn onremovetrack(&self) -> Option<Function>

Getter for the onremovetrack field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn set_onremovetrack(&self, value: Option<&Function>)

Setter for the onremovetrack field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn current_time(&self) -> f64

Getter for the currentTime field of this object.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn add_track(&self, track: &MediaStreamTrack)

The addTrack() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream, MediaStreamTrack

source

pub fn clone(&self) -> MediaStream

The clone() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn get_audio_tracks(&self) -> Array

The getAudioTracks() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn get_track_by_id(&self, track_id: &str) -> Option<MediaStreamTrack>

The getTrackById() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream, MediaStreamTrack

source

pub fn get_tracks(&self) -> Array

The getTracks() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn get_video_tracks(&self) -> Array

The getVideoTracks() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream

source

pub fn remove_track(&self, track: &MediaStreamTrack)

The removeTrack() method.

MDN Documentation

This API requires the following crate features to be activated: MediaStream, MediaStreamTrack

Methods from Deref<Target = EventTarget>§

source

pub fn add_event_listener_with_callback( &self, type_: &str, listener: &Function ) -> Result<(), JsValue>

The addEventListener() method.

MDN Documentation

This API requires the following crate features to be activated: EventTarget

source

pub fn add_event_listener_with_callback_and_bool( &self, type_: &str, listener: &Function, options: bool ) -> Result<(), JsValue>

The addEventListener() method.

MDN Documentation

This API requires the following crate features to be activated: EventTarget

source

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.

MDN Documentation

This API requires the following crate features to be activated: EventTarget

source

pub fn remove_event_listener_with_callback( &self, type_: &str, listener: &Function ) -> Result<(), JsValue>

The removeEventListener() method.

MDN Documentation

This API requires the following crate features to be activated: EventTarget

source

pub fn remove_event_listener_with_callback_and_bool( &self, type_: &str, listener: &Function, options: bool ) -> Result<(), JsValue>

The removeEventListener() method.

MDN Documentation

This API requires the following crate features to be activated: EventTarget

Methods from Deref<Target = Object>§

source

pub fn constructor(&self) -> Function

The constructor property returns a reference to the Object constructor function that created the instance object.

MDN documentation

source

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).

MDN documentation

source

pub fn is_prototype_of(&self, value: &JsValue) -> bool

The isPrototypeOf() method checks if an object exists in another object’s prototype chain.

MDN documentation

source

pub fn property_is_enumerable(&self, property: &JsValue) -> bool

The propertyIsEnumerable() method returns a Boolean indicating whether the specified property is enumerable.

MDN documentation

source

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.

MDN documentation

source

pub fn to_string(&self) -> JsString

The toString() method returns a string representing the object.

MDN documentation

source

pub fn value_of(&self) -> Object

The valueOf() method returns the primitive value of the specified object.

MDN documentation

Methods from Deref<Target = JsValue>§

source

pub const NULL: JsValue = JsValue{ idx: JSIDX_NULL, _marker: marker::PhantomData,}

source

pub const UNDEFINED: JsValue = JsValue{ idx: JSIDX_UNDEFINED, _marker: marker::PhantomData,}

source

pub const TRUE: JsValue = JsValue{ idx: JSIDX_TRUE, _marker: marker::PhantomData,}

source

pub const FALSE: JsValue = JsValue{ idx: JSIDX_FALSE, _marker: marker::PhantomData,}

source

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.

source

pub fn is_string(&self) -> bool

Tests whether this JS value is a JS string.

source

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.

source

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.

source

pub fn is_null(&self) -> bool

Tests whether this JS value is null

source

pub fn is_undefined(&self) -> bool

Tests whether this JS value is undefined

source

pub fn is_symbol(&self) -> bool

Tests whether the type of this JS value is symbol

source

pub fn is_object(&self) -> bool

Tests whether typeof self == "object" && self !== null.

source

pub fn is_array(&self) -> bool

Tests whether this JS value is an instance of Array.

source

pub fn is_function(&self) -> bool

Tests whether the type of this JS value is function.

source

pub fn is_bigint(&self) -> bool

Tests whether the type of this JS value is bigint.

source

pub fn js_typeof(&self) -> JsValue

Applies the unary typeof JS operator on a JsValue.

MDN documentation

source

pub fn js_in(&self, obj: &JsValue) -> bool

Applies the binary in JS operator on the two JsValues.

MDN documentation

source

pub fn is_truthy(&self) -> bool

Tests whether the value is “truthy”.

source

pub fn is_falsy(&self) -> bool

Tests whether the value is “falsy”.

source

pub fn loose_eq(&self, other: &JsValue) -> bool

Compare two JsValues for equality, using the == operator in JS.

MDN documentation

source

pub fn bit_not(&self) -> JsValue

Applies the unary ~ JS operator on a JsValue.

MDN documentation

source

pub fn unsigned_shr(&self, rhs: &JsValue) -> u32

Applies the binary >>> JS operator on the two JsValues.

MDN documentation

source

pub fn checked_div(&self, rhs: &JsValue) -> JsValue

Applies the binary / JS operator on two JsValues, catching and returning any RangeError thrown.

MDN documentation

source

pub fn pow(&self, rhs: &JsValue) -> JsValue

Applies the binary ** JS operator on the two JsValues.

MDN documentation

source

pub fn lt(&self, other: &JsValue) -> bool

Applies the binary < JS operator on the two JsValues.

MDN documentation

source

pub fn le(&self, other: &JsValue) -> bool

Applies the binary <= JS operator on the two JsValues.

MDN documentation

source

pub fn ge(&self, other: &JsValue) -> bool

Applies the binary >= JS operator on the two JsValues.

MDN documentation

source

pub fn gt(&self, other: &JsValue) -> bool

Applies the binary > JS operator on the two JsValues.

MDN documentation

source

pub fn unchecked_into_f64(&self) -> f64

Applies the unary + JS operator on a JsValue. Can throw.

MDN documentation

Trait Implementations§

source§

impl Deref for JSCamera

§

type Target = MediaStream

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Drop for JSCamera

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for JSCamera

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>