@face-auth/face-id-video
Version:
Utility library for capturing photos from webcam video streams in the browser. Handles camera selection, image formatting, and output for face authentication APIs.
87 lines (86 loc) • 3.39 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export class FaceCameraManager {
constructor(videoElement) {
this.stream = null;
this.video = videoElement;
}
/**
* List available cameras
*/
getDevices() {
return __awaiter(this, void 0, void 0, function* () {
const devices = yield navigator.mediaDevices.enumerateDevices();
return devices.filter(d => d.kind === "videoinput");
});
}
/**
* Start video stream
*/
start() {
return __awaiter(this, arguments, void 0, function* (options = {}, deviceId) {
let constraints = {
video: {}
};
if (deviceId) {
constraints.video = {
deviceId: { exact: deviceId }
};
}
else if (options.preferredFacingMode) {
constraints.video = {
facingMode: options.preferredFacingMode
};
}
if (options.idealResolution) {
constraints.video = Object.assign(Object.assign({}, constraints.video), { width: { ideal: options.idealResolution.width }, height: { ideal: options.idealResolution.height } });
}
try {
this.stream = yield navigator.mediaDevices.getUserMedia(constraints);
this.video.srcObject = this.stream;
// Wait for metadata to ensure video dimensions are ready
let promise = yield new Promise((resolve) => {
this.video.onloadedmetadata = () => {
resolve(this.video);
};
});
// Play requires user interaction in most browsers
yield this.video.play();
return promise;
}
catch (err) {
const error = new Error((err === null || err === void 0 ? void 0 : err.message) || "Unknown error");
if (err.name === "NotAllowedError") {
error.code = "PERMISSION_DENIED";
}
else if (err.name === "NotFoundError") {
error.code = "NO_DEVICES";
}
else if (err.name === "OverconstrainedError") {
error.code = "OVERCONSTRAINED";
}
else {
error.code = "UNKNOWN";
}
throw error;
}
});
}
/**
* Stop the camera
*/
stop() {
if (this.stream) {
this.stream.getTracks().forEach(t => t.stop());
this.stream = null;
}
this.video.srcObject = null;
}
}