使用jquery-qrcode生成二维码
2016-08-29 17:38
621 查看
java方面二维码相关工具大概有qrcode和zxing,但是一般来说应用二维码比较多的是生成,扫描用的比较少,而qrcode和zxing是两者都有,所以比较臃肿。
jquery-qrcode,这个开源的三方库(可以从https://github.com/jeromeetienne/jquery-qrcode 获取)。
qrcode.js 是实现二维码数据计算的核心类,
jquery.qrcode.js 是把它用jquery方式封装起来的,用它来实现图形渲染,其实就是画图(支持canvas和table两种方式)。
支持的参数
text : "1233" //设置二维码内容
render : "canvas",//设置渲染方式
width : 256, //设置宽度
height : 256, //设置高度
typeNumber : -1, //计算模式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000" //前景颜色
简单非中文生成二维码:
$('#test').qcode('123');
自定义配置生成二维码:
$('#test').qcode({
text : '你好',
type : 'cn',
width : 100,
height : 100
});
其实上面的js有一个小小的缺点,就是默认不支持中文。
这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,
而这个方法默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,
英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。
解决方式当然是,在二维码编码前把字符串转换成UTF-8,具体代码如下:
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;
}
引入中文转码的js
<!-- jquery-1.11.1 -->
<script type="text/javascript" src="jquery/1.11.1/jquery.min.js"></script>
<!-- jquery-qrcode -->
<script type="text/javascript" src="jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<!-- 中文转码 -->
<script type="text/javascript">
function qcodetochar(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;
};
</script>
jquery-qrcode,这个开源的三方库(可以从https://github.com/jeromeetienne/jquery-qrcode 获取)。
qrcode.js 是实现二维码数据计算的核心类,
jquery.qrcode.js 是把它用jquery方式封装起来的,用它来实现图形渲染,其实就是画图(支持canvas和table两种方式)。
支持的参数
text : "1233" //设置二维码内容
render : "canvas",//设置渲染方式
width : 256, //设置宽度
height : 256, //设置高度
typeNumber : -1, //计算模式
correctLevel : QRErrorCorrectLevel.H,//纠错等级
background : "#ffffff",//背景颜色
foreground : "#000000" //前景颜色
简单非中文生成二维码:
$('#test').qcode('123');
自定义配置生成二维码:
$('#test').qcode({
text : '你好',
type : 'cn',
width : 100,
height : 100
});
其实上面的js有一个小小的缺点,就是默认不支持中文。
这跟js的机制有关系,jquery-qrcode这个库是采用 charCodeAt() 这个方式进行编码转换的,
而这个方法默认会获取它的 Unicode 编码,一般的解码器都是采用UTF-8, ISO-8859-1等方式,
英文是没有问题,如果是中文,一般情况下Unicode是UTF-16实现,长度2位,而UTF-8编码是3位,这样二维码的编解码就不匹配了。
解决方式当然是,在二维码编码前把字符串转换成UTF-8,具体代码如下:
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;
}
引入中文转码的js
<!-- jquery-1.11.1 -->
<script type="text/javascript" src="jquery/1.11.1/jquery.min.js"></script>
<!-- jquery-qrcode -->
<script type="text/javascript" src="jquery.qrcode/1.0/jquery.qrcode.min.js"></script>
<!-- 中文转码 -->
<script type="text/javascript">
function qcodetochar(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;
};
</script>
相关文章推荐
- 使用jquery插件qrcode生成二维码
- 使用jQuery的插件qrcode生成二维码(静态+动态生成)及常见问题解决方法
- 使用jquery-qrcode生成二维码
- 使用jquery-qrcode在页面上生成二维码,支持中文
- 使用jquery插件qrcode生成二维码
- 使用jQuery插件qrcode生成二维码
- 使用jquery组件qrcode生成二维码及应用指南
- 使用jquery-qrcode生成二维码(兼容中文)
- 使用jquery-qrcode生成二维码
- 使用jquery-qrcode生成二维码
- 使用jquery组件qrcode生成二维码及应用指南
- 使用jquery-qrcode在页面上生成二维码,支持中文
- 使用jquery-qrcode生成二维码
- 使用jquery-qrcode生成二维码
- 使用jquery-qrcode在页面上生成二维码,支持中文
- 使用jquery-qrcode生成二维码
- 使用jquery-qrcode在页面上生成二维码,支持中文
- 使用jquery-qrcode生成二维码
- 使用jQuery插件qrcode生成的二维码在微信中不能够识别
- java实现二维码-使用jquery-qrcode方式生成二维码