如何解决使用For循环对Java中的Qr代码扫描器进行计数
我正在从这里使用Qr码扫描仪:https://github.com/mebjas/html5-qrcode
我成功启动了扫描仪,它将返回结果。
我要实现的结果是扫描11次qr代码,并将其追加到列表中。
PS:由于使用Framework7自定义DOM,因此请调用$$
我尝试循环它:
$$(document).on('click','#skvqr',function () {
Html5Qrcode.getCameras().then(devices => {
/**
* devices would be an array of objects of type:
* { id: "id",label: "label" }
*/
if (devices && devices.length) {
var cameraId = devices[1].id;
// .. use this to start scanning.
const html5QrCode = new Html5Qrcode(/* element id */ "reader");
html5QrCode.start(
cameraId,{
fps: 10,// Optional frame per seconds for qr code scanning
qrBox: 200 // Optional if you want bounded Box UI
},qrCodeMessage => {
// do something when code is read
alert("scan succesful " + qrCodeMessage)
for (i = 11; i >= 1; --i) {
alert("scan succesful " + qrCodeMessage + " Press Ok and Scan " + i + " more times")
$$("#qr-scan-3").append('<input type="text" value=" ' + 'SKV : ' + qrCodeMessage + ' " placeholder="Qr Scan 1">' +
'<span class="input-clear-button"></span>'
);
this.style.display = 'none'
html5QrCode.stop().then(ignore => {
// QR Code scanning is stopped.
}).catch(err => {
// Stop Failed,handle it.
});
}
},errorMessage => {
// parse error,ignore it.
})
.catch(err => {
// Start Failed,handle it.
});
}
// ------------------------------------------------------------------------
}).catch(err => {
// handle err
});
});
它会给我11次警告
("scan succesful " + qrCodeMessage + " Press Ok and Scan " + i + " more times")
,它只会第一次扫描。
请帮助我,仍然在学习JavaScript
解决方法
看起来您需要保持对成功回调全局的状态。
// define the scanner object
const html5QrCode = new Html5Qrcode(/* element id */ "reader");
let matchesFound = 0;
const totalScansNeeded = 11;
var successCallback = qrCodeMessage => {
matchesFound++;
if (matchesFound < totalScansNeeded) {
let remainingScans = totalScansNeeded - matchesFound;
alert(`scan successful ${qrCodeMessage}. Press Ok `
+ `and Scan ${remainingScans} more times"`);
} else {
// .. expected matches found
// .. do required UI changes
html5QrCode.stop().then(ignore => {
// QR Code scanning is stopped.
}).catch(err => {
// Stop failed,handle it.
});
}
}
然后在实际的侦听器中使用它
$$(document).on('click','#skvqr',function () {
Html5Qrcode.getCameras().then(devices => {
if (devices && devices.length) {
var cameraId = devices[1].id;
html5QrCode.start(
cameraId,{ fps: 10,qrbox: 200 },successCallback)
.catch(err => {
// Start failed,handle it.
});
}
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。