I use this library for qrcode scan https://github.com/mebjas/html5-qrcode
It works fine but I can't stop camera after successful scan and camera stays open all the time after usage.
This is the code I used, and I use suggested stop function too but not working.
docReady(function () {
var resultContainer = document.getElementById('qr-reader-results');
var lastResult, countResults = 0;
function onScanSuccess(decodedText, decodedResult) {
if (decodedText !== lastResult) {
++countResults;
lastResult = decodedText;
// Handle on success condition with the decoded message.
console.log(`Scan result ${decodedText}`, decodedResult);
add_my_by_qr_code(decodedText);
html5QrcodeScanner.stop().then((ignore) => {
// QR Code scanning is stopped.
}).catch((err) => {
// Stop failed, handle it.
});
}
}
var html5QrcodeScanner = new Html5QrcodeScanner(
"qr-reader", { fps: 10, qrbox: 250 });
html5QrcodeScanner.render(onScanSuccess);
I found the solution to trigger click event on library's "Stop Camera" button which is shown when camera is active.
jQuery way
$("#html5-qrcode-button-camera-stop").trigger("click");
Pure JS
document.getElementById("html5-qrcode-button-camera-stop").click();
But it is better to check whether the button is active and also the scanner division visible (if you are showing scanner after a button is clicked)
if (
$("#html5-qrcode-button-camera-stop").length
&&
!$('#qr-reader').is(':visible')
)
$("#html5-qrcode-button-camera-stop").trigger("click");