ts-webcam
Version:
A production-grade TypeScript webcam library with callback-based APIs, flexible permission handling, and comprehensive device support
209 lines • 6.05 kB
TypeScript
import type { CaptureOptions, CaptureResult } from "./types";
import { DeviceCapability, PermissionRequestOptions, WebcamState, WebcamConfiguration, Resolution } from "./types";
export declare class Webcam {
/**
* Initializes the webcam state
*/
private state;
/**
* Listener for device changes
*/
private _deviceChangeListener?;
private _disposed;
private _config?;
private _debugEnabled;
constructor();
/**
* Enable debug logging
*/
enableDebug(): void;
/**
* Disable debug logging
*/
disableDebug(): void;
/**
* Check if debug logging is enabled
*/
isDebugEnabled(): boolean;
/**
* Log debug message if debug is enabled
* @param message The message to log
* @param args Additional arguments to log
*/
debugLog(message: string, ...args: any[]): void;
/**
* Get the current state of the webcam.
* @returns WebcamState
*/
getState(): WebcamState;
/**
* Check the current permissions of the user.
* @returns Record<string, PermissionState>
*/
getCurrentDevice(): Promise<MediaDeviceInfo | null>;
getCurrentResolution(): Resolution | null;
checkPermissions(): Promise<Record<string, PermissionState>>;
/**
* Request permissions from the user.
* @param options PermissionRequestOptions
* @returns Record<string, PermissionState>
*/
requestPermissions(options?: PermissionRequestOptions): Promise<Record<string, PermissionState>>;
/**
* Get a list of available video devices.
* @returns Promise<MediaDeviceInfo[]>
*/
getVideoDevices(): Promise<MediaDeviceInfo[]>;
/**
* Start the camera with the provided configuration.
* @param config WebcamConfiguration
* @returns Promise<void>
*/
startCamera(config: WebcamConfiguration): Promise<void>;
/**
* Stop the camera and release resources.
*/
stopCamera(): void;
/**
* Capture an image from the webcam.
* @param options Capture options including image type, quality, and scale
* @returns A Promise that resolves with a CaptureResult object containing both blob and base64
* @example
* // Basic usage - returns { blob, base64, width, height, mimeType, timestamp }
* const result = await webcam.captureImage();
* console.log("Base64 image:", result.base64);
*
* // With options
* const result = await webcam.captureImage({
* imageType: 'image/jpeg',
* quality: 0.8,
* scale: 0.5
* });
*
* // Or destructure what you need
* const { base64, blob } = await webcam.captureImage();
*/
captureImage(options?: CaptureOptions): Promise<CaptureResult>;
/**
* Get the capabilities of a specific device.
* @param deviceId The ID of the device to get capabilities for.
* @returns A Promise that resolves with the device capabilities.
*/
getDeviceCapabilities(deviceId: string): Promise<DeviceCapability>;
dispose(): void;
private _ensureNotDisposed;
/**
* Set the status of the webcam.
* @param status The new status of the webcam.
*/
private _setStatus;
/**
* Set the error state of the webcam.
* @param error The new error state of the webcam.
*/
private _setError;
/**
* Clear the error state of the webcam.
*/
private _clearError;
/**
* Call the onStateChange callback with the current state.
*/
private _callStateChange;
/**
* Call the onDeviceChange callback with the current devices.
* @param devices The current devices.
*/
private _callDeviceChange;
/**
* Call the onStreamStart callback with the current stream.
* @param stream The MediaStream that has started.
*/
private _callStreamStart;
/**
* Call the onStreamStop callback.
*/
private _callStreamStop;
/**
* Call the onError callback with the current error.
* @param error The WebcamError that has occurred.
*/
private _callError;
/**
* Call the onPermissionChange callback with the current permissions.
* @param permissions The current permissions.
*/
private _callPermissionChange;
/**
* Call the onDeviceChange callback with the current devices.
* @param devices The current devices.
*/
private _buildConstraints;
private _handleStartCameraError;
private _captureFromVideoElement;
/**
* Mirror control (CSS only, always supported if video element present).
* @param mirror boolean
*/
setMirror(mirror: boolean): void;
/**
* Get the current mirror state.
* @returns boolean
*/
getMirror(): boolean;
/**
* Check if mirror is supported.
* @returns boolean
*/
isMirrorSupported(): boolean;
/**
* Torch control (if supported).
* @param enabled boolean
*/
setTorch(enabled: boolean): Promise<void>;
/**
* Get the current torch state.
* @returns boolean
*/
getTorch(): boolean | undefined;
/**
* Check if torch is supported.
* @returns boolean
*/
isTorchSupported(): boolean;
/**
* Zoom control (if supported).
* @param zoom number
*/
setZoom(zoom: number): Promise<void>;
/**
* Get the current zoom level.
* @returns number
*/
getZoom(): number | undefined;
/**
* Check if zoom is supported.
* @returns boolean
*/
isZoomSupported(): boolean;
/**
* Focus mode control (if supported).
* @param mode string
*/
setFocusMode(mode: string): Promise<void>;
/**
* Get the current focus mode.
* @returns string
*/
getFocusMode(): string | undefined;
/**
* Check if focus mode is supported.
* @returns boolean
*/
isFocusSupported(): boolean;
/**
* Helper: get the active video track
**/
private _getActiveVideoTrack;
}
//# sourceMappingURL=ts-webcam.d.ts.map