使用来自 javascript 的输出 base64 创建 qrCode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23861986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:42:52  来源:igfitidea点击:

Create qrCode with output base64 from javascript

javascriptbase64qr-code

提问by Reinstar

I want to create qr-code with output base64 string from javascript.

我想用来自 javascript 的输出 base64 字符串创建 qr 代码。

I found http://davidshimjs.github.io/qrcodejs/for generate qr-code. That I want is get base64 string from this awesome tools.

我发现http://davidshimjs.github.io/qrcodejs/用于生成二维码。我想要的是从这个很棒的工具中获取 base64 字符串。

回答by Andrey Vaganov

Much more better and clear solution with http://neocotic.com/qr.js

使用http://neocotic.com/qr.js 提供更好更清晰的解决方案

qr.toDataURL(yourAwesomeData); //yourAwesomeData in Base64

回答by Sébastien Barré

you can get the Base64 directly from the generated variable, i.e.:

您可以直接从生成的变量中获取 Base64,即:

/*generate QRCode*/
var qrcodjs = new QRCode("qrcode", {
    text: orderData.reference,
    width: 128,
    height: 128,
    colorDark: "#000000",
    colorLight: "#ffffff",
    correctLevel: QRCode.CorrectLevel.H
});

/*get base64*/
var imgBase64Data = qrcodjs._el.childNodes[4].currentSrc;

This will work unfortunately only on google chrome or Firefox... for IE or Safari you will have to use:

不幸的是,这仅适用于 google chrome 或 Firefox ... 对于 IE 或 Safari,您必须使用:

                var imgBase64 = qrcodjs._oDrawing._elImage.currentSrc;
                if (typeof imgBase64 === "undefined")
                    imgBase64 = qrcodjs._oDrawing._elImage.href;
                if (typeof imgBase64 === "undefined")
                    imgBase64 = qrcodjs._oDrawing._elImage.src;

回答by contributorpw

May be you can get this code d-project.googlecode.com

也许你可以得到这个代码d-project.googlecode.com

And replace last strings

并替换最后一个字符串

from

    var img = '';
    img += '<img';
    img += '\u0020src="';
    img += 'data:image/gif;base64,';
    img += base64;
    img += '"';
    img += '\u0020width="';
    img += width;
    img += '"';
    img += '\u0020height="';
    img += height;
    img += '"';
    if (alt) {
        img += '\u0020alt="';
        img += alt;
        img += '"';
    }
    img += '/>';

to

    var img = 'data:image/gif;base64,' + base64;

Getting a base64

获取 base64

    var qr = qrcode(4, 'M');
    qr.addData('This is base64 string');
    qr.make();
    concole.log(qr.createImgTag());