Giter Site home page Giter Site logo

Comments (8)

rodolfojnn avatar rodolfojnn commented on June 8, 2024 1

Another simple workaround to get the base64 is:

const qrcode = new QRCode(document.createElement('div'), {
	text: "http://jindo.dev.naver.com/collie",
	width: 128,
	height: 128,
	colorDark : "#000000",
	colorLight : "#ffffff",
	correctLevel : QRCode.CorrectLevel.H
});
qrcode._oDrawing._elImage.onload = ev => { 
    console.log(ev.target.src);
}

from qrcodejs.

rodrigomtz avatar rodrigomtz commented on June 8, 2024

I am guessing that one solution might be jquerying it to get the src of the image.. but there should be a more elegant way to solve this no?

from qrcodejs.

glen1166 avatar glen1166 commented on June 8, 2024

As [rodrigomtz] said, i got the base64 string of QR code using JQuery:

qrCode.makeCode(text);
$("img").load(function () {
//get the src attribute of image here...
});

from qrcodejs.

yanpeipan avatar yanpeipan commented on June 8, 2024

the same issue, this work is in plan?

from qrcodejs.

nmcv avatar nmcv commented on June 8, 2024

There's no callback for makeCode at all?

from qrcodejs.

kdavis45 avatar kdavis45 commented on June 8, 2024

makeCode() already creates an img with a base64 src and places it inside the

you provide it. So the best way I can see to get at that base64 string is to simply add one line of code into qrcode.js and add an id tag you can use to retrieve the b64 data. Somewhere inside var Drawing. I gave mine an id tag of "qrjs".

var Drawing = function (el, htOption) {
this._bIsPainted = false;
this._android = _getAndroid();
this._htOption = htOption;
this._elCanvas = document.createElement("canvas");
this._elCanvas.width = htOption.width;
this._elCanvas.height = htOption.height;
el.appendChild(this._elCanvas);
this._el = el;
this._oContext = this._elCanvas.getContext("2d");
this._bIsPainted = false;
this._elImage = document.createElement("img");
this._elImage.alt = "Scan me!";
this._elImage.id = "qrjs";
this._elImage.style.display = "none";
this._el.appendChild(this._elImage);
this._bSupportDataURI = null;
};

Then you simply wait for the drawing to load using .onload() and retrieve the base64 string from the source and use however you like.

Mine went something like this:

  var aeqr; // this will be used to point directly at the base 64 img part of the output
  // Instantiate a new QRCode and place it in the "qrcode" div element
  var qrurl = new QRCode(document.getElementById("qrcode"), {
            width : 140,
            height : 140
        });
    var qrdest = "https://www.airbridgelabs.com/v/"+vid+"/app.html?tm=10%26vid=" + vid;
    aeqr = document.getElementById("qrjs");
  // create the listener to fire when qrurl is finished (and aeqr has loaded with the img
  aeqr.onload = function() {
     // do something with the base64 QR image AFTER it finishes loading
     ctxq.drawImage(aeqr, 0, 0, 120, 120);
     var qrdataURL = qrcanvas.toDataURL();
  }

 qrurl.makeCode(qrdest); // start building the QR image using qrdest text AFTER you create the onload() element for it

As you can see this is great if you only need to build one QR image (or one at a time) since the id is hard coded above. If you need to get at multiple B64 QR images simultaneously, then this will require a bit more code. You could, for example, pass yet another parameter in along with width and height and provide a unique id for each img.

I know it is several months past the previous comment but hope this helps someone.

from qrcodejs.

Abobos avatar Abobos commented on June 8, 2024

Thanks.. This helps

from qrcodejs.

ibalaji777 avatar ibalaji777 commented on June 8, 2024

const qrcode = new QRCode(document.createElement('div'), {
text: "http://jindo.dev.naver.com/collie",
width: 128,
height: 128,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
var base64=qrcode._oDrawing._elImage.currentSrc

from qrcodejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.