QR code to dataURL

巧了我就是萌 提交于 2019-12-25 06:38:25

问题


I am trying to generate a QR code using davidshimjs/qrcodejs with the code below. But, when i try to generate DataURL, it gives following error :

TypeError: document.getElementById(...).toDataURL is not a function

Below is my code:

HTML :

<div id="qrcode"></div>

JS :

var qrcode = new QRCode("qrcode", {
    text: QRId,
    width: 200,
    height: 200,
    colorDark : "#000000",
    colorLight : "#ffffff",
    correctLevel : QRCode.CorrectLevel.H
});
var dataURL = document.getElementById('qrcode').toDataURL();

回答1:


That's because toDataURL only works on the <canvas> element.

canvas.toDataURL(type, encoderOptions);

See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL

UPDATE

Here is how you would get the data URL...

var QRId = "123456789"
var qrcode = new QRCode("qrcode", {
    text: QRId,
    width: 200,
    height: 200,
    colorDark : "#000000",
    colorLight : "#ffffff",
    correctLevel : QRCode.CorrectLevel.H
});

// get the qr div, then find the canvas element inside it
var canvas = document.getElementById('qrcode').querySelector('canvas');

var dataURL = canvas.toDataURL();

document.getElementById('result').innerHTML = dataURL;
<script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/master/qrcode.js"></script>

<div id="qrcode"></div>

<div id="result"></div>


来源:https://stackoverflow.com/questions/38900628/qr-code-to-dataurl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!