pairing-qr-scanner.ts

620 B · raw

1import { BrowserQRCodeReader, NotFoundException } from "@zxing/library";23export interface PairingQrScannerHandle {4  stop(): void;5}67export async function startPairingQrScanner(8  video: HTMLVideoElement,9  onScan: (value: string) => void,10  onError: (error: Error) => void,11): Promise<PairingQrScannerHandle> {12  const reader = new BrowserQRCodeReader();13  await reader.decodeFromVideoDevice(null, video, (result, error) => {14    if (result) {15      onScan(result.getText());16      return;17    }18    if (error && !(error instanceof NotFoundException)) onError(error);19  });20  return { stop: () => reader.reset() };21}