UNPKG

barcode-tool

Version:

lightweight npm package that facilitates barcode generation and detection. It leverages the Barcode Detection API for barcode scanning directly in web browsers and provides an intuitive interface for generating various barcode formats. Seamlessly integrat

30 lines (24 loc) 894 B
export type DetectBarcodeParams = { image: HTMLElement | Blob | HTMLCanvasElement | HTMLImageElement | HTMLVideoElement | ImageBitmap | ImageData | SVGImageElement | undefined; formats?: string[]; }; export type Barcode = { format: string; rawValue: string; }; export async function detectBarcode({ image, formats }: DetectBarcodeParams): Promise<Barcode[]> { if (!image) { throw new Error('image property is not provided'); } if ('BarcodeDetector' in window) { const barcodeDetector = new (window as any).BarcodeDetector({ formats, }); const barcodes = await barcodeDetector.detect(image); return barcodes.map((barcode: any) => ({ format: barcode.format, rawValue: barcode.rawValue, })); } throw new Error('Barcode Detection API is not supported in this browser'); }