尝试扫描二维码然后调用 API
Trying to scan a QR Code then call an API
我正在尝试创建一个移动应用程序,用户将在其中扫描唯一的 QR 码,然后该程序应执行检查以确保扫描了正确的 QR 码(而不是随机的 QR 码)并且如果扫描了正确的代码 QR,则应调用 API。我从 https://www.sitepoint.com/create-qr-code-reader-mobile-website/ 找到了一个适用于手机的 javascript 二维码扫描仪。这一切都很完美,因为它可以访问设备摄像头并扫描二维码,但我正在努力研究如何检查是否扫描了正确的二维码。为了测试 "correct" QR 码只是转换为字符串 "hello".
我试过使用 alert(res) 找到 QR 码被解码的点。当我尝试使用 if 语句将解码的 QR 与等效于 "hello" 的变量进行比较时,QR 扫描器停止工作。
完整代码见https://www.sitepoint.com/create-qr-code-reader-mobile-website/
这是解码二维码图像的部分。
function openQRCamera(node) {
var reader = new FileReader();
reader.onload = function() {
node.value = "";
qrcode.callback = function(res) {
if(res instanceof Error) {
alert("No QR code found. Please make sure the QR code is within the camera's frame and try again.");
} else {
node.parentNode.previousElementSibling.value = res; }
alert(res)
};
qrcode.decode(reader.result);
};
reader.readAsDataURL(node.files[0]);
}
它说 alert(res) 的部分是我相信 QR 图像被解码的地方,我已经尝试了各种 if 语句但无法让它工作任何人都可以帮助。
使用 sitepoint.com 中的上述 link 并遵循教程,我没有 运行 对返回的响应有任何问题。尝试检查它是否像这样工作:
function openQRCamera(node) {
var reader = new FileReader();
reader.onload = function() {
node.value = "";
qrcode.callback = function(res) {
if(res instanceof Error) {
alert("Failed to scan QR code.");
} else {
console.log("Scanned new code: " + res.trim());
if (res.trim() === "hello") {
alert("Correct code!");
} else {
alert("Incorrect code!");
}
}
};
qrcode.decode(reader.result);
};
reader.readAsDataURL(node.files[0]);
}
function showQRIntro() {
return confirm("Please scan the code");
}
我用这个 image as an example of a correct code and this image 作为不正确的例子。
希望一切顺利,祝你好运:)
我正在尝试创建一个移动应用程序,用户将在其中扫描唯一的 QR 码,然后该程序应执行检查以确保扫描了正确的 QR 码(而不是随机的 QR 码)并且如果扫描了正确的代码 QR,则应调用 API。我从 https://www.sitepoint.com/create-qr-code-reader-mobile-website/ 找到了一个适用于手机的 javascript 二维码扫描仪。这一切都很完美,因为它可以访问设备摄像头并扫描二维码,但我正在努力研究如何检查是否扫描了正确的二维码。为了测试 "correct" QR 码只是转换为字符串 "hello".
我试过使用 alert(res) 找到 QR 码被解码的点。当我尝试使用 if 语句将解码的 QR 与等效于 "hello" 的变量进行比较时,QR 扫描器停止工作。
完整代码见https://www.sitepoint.com/create-qr-code-reader-mobile-website/
这是解码二维码图像的部分。
function openQRCamera(node) {
var reader = new FileReader();
reader.onload = function() {
node.value = "";
qrcode.callback = function(res) {
if(res instanceof Error) {
alert("No QR code found. Please make sure the QR code is within the camera's frame and try again.");
} else {
node.parentNode.previousElementSibling.value = res; }
alert(res)
};
qrcode.decode(reader.result);
};
reader.readAsDataURL(node.files[0]);
}
它说 alert(res) 的部分是我相信 QR 图像被解码的地方,我已经尝试了各种 if 语句但无法让它工作任何人都可以帮助。
使用 sitepoint.com 中的上述 link 并遵循教程,我没有 运行 对返回的响应有任何问题。尝试检查它是否像这样工作:
function openQRCamera(node) {
var reader = new FileReader();
reader.onload = function() {
node.value = "";
qrcode.callback = function(res) {
if(res instanceof Error) {
alert("Failed to scan QR code.");
} else {
console.log("Scanned new code: " + res.trim());
if (res.trim() === "hello") {
alert("Correct code!");
} else {
alert("Incorrect code!");
}
}
};
qrcode.decode(reader.result);
};
reader.readAsDataURL(node.files[0]);
}
function showQRIntro() {
return confirm("Please scan the code");
}
我用这个 image as an example of a correct code and this image 作为不正确的例子。
希望一切顺利,祝你好运:)