Untitled
unknown
plain_text
a year ago
2.8 kB
12
Indexable
// Setup webcam
Webcam.set({
width: 420,
height: 340,
image_format: 'jpeg',
jpeg_quality: 90
});
// Attach webcam to the DOM element
Webcam.attach('#TakeAbsen');
// Ensure webcam is ready before scanning
Webcam.on('load', function() {
console.log('Webcam is ready');
// Start scanning for QR codes
setInterval(scanQRCode, 1000); // Scan every 1 second
});
function scanQRCode() {
Webcam.snap(function(dataUri) {
// Create an image element
let img = new Image();
img.src = dataUri;
// Wait for the image to load
img.onload = function() {
// Draw the image on the canvas
let canvas = document.getElementById('qr-canvas');
let context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0, canvas.width, canvas.height);
// Get the image data
let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
// Use jsQR to detect the QR code
let code = jsQR(imageData.data, canvas.width, canvas.height);
if (code) {
console.log('Found QR code:', code.data);
// Send the result to the backend
fetch('<?= base_url('presensi/check_out') ?>', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-Token': '<?= csrf_hash() ?>' // Adjust as necessary for CSRF protection
},
body: JSON.stringify({ nik: code.data, lokasi: document.getElementById("lokasi").value })
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Check-out recorded successfully');
window.location.href = '<?= base_url('presensi/success') ?>';
} else {
alert(data.message);
window.location.href = '<?= base_url('presensi/failure') ?>';
}
})
.catch(error => {
console.error('Error:', error);
window.location.href = '<?= base_url('presensi/failure') ?>';
});
}
};
});
}Editor is loading...
Leave a Comment