fix(camera-parity-pwa): resolver parpadeo de camara, paridad funcional de acompanantes, lotes, egreso y PWA responsive
This commit is contained in:
@@ -44,6 +44,9 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
const [selectedDeviceId, setSelectedDeviceId] = useState<string>('');
|
||||
const [activeCameraLabel, setActiveCameraLabel] = useState<string>('');
|
||||
|
||||
const isProcessingRef = useRef<boolean>(false);
|
||||
const decodeFrameRef = useRef<(ctx: CanvasRenderingContext2D, width: number, height: number) => Promise<boolean>>(() => Promise.resolve(false));
|
||||
|
||||
// Inicializar motor ZXing configurado para DNI Argentino (PDF417 + QR + Code128)
|
||||
useEffect(() => {
|
||||
const hints = new Map();
|
||||
@@ -61,6 +64,12 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
// Crear canvas offscreen reutilizable
|
||||
const offscreenCanvas = document.createElement('canvas');
|
||||
canvasRef.current = offscreenCanvas;
|
||||
|
||||
return () => {
|
||||
try {
|
||||
reader.reset();
|
||||
} catch {}
|
||||
};
|
||||
}, []);
|
||||
|
||||
const stopCamera = useCallback(() => {
|
||||
@@ -81,11 +90,14 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
if (videoRef.current) {
|
||||
videoRef.current.srcObject = null;
|
||||
}
|
||||
isProcessingRef.current = false;
|
||||
setIsProcessing(false);
|
||||
}, []);
|
||||
|
||||
const handleDecodedString = useCallback(
|
||||
async (rawText: string) => {
|
||||
if (isProcessing || !rawText) return;
|
||||
if (isProcessingRef.current || !rawText) return;
|
||||
isProcessingRef.current = true;
|
||||
setIsProcessing(true);
|
||||
setErrorMsg(null);
|
||||
try {
|
||||
@@ -100,15 +112,18 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
} catch (err: any) {
|
||||
setErrorMsg(err.message || 'Error al interpretar el código escaneado.');
|
||||
} finally {
|
||||
isProcessingRef.current = false;
|
||||
setIsProcessing(false);
|
||||
}
|
||||
},
|
||||
[isProcessing, stopCamera, onScanSuccess, onClose]
|
||||
[stopCamera, onScanSuccess, onClose]
|
||||
);
|
||||
|
||||
// Decodificar un buffer ImageData mediante ZXing y BarcodeDetector
|
||||
const decodeFrame = useCallback(
|
||||
async (ctx: CanvasRenderingContext2D, width: number, height: number): Promise<boolean> => {
|
||||
if (isProcessingRef.current) return false;
|
||||
|
||||
// 1. Probar BarcodeDetector nativo si está presente en el navegador (acelerado por GPU)
|
||||
if ('BarcodeDetector' in window) {
|
||||
try {
|
||||
@@ -137,7 +152,11 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
// NotFoundException en cada frame sin código
|
||||
// NotFoundException en cada frame sin código (normal)
|
||||
} finally {
|
||||
try {
|
||||
zxingReaderRef.current.reset();
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,6 +165,9 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
[handleDecodedString]
|
||||
);
|
||||
|
||||
// Mantener decodeFrameRef sincronizado
|
||||
decodeFrameRef.current = decodeFrame;
|
||||
|
||||
const startCamera = useCallback(
|
||||
async (deviceIdToUse?: string) => {
|
||||
setErrorMsg(null);
|
||||
@@ -172,10 +194,23 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
try {
|
||||
let stream: MediaStream | null = null;
|
||||
|
||||
// Estrategia de Constraints flexible:
|
||||
// Intento 1: Dispositivo seleccionado o cámara trasera
|
||||
const constraints1: MediaStreamConstraints = deviceIdToUse
|
||||
? { video: { deviceId: { exact: deviceIdToUse } }, audio: false }
|
||||
// Limpiar stream previo si hubiera
|
||||
if (streamRef.current) {
|
||||
streamRef.current.getTracks().forEach((t) => {
|
||||
try { t.stop(); } catch {}
|
||||
});
|
||||
streamRef.current = null;
|
||||
}
|
||||
|
||||
const constraints: MediaStreamConstraints = deviceIdToUse
|
||||
? {
|
||||
video: {
|
||||
deviceId: { exact: deviceIdToUse },
|
||||
width: { ideal: 1280 },
|
||||
height: { ideal: 720 },
|
||||
},
|
||||
audio: false,
|
||||
}
|
||||
: {
|
||||
video: {
|
||||
facingMode: { ideal: 'environment' },
|
||||
@@ -186,9 +221,9 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
};
|
||||
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia(constraints1);
|
||||
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
||||
} catch {
|
||||
// Intento 2: Fallback incondicional para webcams Windows / laptop estándar
|
||||
// Fallback simple si fallaron los constraints ideales
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
video: true,
|
||||
audio: false,
|
||||
@@ -211,11 +246,16 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
}
|
||||
}
|
||||
|
||||
// Enumerar dispositivos disponibles (ahora con permisos ya otorgados)
|
||||
// Enumerar dispositivos disponibles sin provocar re-renders cíclicos
|
||||
try {
|
||||
const allDevices = await navigator.mediaDevices.enumerateDevices();
|
||||
const vInputs = allDevices.filter((d) => d.kind === 'videoinput');
|
||||
setAvailableDevices(vInputs);
|
||||
setAvailableDevices((prev) => {
|
||||
if (prev.length !== vInputs.length) return vInputs;
|
||||
const prevIds = prev.map((p) => p.deviceId).join(',');
|
||||
const newIds = vInputs.map((p) => p.deviceId).join(',');
|
||||
return prevIds === newIds ? prev : vInputs;
|
||||
});
|
||||
} catch {
|
||||
// Ignorar si falla enumeración
|
||||
}
|
||||
@@ -230,7 +270,7 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
// Iniciar loop de escaneo periódico cada 200ms
|
||||
if (timerRef.current) window.clearInterval(timerRef.current);
|
||||
timerRef.current = window.setInterval(async () => {
|
||||
if (!videoRef.current || videoRef.current.readyState < 2 || isProcessing) return;
|
||||
if (!videoRef.current || videoRef.current.readyState < 2 || isProcessingRef.current) return;
|
||||
|
||||
const video = videoRef.current;
|
||||
const canvas = canvasRef.current;
|
||||
@@ -239,13 +279,13 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
const width = video.videoWidth || 640;
|
||||
const height = video.videoHeight || 480;
|
||||
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
if (canvas.width !== width) canvas.width = width;
|
||||
if (canvas.height !== height) canvas.height = height;
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) return;
|
||||
|
||||
ctx.drawImage(video, 0, 0, width, height);
|
||||
await decodeFrame(ctx, width, height);
|
||||
await decodeFrameRef.current(ctx, width, height);
|
||||
}, 200);
|
||||
} catch (err: any) {
|
||||
setHasCamera(false);
|
||||
@@ -261,7 +301,7 @@ export const CameraScannerModal: React.FC<CameraScannerModalProps> = ({
|
||||
}
|
||||
}
|
||||
},
|
||||
[decodeFrame, isProcessing]
|
||||
[]
|
||||
);
|
||||
|
||||
// Manejador de captura de foto nativa (funciona sin HTTPS en tablets Android)
|
||||
|
||||
Reference in New Issue
Block a user