龍が如く
龍が如く

Reputation: 309

Create a qrcode image then convert to base64

I am using http://davidshimjs.github.io/qrcodejs/ to generate qr-code

          var qrcode = new QRCode("qrcode", {
            text: "http://jindo.dev.naver.com/collie",
            width: 128,
            height: 128,
            colorDark: "#000000",
            colorLight: "#ffffff",
            correctLevel: QRCode.CorrectLevel.H
          });
          console.log(qrcode);
          console.log(qrcode._el.children[1].currentSrc);

enter image description here

I can find the base64 code in "qrcode"
But why I get empty when I run:

console.log(qrcode._el.children[1].currentSrc);

enter image description here

Is there any other method to get the base64 code?

Upvotes: 1

Views: 5268

Answers (1)

Oleksandr Karpov
Oleksandr Karpov

Reputation: 624

Try:

setTimeout(function () {
    const base64String = document.querySelector("#qrcode img").src;
});

The reason for that is that it simply runs async and needs some time to be set to the image attribute.

Upvotes: 1

Related Questions