fix(scanner): escaneo QR ultra rapido con BarcodeDetector, jsQR optimizado y loop continuo
This commit is contained in:
+29
-10
@@ -1175,25 +1175,44 @@
|
||||
|
||||
<!-- MODAL: ESCANEAR QR PARA RECARGA EN SODA (ADMIN) -->
|
||||
<div class="modal-qr-backdrop" id="modalAdminScanQr" style="display: none;" onclick="closeAdminScanQrModal(event)">
|
||||
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 400px; text-align: center;">
|
||||
<div class="modal-qr-card" onclick="event.stopPropagation()" style="max-width: 420px; text-align: center;">
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; border-bottom: 1px solid var(--border); padding-bottom: 8px;">
|
||||
<strong style="font-size: 1rem; color: var(--text-main);">📷 Escanear Carné para Recarga</strong>
|
||||
<strong style="font-size: 1rem; color: var(--text-main); display: flex; align-items: center; gap: 6px;">
|
||||
<span>📷</span> Escanear Carné para Recarga
|
||||
</strong>
|
||||
<button type="button" onclick="closeAdminScanQrModal()" style="background: none; border: none; font-size: 1.2rem; cursor: pointer; color: var(--text-muted);">✕</button>
|
||||
</div>
|
||||
|
||||
<div style="position: relative; width: 100%; height: 220px; background: #000; border-radius: 14px; overflow: hidden; margin-bottom: 10px;">
|
||||
<div style="position: relative; width: 100%; height: 230px; background: #000; border-radius: 14px; overflow: hidden; margin-bottom: 10px;">
|
||||
<video id="videoAdminScan" playsinline webkit-playsinline autoplay muted style="width: 100%; height: 100%; object-fit: cover;"></video>
|
||||
<canvas id="canvasAdminScan" style="display: none;"></canvas>
|
||||
<div style="position: absolute; inset: 16px; border: 2px dashed #10b981; border-radius: 12px; pointer-events: none;"></div>
|
||||
<div style="position: absolute; inset: 16px; border: 2px dashed #10b981; border-radius: 12px; pointer-events: none; overflow: hidden;">
|
||||
<div style="position: absolute; left: 0; right: 0; height: 3px; background: #10b981; box-shadow: 0 0 10px #10b981, 0 0 20px #34d399; animation: adminScanLaser 2s ease-in-out infinite;"></div>
|
||||
</div>
|
||||
<button type="button" onclick="toggleAdminCameraFacing()" style="position: absolute; right: 10px; bottom: 10px; background: rgba(15, 23, 42, 0.75); color: white; border: 1px solid rgba(255,255,255,0.25); border-radius: 8px; padding: 5px 9px; font-size: 0.72rem; font-weight: 800; cursor: pointer; backdrop-filter: blur(4px); display: flex; align-items: center; gap: 4px;">
|
||||
🔄 Voltear
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div id="adminScanModalStatus" style="font-size: 0.8rem; font-weight: 800; color: #0284c7; min-height: 22px; margin-bottom: 6px; padding: 4px 8px; border-radius: 6px;">
|
||||
<style>
|
||||
@keyframes adminScanLaser {
|
||||
0% { top: 4%; opacity: 0.6; }
|
||||
50% { top: 92%; opacity: 1; }
|
||||
100% { top: 4%; opacity: 0.6; }
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id="adminScanModalStatus" style="font-size: 0.8rem; font-weight: 800; color: #0284c7; min-height: 22px; margin-bottom: 6px; padding: 6px 10px; border-radius: 8px; background: #e0f2fe; transition: all 0.2s ease;">
|
||||
📷 Enfoca el código QR del carné
|
||||
</div>
|
||||
|
||||
<p style="font-size: 0.76rem; color: var(--text-muted); margin-bottom: 8px;">
|
||||
Escanea el QR del carné para seleccionarlo al instante.
|
||||
</p>
|
||||
<!-- ALTERNATIVA: DIGITAR CÓDIGO O CARNÉ MANUALMENTE -->
|
||||
<div style="margin: 8px 0 10px 0; display: flex; gap: 6px;">
|
||||
<input type="text" id="inputAdminScanManualCode" placeholder="O digita carné (ej: EST-2026-001)" onkeydown="if(event.key==='Enter')submitAdminScanManualCode()" style="flex: 1; padding: 9px 12px; border-radius: 10px; border: 1.5px solid #cbd5e1; font-size: 0.85rem; font-weight: 700; outline: none; background: var(--card-bg, #ffffff); color: var(--text-main);">
|
||||
<button type="button" onclick="submitAdminScanManualCode()" style="padding: 9px 14px; background: #0284c7; color: white; border: none; border-radius: 10px; font-weight: 800; font-size: 0.8rem; cursor: pointer; white-space: nowrap;">
|
||||
🔍 Validar
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button type="button" onclick="closeAdminScanQrModal()" style="width: 100%; padding: 10px; background: #f1f5f9; color: #475569; border: 1px solid var(--border); border-radius: 10px; font-weight: 800; font-size: 0.82rem; cursor: pointer;">
|
||||
Cerrar Cámara
|
||||
@@ -1225,8 +1244,8 @@
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
<script src="/js/sounds.js?v=9.0"></script>
|
||||
<script src="/js/sounds.js?v=9.1"></script>
|
||||
<script src="/js/jsQR.js"></script>
|
||||
<script src="/js/app.js?v=9.0"></script>
|
||||
<script src="/js/app.js?v=9.1"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
+167
-45
@@ -1996,6 +1996,72 @@ function switchLinkChildTab(tab) {
|
||||
if (window.sounds) window.sounds.playTap();
|
||||
}
|
||||
|
||||
// DETECTOR UNIVERSAL DE CÓDIGOS QR (BARCODE DETECTOR CON HARDWARE ACCELERATION + FALLBACK JSQR OPTIMIZADO)
|
||||
let nativeBarcodeDetector = null;
|
||||
if (typeof window !== 'undefined' && 'BarcodeDetector' in window) {
|
||||
try {
|
||||
nativeBarcodeDetector = new window.BarcodeDetector({ formats: ['qr_code'] });
|
||||
} catch (e) {
|
||||
console.warn('BarcodeDetector formats no soportados:', e);
|
||||
}
|
||||
}
|
||||
|
||||
async function detectQrFromMedia(video, canvas, ctx) {
|
||||
if (!video || video.videoWidth === 0 || video.videoHeight === 0 || video.readyState < 2) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. Detección nativa por aceleración de hardware (ultrarrápida ~1ms)
|
||||
if (nativeBarcodeDetector) {
|
||||
try {
|
||||
const barcodes = await nativeBarcodeDetector.detect(video);
|
||||
if (barcodes && barcodes.length > 0 && barcodes[0].rawValue) {
|
||||
return barcodes[0].rawValue.trim();
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignorar error transitorio y continuar a jsQR
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Fallback de jsQR optimizado por downscale (máx 640px para 30-60 FPS fluidos)
|
||||
if (window.jsQR && canvas && ctx) {
|
||||
let w = video.videoWidth;
|
||||
let h = video.videoHeight;
|
||||
const maxDim = 640;
|
||||
if (w > maxDim || h > maxDim) {
|
||||
if (w > h) {
|
||||
h = Math.round((h * maxDim) / w);
|
||||
w = maxDim;
|
||||
} else {
|
||||
w = Math.round((w * maxDim) / h);
|
||||
h = maxDim;
|
||||
}
|
||||
}
|
||||
|
||||
if (canvas.width !== w) canvas.width = w;
|
||||
if (canvas.height !== h) canvas.height = h;
|
||||
|
||||
ctx.drawImage(video, 0, 0, w, h);
|
||||
const imageData = ctx.getImageData(0, 0, w, h);
|
||||
|
||||
// Intento 1: Sin invertir (el 99% de los carnés son oscuros sobre fondo claro)
|
||||
let qr = window.jsQR(imageData.data, w, h, { inversionAttempts: 'dontInvert' });
|
||||
if (!qr || !qr.data) {
|
||||
// Intento 2: Inversión en caso de pantalla oscura o reflejos
|
||||
qr = window.jsQR(imageData.data, w, h, { inversionAttempts: 'attemptBoth' });
|
||||
}
|
||||
|
||||
if (qr && qr.data && String(qr.data).trim().length > 0) {
|
||||
return String(qr.data).trim();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
let isScanChildLoopRunning = false;
|
||||
let isProcessingChildScan = false;
|
||||
|
||||
async function startScanChildCamera() {
|
||||
stopScanChildCamera();
|
||||
const video = document.getElementById('videoScanChild');
|
||||
@@ -2019,25 +2085,26 @@ async function startScanChildCamera() {
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
isScanChildLoopRunning = true;
|
||||
isProcessingChildScan = false;
|
||||
|
||||
function scanFrame() {
|
||||
if (video.readyState === video.HAVE_ENOUGH_DATA) {
|
||||
canvas.width = video.videoWidth;
|
||||
canvas.height = video.videoHeight;
|
||||
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
||||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
if (window.jsQR) {
|
||||
const code = window.jsQR(imageData.data, imageData.width, imageData.height, {
|
||||
inversionAttempts: 'dontInvert'
|
||||
});
|
||||
if (code && code.data) {
|
||||
handleChildQrDetected(code.data);
|
||||
return;
|
||||
}
|
||||
async function scanFrame() {
|
||||
if (!isScanChildLoopRunning || !scanChildStream) return;
|
||||
|
||||
if (!isProcessingChildScan) {
|
||||
const detected = await detectQrFromMedia(video, canvas, ctx);
|
||||
if (detected && !isProcessingChildScan) {
|
||||
isProcessingChildScan = true;
|
||||
handleChildQrDetected(detected);
|
||||
return;
|
||||
}
|
||||
}
|
||||
scanChildAnimId = requestAnimationFrame(scanFrame);
|
||||
|
||||
if (isScanChildLoopRunning) {
|
||||
scanChildAnimId = requestAnimationFrame(scanFrame);
|
||||
}
|
||||
}
|
||||
|
||||
scanChildAnimId = requestAnimationFrame(scanFrame);
|
||||
} catch (err) {
|
||||
console.warn('Cámara no disponible para escaneo de carné:', err);
|
||||
@@ -2049,12 +2116,16 @@ async function startScanChildCamera() {
|
||||
}
|
||||
|
||||
function stopScanChildCamera() {
|
||||
isScanChildLoopRunning = false;
|
||||
isProcessingChildScan = false;
|
||||
if (scanChildAnimId) {
|
||||
cancelAnimationFrame(scanChildAnimId);
|
||||
scanChildAnimId = null;
|
||||
}
|
||||
if (scanChildStream) {
|
||||
scanChildStream.getTracks().forEach(t => t.stop());
|
||||
try {
|
||||
scanChildStream.getTracks().forEach(t => t.stop());
|
||||
} catch (e) {}
|
||||
scanChildStream = null;
|
||||
}
|
||||
const video = document.getElementById('videoScanChild');
|
||||
@@ -2809,9 +2880,15 @@ function parseScannedStudentToken(raw) {
|
||||
}
|
||||
|
||||
// ESCANEO QR CON CÁMARA EN MOSTRADOR DE SODA (ADMIN)
|
||||
let adminScanFacingMode = 'environment';
|
||||
let isScanAdminLoopRunning = false;
|
||||
let isProcessingAdminScan = false;
|
||||
|
||||
function openAdminScanQrModal() {
|
||||
const modal = document.getElementById('modalAdminScanQr');
|
||||
const statusEl = document.getElementById('adminScanModalStatus');
|
||||
const manualInput = document.getElementById('inputAdminScanManualCode');
|
||||
if (manualInput) manualInput.value = '';
|
||||
if (modal) modal.style.display = 'flex';
|
||||
if (statusEl) {
|
||||
statusEl.textContent = '📷 Enfoca el código QR del carné';
|
||||
@@ -2830,6 +2907,22 @@ function closeAdminScanQrModal(event) {
|
||||
if (modal) modal.style.display = 'none';
|
||||
}
|
||||
|
||||
async function toggleAdminCameraFacing() {
|
||||
adminScanFacingMode = (adminScanFacingMode === 'environment') ? 'user' : 'environment';
|
||||
await startScanAdminCamera();
|
||||
}
|
||||
|
||||
async function submitAdminScanManualCode() {
|
||||
const input = document.getElementById('inputAdminScanManualCode');
|
||||
const val = input ? input.value.trim() : '';
|
||||
if (!val) {
|
||||
alert('Por favor digita el carné o código del estudiante');
|
||||
if (input) input.focus();
|
||||
return;
|
||||
}
|
||||
await handleAdminQrDetected(val);
|
||||
}
|
||||
|
||||
async function startScanAdminCamera() {
|
||||
stopScanAdminCamera();
|
||||
const video = document.getElementById('videoAdminScan');
|
||||
@@ -2845,9 +2938,9 @@ async function startScanAdminCamera() {
|
||||
video.setAttribute('autoplay', 'true');
|
||||
|
||||
const constraintConfigs = [
|
||||
{ video: { facingMode: { ideal: 'environment' }, width: { ideal: 1280 } }, audio: false },
|
||||
{ video: { facingMode: { ideal: 'environment' }, width: { ideal: 640 }, height: { ideal: 480 } }, audio: false },
|
||||
{ video: { facingMode: 'environment' }, audio: false },
|
||||
{ video: { facingMode: { ideal: adminScanFacingMode }, width: { ideal: 1280 } }, audio: false },
|
||||
{ video: { facingMode: { ideal: adminScanFacingMode }, width: { ideal: 640 }, height: { ideal: 480 } }, audio: false },
|
||||
{ video: { facingMode: adminScanFacingMode }, audio: false },
|
||||
{ video: true, audio: false }
|
||||
];
|
||||
|
||||
@@ -2884,39 +2977,47 @@ async function startScanAdminCamera() {
|
||||
}
|
||||
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
let isProcessingScan = false;
|
||||
isScanAdminLoopRunning = true;
|
||||
isProcessingAdminScan = false;
|
||||
|
||||
function scanFrame() {
|
||||
if (!scanAdminStream) return;
|
||||
if (!isProcessingScan && video.readyState === video.HAVE_ENOUGH_DATA) {
|
||||
canvas.width = video.videoWidth;
|
||||
canvas.height = video.videoHeight;
|
||||
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
||||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
if (window.jsQR) {
|
||||
const code = window.jsQR(imageData.data, imageData.width, imageData.height, {
|
||||
inversionAttempts: 'attemptBoth'
|
||||
});
|
||||
if (code && code.data && String(code.data).trim().length > 0) {
|
||||
isProcessingScan = true;
|
||||
handleAdminQrDetected(code.data).then(success => {
|
||||
if (!success) {
|
||||
setTimeout(() => {
|
||||
isProcessingScan = false;
|
||||
}, 2000);
|
||||
}
|
||||
});
|
||||
return;
|
||||
async function scanFrame() {
|
||||
if (!isScanAdminLoopRunning || !scanAdminStream) return;
|
||||
|
||||
if (!isProcessingAdminScan) {
|
||||
const detected = await detectQrFromMedia(video, canvas, ctx);
|
||||
if (detected && !isProcessingAdminScan) {
|
||||
isProcessingAdminScan = true;
|
||||
if (statusEl) {
|
||||
statusEl.textContent = '⚡ ¡Código detectado! Verificando...';
|
||||
statusEl.style.color = '#166534';
|
||||
statusEl.style.background = '#dcfce7';
|
||||
}
|
||||
|
||||
handleAdminQrDetected(detected).then(success => {
|
||||
if (!success) {
|
||||
setTimeout(() => {
|
||||
isProcessingAdminScan = false;
|
||||
if (statusEl && isScanAdminLoopRunning) {
|
||||
statusEl.textContent = '📷 Enfoca el código QR del carné';
|
||||
statusEl.style.color = '#0284c7';
|
||||
statusEl.style.background = '#e0f2fe';
|
||||
}
|
||||
}, 1500);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
scanAdminAnimId = requestAnimationFrame(scanFrame);
|
||||
|
||||
if (isScanAdminLoopRunning) {
|
||||
scanAdminAnimId = requestAnimationFrame(scanFrame);
|
||||
}
|
||||
}
|
||||
|
||||
scanAdminAnimId = requestAnimationFrame(scanFrame);
|
||||
} catch (err) {
|
||||
console.warn('Cámara de mostrador no disponible:', err);
|
||||
if (statusEl) {
|
||||
statusEl.textContent = '⚠️ Cámara no disponible. Selecciona el alumno en el buscador.';
|
||||
statusEl.textContent = '⚠️ Cámara no disponible. Digita el carné abajo o búscalo arriba.';
|
||||
statusEl.style.color = '#dc2626';
|
||||
statusEl.style.background = '#fee2e2';
|
||||
}
|
||||
@@ -2924,12 +3025,16 @@ async function startScanAdminCamera() {
|
||||
}
|
||||
|
||||
function stopScanAdminCamera() {
|
||||
isScanAdminLoopRunning = false;
|
||||
isProcessingAdminScan = false;
|
||||
if (scanAdminAnimId) {
|
||||
cancelAnimationFrame(scanAdminAnimId);
|
||||
scanAdminAnimId = null;
|
||||
}
|
||||
if (scanAdminStream) {
|
||||
scanAdminStream.getTracks().forEach(t => t.stop());
|
||||
try {
|
||||
scanAdminStream.getTracks().forEach(t => t.stop());
|
||||
} catch (e) {}
|
||||
scanAdminStream = null;
|
||||
}
|
||||
const video = document.getElementById('videoAdminScan');
|
||||
@@ -2982,7 +3087,24 @@ async function handleAdminQrDetected(token) {
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// 4. Procesar resultado
|
||||
// 4. Fallback si el usuario digitó carné o nombre parcial
|
||||
if (!student) {
|
||||
try {
|
||||
const resAll = await fetch('/api/estudiantes');
|
||||
if (resAll.ok) {
|
||||
const allEst = await resAll.json();
|
||||
students = allEst;
|
||||
student = (students || []).find(s =>
|
||||
(s.qr_token && s.qr_token.toLowerCase() === clean) ||
|
||||
(s.codigo_estudiante && s.codigo_estudiante.toLowerCase() === clean) ||
|
||||
(s.nombre_completo && s.nombre_completo.toLowerCase().includes(clean)) ||
|
||||
(String(s.id) === parsed)
|
||||
);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// 5. Procesar resultado
|
||||
if (student) {
|
||||
const idx = (students || []).findIndex(s => s.id === student.id);
|
||||
if (idx >= 0) {
|
||||
|
||||
+4
-3
@@ -779,10 +779,11 @@ app.get('/api/estudiantes/qr/:token', (req, res) => {
|
||||
app.get('/api/qr-image/:token', async (req, res) => {
|
||||
try {
|
||||
const qrDataUrl = await QRCode.toDataURL(req.params.token, {
|
||||
width: 300,
|
||||
margin: 2,
|
||||
width: 350,
|
||||
margin: 3,
|
||||
errorCorrectionLevel: 'M',
|
||||
color: {
|
||||
dark: '#0f172a',
|
||||
dark: '#000000',
|
||||
light: '#ffffff'
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user