id-scanner-lib
Version:
Browser-based ID card, QR code, and face recognition scanner with liveness detection
84 lines (75 loc) • 2.1 kB
text/typescript
/**
* @file V1 Adapter (Compatibility Layer)
* @description Provides backward compatibility with v1 IDScanner API
* @module compat/v1-adapter
*/
import { Scanner, ScannerConfig } from '../core/scanner';
import { EventEmitter } from '../core/event-emitter';
/**
* V1 IDScanner configuration
*/
export interface IDScannerConfig {
/** Debug mode */
debug?: boolean;
/** Callback when faces are detected */
onFaceDetected?: (faces: any[]) => void;
/** Callback on error */
onError?: (error: Error) => void;
}
/**
* IDScanner class - provides v1 API compatibility
* Wraps the new Scanner class with the old IDScanner interface
*/
export class IDScanner {
private _scanner: Scanner;
private _config: IDScannerConfig;
private _eventEmitter: EventEmitter;
/**
* Create a new IDScanner instance
* @param config Configuration
*/
constructor(config: IDScannerConfig = {}) {
this._config = config;
this._scanner = new Scanner({ debug: config.debug });
this._eventEmitter = new EventEmitter();
}
/**
* Initialize the scanner
*/
async initialize(): Promise<void> {
await this._scanner.initialize();
}
/**
* Start face recognition on a video element
* @param video Video element to process
*/
async startFaceRecognition(video: HTMLVideoElement): Promise<void> {
const faces = await this._scanner.detectFace(video);
if (this._config.onFaceDetected) {
this._config.onFaceDetected(faces);
}
this._eventEmitter.emit('faceDetected', faces);
}
/**
* Stop the scanner and release resources
*/
async stop(): Promise<void> {
await this._scanner.destroy();
}
/**
* Register an event handler
* @param event Event name
* @param handler Event handler
*/
on(event: string, handler: (data?: any) => void): void {
this._eventEmitter.on(event, handler);
}
/**
* Unregister an event handler
* @param event Event name
* @param handler Event handler
*/
off(event: string, handler: (data?: any) => void): void {
this._eventEmitter.off(event, handler);
}
}