Add barcode scanning to manage products

This commit is contained in:
beatz174-bit
2025-11-21 18:17:01 +10:00
parent e361af2a1c
commit b81ac51242
6 changed files with 100 additions and 34 deletions
+13 -4
View File
@@ -1,6 +1,9 @@
import { useEffect, useRef, useState } from 'react';
import { BrowserMultiFormatReader } from '@zxing/browser';
type BarcodeDetection = { rawValue: string };
type BarcodeDetectorClass = new () => { detect: (source: ImageBitmapSource) => Promise<BarcodeDetection[]> };
export interface BarcodeResult {
code?: string;
error?: string;
@@ -14,15 +17,20 @@ export const useBarcodeScanner = () => {
const reader = new BrowserMultiFormatReader();
let active = true;
let stop: (() => void) | undefined;
let currentVideoElement: HTMLVideoElement | null = null;
const start = async () => {
try {
if ('BarcodeDetector' in window) {
const detector = new (window as typeof window & { BarcodeDetector: any }).BarcodeDetector();
const detectorClass = (
window as typeof window & { BarcodeDetector?: BarcodeDetectorClass }
).BarcodeDetector;
if (detectorClass) {
const detector = new detectorClass();
const stream = await navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } });
if (videoRef.current) {
videoRef.current.srcObject = stream;
await videoRef.current.play();
currentVideoElement = videoRef.current;
}
const scan = async () => {
if (!active || !videoRef.current) return;
@@ -58,8 +66,9 @@ export const useBarcodeScanner = () => {
return () => {
active = false;
stop?.();
if (videoRef.current?.srcObject) {
(videoRef.current.srcObject as MediaStream).getTracks().forEach((track) => track.stop());
const stream = currentVideoElement?.srcObject as MediaStream | null;
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
};
}, []);