QRCode.js -- 用 Javascript 生产二维码

QRCode.js是一个生成二维码的JS库。QRCode.js支持在DOM中使用跨浏览器Canvas和table标签的。 QRCode.js不依赖其余JS库。
javascript


基本用法:html

<div id="qrcode"></div>
<script type="text/javascript">
new QRCode(document.getElementById("qrcode"), "http://jindo.dev.naver.com/collie");
</script>


还能够添加其它选项:java

<div id="qrcode"></div>
<script type="text/javascript">
var qrcode = new QRCode(document.getElementById("qrcode"), {
    text: "http://jindo.dev.naver.com/collie",
    width: 128,
    height: 128,
    colorDark : "#000000",
    colorLight : "#ffffff",
    correctLevel : QRCode.CorrectLevel.H
});
</script>


也可使用一些方法:
jquery

qrcode.clear(); // 清除二维码
qrcode.makeCode("http://naver.com"); // 生成一个新的二维码


浏览器兼容性:git

IE6~10, Chrome, Firefox, Safari, Opera, Mobile Safari, Android, Windows Mobile 等等github



案例演示:浏览器

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
    <title></title>
</head>
<body>

<input id="text" type="text" value="http://dapengtalk.blog.51cto.com" style="width:80%" /><br />
<div id="qrcode" style="width:100px; height:100px; margin-top:15px;"></div>

<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/qrcode.js"></script>
<script>

    var qrcode = new QRCode(document.getElementById("qrcode"), {
        width : 100,
        height : 100
    });
    function makeCode () {
        var elText = document.getElementById("text");

        if (!elText.value) {
            alert("Input a text");
            elText.focus();
            return;
        }

        qrcode.makeCode(elText.value);
    }
    makeCode();
    $("#text").
    on("blur", function () {
        makeCode();
    }).
    on("keydown", function (e) {
        if (e.keyCode == 13) {
            makeCode();
        }
    });

</script>
</body>
</html>


页面截图:ide

wKiom1hFDVqDfb1qAAAzBXxWuao851.png


扫描二维码:ui

wKioL1hFDyeR1hrIAAhJCwfMxKA149.png



Github 地址:https://github.com/davidshimjs/qrcodejs spa