This commit is contained in:
2025-10-27 14:24:30 -06:00
14 changed files with 670 additions and 31 deletions
+42
View File
@@ -0,0 +1,42 @@
"use client";
import { useEffect, useRef } from "react";
import { BrowserMultiFormatReader } from "@zxing/library";
type Props = { onDetected: (code: string) => void; };
export default function BarcodeScanner({ onDetected }: Props) {
const videoRef = useRef<HTMLVideoElement>(null);
useEffect(() => {
const codeReader = new BrowserMultiFormatReader();
if (!videoRef.current) return;
// Obtener la cámara trasera si existe
codeReader.listVideoInputDevices().then((devices) => {
const backDevice = devices.find(d => d.label.toLowerCase().includes("back"));
const deviceId = backDevice ? backDevice.deviceId : devices[0].deviceId;
// Inicia la cámara inmediatamente
codeReader.decodeFromVideoDevice(deviceId, videoRef.current!, (result) => {
if (result) onDetected(result.getText());
});
}).catch(console.error);
return () => { codeReader.reset(); };
}, [onDetected]);
return (
<video
ref={videoRef}
style={{
width: "100%",
maxWidth: "600px", // Más grande
border: "2px solid #004aad",
borderRadius: "10px"
}}
autoPlay
muted
/>
);
}