From e7ba02508e7fb2dd42d99c63b90d0fb8e126360d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=AB=E8=B0=B7=E5=89=91=E4=BB=99?= Date: Sat, 29 May 2021 03:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8D=B3=E6=97=B6=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E9=80=89=E6=8B=A9=E7=9A=84=E8=AE=BE=E5=A4=87=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script.js | 82 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 35 deletions(-) diff --git a/script.js b/script.js index 3fbe85ff..837896af 100644 --- a/script.js +++ b/script.js @@ -1180,12 +1180,53 @@ function initialize() { console.log(err); }); } + if (location.protocol == "http:" && location.host != "localhost" && location.host != "127.0.0.1") { //http不支持攝像頭 qrContent.readQrCamera.classList.add(className_displayNone); }else { + + function scanContinuously() + { + qrcodeReader.decodeFromInputVideoDeviceContinuously(selectedDeviceId, 'video', (result, err) => { + if (result) { + // properly decoded qr code + console.log('Found QR code!', result); + let inputResult = inputFromQrString(result.text); + + if (inputResult.code == 1) + { //成功后就关闭 + qrContent.readQrCamera.onclick(); + qrContent.info.textContent = ""; + const newLink = document.createElement("a"); + newLink.className = "formation-from-qrcode"; + newLink.href = inputResult.url; + newLink.target = "_blank"; + qrContent.info.appendChild(newLink); + }else + { + qrContent.info.textContent = inputResult.code + ':' + inputResult.message; + } + } + + if (err) { + if (err instanceof ZXing.NotFoundException) { + console.log('No QR code found.') + } + + if (err instanceof ZXing.ChecksumException) { + console.log('A code was found, but it\'s read value was not valid.') + } + + if (err instanceof ZXing.FormatException) { + console.log('A code was found, but it was in a invalid format.') + } + } + }); + } + qrcodeReader.getVideoInputDevices() .then((videoInputDevices) => { const sourceSelect_id = "selected-device-id"; @@ -1206,6 +1247,11 @@ function initialize() { qrContent.sourceSelect.onchange = function() { selectedDeviceId = this.value; localStorage.setItem(cfgPrefix + sourceSelect_id, this.value); + if (qrContent.readQrCamera.classList.contains("running")) + { + qrcodeReader.reset(); + scanContinuously(); + } }; } qrContent.readQrCamera.onclick = function() @@ -1221,41 +1267,7 @@ function initialize() { { qrContent.cavans.classList.add(className_displayNone); qrContent.videoBox.classList.remove(className_displayNone); - qrcodeReader.decodeFromInputVideoDeviceContinuously(selectedDeviceId, 'video', (result, err) => { - if (result) { - // properly decoded qr code - console.log('Found QR code!', result); - let inputResult = inputFromQrString(result.text); - - if (inputResult.code == 1) - { //成功后就关闭 - qrContent.readQrCamera.onclick(); - qrContent.info.textContent = ""; - const newLink = document.createElement("a"); - newLink.className = "formation-from-qrcode"; - newLink.href = inputResult.url; - newLink.target = "_blank"; - qrContent.info.appendChild(newLink); - }else - { - qrContent.info.textContent = inputResult.code + ':' + inputResult.message; - } - } - - if (err) { - if (err instanceof ZXing.NotFoundException) { - console.log('No QR code found.') - } - - if (err instanceof ZXing.ChecksumException) { - console.log('A code was found, but it\'s read value was not valid.') - } - - if (err instanceof ZXing.FormatException) { - console.log('A code was found, but it was in a invalid format.') - } - } - }) + scanContinuously(); } this.classList.toggle("running"); }