您好,欢迎访问一九零五行业门户网

使用jquery.qrcode生成二维码

使用jquery.qrcode生成二维码
原文地址http://www.bieanju.com/75.html
试验的时候发现不能识别中文内容的二维码,通过查找多方资料了解到,jquery-qrcode是采用charcodeat()方式进行编码转换的。而这个方法默认会获取它的unicode编码,如果有中文内容,在生成二维码前就要把字符串转换成utf-8,然后再生成二维码。您可以通过以下函数来转换中文字符串:
function toutf8(str) {    
    var out, i, len, c;    
    out = ;    
    len = str.length;    
    for(i = 0; i         c = str.charcodeat(i);    
        if ((c >= 0x0001) && (c             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;    
} 以下示例:
var str = toutf8(程序猿!); 
$('#code').qrcode(str); 
qrcode.zip ( 5.88 kb 下载:248 次 )
ad:真正免费,域名+虚机+企业邮箱=0元
其它类似信息

推荐信息