利用JS生成二维码图片,支持中文

转自cloud.tencent.com/developer/article/1068483

只贴快速使用的片段了

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="//cdn.bootcss.com/jquery/1.8.2/jquery.min.js"></script>
    <script src="//cdn.bootcss.com/jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div id="output"></div>
            <script type="text/javascript">
                function utf16to8(str) {
                    var out, i, len, c;
                    out = "";
                    len = str.length;
                    for (i = 0; i < len; i++) {
                        c = str.charCodeAt(i);
                        if ((c >= 0x0001) && (c <= 0x007F)) {
                            out += str.charAt(i);
                        } else if (c > 0x07FF) {
                            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                        } else {
                            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
                        }
                    }
                    return out;
                }
                content = utf16to8('手机扫一扫,精彩随声带!');
                $('#output').qrcode({
                    width: 200,
                    height: 200,
                    render: "canvas",
                    correctLevel: 0,
                    text: content
                });
            </script>
        </div>
    </form>
</body>
</html>

 

posted @ 2019-11-12 17:46  离。  阅读(41)  评论(0编辑  收藏  举报