我正在使用 http://davidshimjs.github.io/qrcodejs/ 为事件生成 QR 码,但是当我尝试从下面显示的字符串生成代码时,出现此错误 错误:代码长度溢出。 (1716>1056)。我发现当我将
correctLevel : QRCode.CorrectLevel.H
更改为媒介时它就可以工作。当我删除描述或字符串的其他部分时它也有效。
您能否向我解释一下这些限制是如何工作的,以及我应该如何设置,以便当从用户输入生成代码时(它可以有很长的描述)它不会崩溃?
qrkod(){
this.qrCodeText = "BEGIN:VCALENDAR"+"%0D%0A"+
"VERSION:1.0"+"%0D%0A"+
"BEGIN:VEVENT"+"%0D%0A"+
"DTSTART:" + "19960401T090000" +"%0D%0A"+
"DTEND:" + "19960401T043000" +"%0D%0A"+
"SUMMARY:" + "Your Proposal Review"+"%0D%0A"+
"DESCRIPTION:" + "Steve and John to review newest proposal material"+"%0D%0A"+
"END:VEVENT"+"%0D%0A"+
"END:VCALENDAR"+"%0D%0A";
this.qrCodeText = decodeURIComponent(this.qrCodeText);
this.qrcode0 = new QRCode("qrcode", {
text: this.qrCodeText,
width: 363,
height: 385,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
}
我自己使用该库时也遇到了同样的错误,看来库中存在一个错误,如果您使用 CorrectLevel.H 并具有 192 到 220 个字符之间的字符串,它将失败(请注意,错误消息中指定的长度与字符串的长度不同)。它没有为 QR 码分配正确数量的空间/块,然后捕获该错误。
这可以通过增加/减少字符串长度使其超出该范围来解决。
正如Archit Jain在对上一个答案的评论中所建议的那样,这样做解决了我的问题:
qrCodeText = qrCodeText.padEnd(220);
这个问题很容易解决。
var qrcode = new QRCode(document.getElementById("qrcode"), {
correctLevel : QRCode.CorrectLevel.L
});
用途:
correctLevel : QRCode.CorrectLevel.L
这似乎是 QR 库的问题,解决方案是这样调用它
new QRCode(document.getElementById("qrcode-2"),"Your very long string here ")