implementation of a new Framework for the Barcode scanning via Quagga2 -> only test implementation

This commit is contained in:
2026-06-06 16:53:11 +02:00
parent 577c6c4bed
commit 76e9670cee
+84 -44
View File
@@ -2732,7 +2732,7 @@ document.addEventListener('DOMContentLoaded', ()=>{
window.isDebug = false; // Set to true only for development environment window.isDebug = false; // Set to true only for development environment
</script> </script>
<script src="https://unpkg.com/@zxing/library@latest/umd/index.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/@ericblade/quagga2/dist/quagga.js"></script>
<script src="/static/js/scanner.js"></script> <script src="/static/js/scanner.js"></script>
<script> <script>
// Function to check if a file is a video // Function to check if a file is a video
@@ -2773,57 +2773,97 @@ document.addEventListener('DOMContentLoaded', ()=>{
return; return;
} }
document.getElementById('scanButton').addEventListener('click', function() { // Keep track of the scanner state globally/outer scope
let isScanning = false;
function startScanner() {
const qrReader = document.getElementById('qr-reader'); const qrReader = document.getElementById('qr-reader');
const scanBtn = document.getElementById('scanButton'); const statusText = document.getElementById('status');
const setScannerUi = (isOpen) => { // Show the container
if (!scanBtn) return; if (qrReader) qrReader.style.display = 'block';
scanBtn.classList.toggle('is-active', isOpen); if (statusText) statusText.innerText = "Initializing camera...";
scanBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
scanBtn.textContent = isOpen ? 'Scanner schliessen' : 'Barcode scannen';
};
if (qrReader.style.display === 'none') { Quagga.init({
qrReader.style.display = 'block'; inputStream: {
name: "Live",
hybridScanner = new HybridScanner({ type: "LiveStream",
videoId: 'qr-reader-video', // Make sure this targets your correct HTML container element
canvasId: 'qr-reader-canvas', target: document.querySelector('#scanner-container'),
formats: ['QR_CODE', 'CODE_128', 'EAN_13', 'EAN_8', 'CODE_39', 'UPC_A'], constraints: {
fps: 10, width: 640,
facingMode: 'environment', height: 480,
onSuccess: function(decodedText) { facingMode: "environment" // Force rear camera
hybridScanner.stop();
qrReader.style.display = 'none';
// Put the scanned code in the search box
const searchInput = document.getElementById('code-search');
if (searchInput) {
searchInput.value = decodedText;
// Trigger search automatically
searchByCode();
}
setScannerUi(false);
}, },
onError: function(error) { },
console.error('Scanner error:', error); decoder: {
} // Your required formats mapped to Quagga2 reader strings
}); readers: ["code_128_reader", "ean_reader", "code_39_reader", "upc_reader"]
}
hybridScanner.start(); }, function(err) {
setScannerUi(true); if (err) {
} else { console.error("Initialization error:", err);
if (hybridScanner) { if (statusText) statusText.innerText = "Camera Error";
hybridScanner.stop(); return;
hybridScanner = null; }
Quagga.start();
isScanning = true;
setScannerUi(true);
if (statusText) statusText.innerText = "Scanning...";
});
}
function stopScanner() {
Quagga.stop();
isScanning = false;
const qrReader = document.getElementById('qr-reader');
if (qrReader) qrReader.style.display = 'none';
setScannerUi(false);
}
function setScannerUi(isOpen) {
const scanBtn = document.getElementById('scanButton');
if (!scanBtn) return;
scanBtn.classList.toggle('is-active', isOpen);
scanBtn.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
scanBtn.textContent = isOpen ? 'Scanner schliessen' : 'Barcode scannen';
}
// 4. Quagga event listener for successful scans
Quagga.onDetected(function(data) {
const barcode = data.codeResult.code;
console.log("Barcode detected:", barcode);
// Critical: Stop scanning immediately to prevent multiple triggers
stopScanner();
// Update your results display if you have them
const resultText = document.getElementById('barcode-result');
if (resultText) resultText.innerText = barcode;
// Put the scanned code in the search box
const searchInput = document.getElementById('code-search');
if (searchInput) {
searchInput.value = barcode;
// Trigger your automatic search
if (typeof searchByCode === "function") {
searchByCode();
} }
qrReader.style.display = 'none';
setScannerUi(false);
} }
}); });
// 5. Wire up your Toggle Button click event
document.getElementById('scanButton').addEventListener('click', function() {
if (!isScanning) {
startScanner();
} else {
stopScanner();
}
});
function scanIntoEditCode() { function scanIntoEditCode() {
const qrReader = document.getElementById('qr-reader'); const qrReader = document.getElementById('qr-reader');
const editCodeInput = document.getElementById('edit-code4'); const editCodeInput = document.getElementById('edit-code4');