javaScript⽣成⽀持中⽂带logo的⼆维码
(jquery.qrcode.js)
本⽂实例为⼤家分享了⽀持中⽂,且带logo的⼆维码的⽣成代码,供⼤家参考,具体内容如下
资料搜索
选择star最多的两个
第⼀个就是⽤的⽐较多的jquery.qrcode.js(但不⽀持中⽂,不能带logo)啦,第⼆个⽀持ie6+,⽀持中⽂,根据第⼆个源代码,使得,jquery.qrcode.js,⽀持中⽂。
⽀持中⽂
//qrcode.js
function QR8bitByte(data) {
this.data = data;
}
QR8bitByte.prototype = {
getLength : function(buffer) {
return this.data.length;
},
write : function(buffer) {
for (var i = 0; i < this.data.length; i++) {
// not JIS ...
buffer.put(this.data.charCodeAt(i), 8);
}
}
};
修改如下(就是复制粘贴了第⼆份代码的头部):
function QR8bitByte(data) {
this.data = data;
this.parsedData = [];
// Added to support UTF-8 Characters
for (var i = 0, l = this.data.length; i < l; i++) {
var byteArray = [];
var code = this.data.charCodeAt(i);
if (code > 0x10000) {
byteArray[0] = 0xF0 | ((code & 0x1C0000) >>> 18);
byteArray[1] = 0x80 | ((code & 0x3F000) >>> 12);
byteArray[2] = 0x80 | ((code & 0xFC0) >>> 6);
byteArray[3] = 0x80 | (code & 0x3F);
} else if (code > 0x800) {
byteArray[0] = 0xE0 | ((code & 0xF000) >>> 12);
byteArray[1] = 0x80 | ((code & 0xFC0) >>> 6);
byteArray[2] = 0x80 | (code & 0x3F);
} else if (code > 0x80) {
byteArray[0] = 0xC0 | ((code & 0x7C0) >>> 6);
byteArray[1] = 0x80 | (code & 0x3F);
} else {
byteArray[0] = code;
}
this.parsedData.push(byteArray);
}
this.parsedData = at.apply([], this.parsedData);
if (this.parsedData.length != this.data.length) {
this.parsedData.unshift(191);
this.parsedData.unshift(187);
this.parsedData.unshift(239);
}
}
QR8bitByte.prototype = {
getLength: function (buffer) {
return this.parsedData.length;
},
jquery源码在线write: function (buffer) {
for (var i = 0, l = this.parsedData.length; i < l; i++) {
buffer.put(this.parsedData[i], 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;
}
⽀持⾃定义logo
修改jquery.qrcode.js,createCanvas函数
var createCanvas  = function(){
// create the qrcode itself
var qrcode = new peNumber, tLevel);
qrcode.);
qrcode.make();
// create canvas element
var canvas = ateElement('canvas');
canvas.width  = options.width;
canvas.height  = options.height;
var ctx  = Context('2d');
/
/增加以下代码,把图⽚画出来
if( options.src ) {//传进来的图⽚地址
//图⽚⼤⼩
options.imgWidth = options.imgWidth || options.width / 4.7;
options.imgHeight = options.imgHeight || options.height / 4.7;
var img = new Image();
img.src = options.src;
//不放在onload⾥,图⽚出不来
ctx.drawImage(img, (options.width - options.imgWidth) / 2, (options.height - options.imgHeight) / 2, options.imgWidth, options.imgHeight);        }
}
// compute tileW/tileH based on options.width/options.height
var tileW  = options.width / ModuleCount();
var tileH  = options.height / ModuleCount();
// draw in the canvas
for( var row = 0; row < ModuleCount(); row++ ){
for( var col = 0; col < ModuleCount(); col++ ){
ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
var w = (il((col+1)*tileW) - Math.floor(col*tileW));
var h = (il((row+1)*tileW) - Math.floor(row*tileW));
ctx.und(col*tileW),und(row*tileH), w, h);
}
}
// return just built canvas
return canvas;
};
修改jquery.qrcode.js,createTable函数(不⽀持canvas⽤table画⼆维码)
var createTable = function(){
// create the qrcode itself
var qrcode = new peNumber, tLevel);
qrcode.);
qrcode.make();
// create table element
var $table  = $('<table></table>')
.css("width", options.width+"px")
.css("height", options.height+"px")
.css("border", "0px")
.css("border-collapse", "collapse")
.css('background-color', options.background);
// compute tileS percentage
var tileW  = options.width / ModuleCount();
var tileH  = options.height / ModuleCount();
// draw in the table
for(var row = 0; row < ModuleCount(); row++ ){
var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
for(var col = 0; col < ModuleCount(); col++ ){
$('<td></td>')
.css('width', tileW+"px")
.css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
.appendTo($row);
}
}
//主要思想,把table,和img标签放在同⼀个div下,div relative定位,然后使得图⽚absolute定位在table中间      if( options.src ) {
options.imgWidth = options.imgWidth || options.width / 4.7;
options.imgHeight = options.imgHeight || options.height / 4.7;
var $img = $('<img>').attr("src", options.src)
.css("width", options.imgWidth)
.css("height", options.imgHeight)
.css("position", "absolute")
.css("left", (options.width - options.imgWidth) / 2)
.css("top", (options.height - options.imgHeight) / 2);
$table = $('<div ></div>')
.append($table)
.append($img);
}
// return just built canvas
return $table;
};
对IE做特殊判断,⼤家懂的
//判断是否IE, IE8以下,⽤ table,否则⽤ canvas
var isIE = function() {
var b = ateElement('b');
b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
ElementsByTagName('i').length === 1;
};
(isIE(6) || isIE(7) || isIE(8))? "table": "canvas";
改过后的jquery.qrcode.js如下:
(function( $ ){
$.fn.qrcode = function(options) {
// if options is string,
if( typeof options === 'string' ){
options = { text: options };
}
//判断是否IE, IE8以下,⽤ table,否则⽤ canvas
var isIE = function() {
var b = ateElement('b');
b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
ElementsByTagName('i').length === 1;
};
(isIE(6) || isIE(7) || isIE(8))? "table": "canvas";
// set default values
// typeNumber < 1 for automatic calculation
options = $.extend( {}, {
// render    : "canvas",
width    : 256,
height  : 256,
typeNumber : -1,
correctLevel  : QRErrorCorrectLevel.H,
background  : "#ffffff",
foreground  : "#000000"
}, options);
var createCanvas  = function(){
// create the qrcode itself
var qrcode = new peNumber, tLevel);
qrcode.);
qrcode.make();
// create canvas element
var canvas = ateElement('canvas');
canvas.width  = options.width;
canvas.height  = options.height;
var ctx  = Context('2d');
//在中间画logo
if( options.src ) {
options.imgWidth = options.imgWidth || options.width / 4.7;
options.imgHeight = options.imgHeight || options.height / 4.7;
var img = new Image();
img.src = options.src;
ctx.drawImage(img, (options.width - options.imgWidth) / 2, (options.height - options.imgHeight) / 2, options.imgWidth, options.imgHeight);        }
}
// compute tileW/tileH based on options.width/options.height
var tileW  = options.width / ModuleCount();
var tileH  = options.height / ModuleCount();
// draw in the canvas
for( var row = 0; row < ModuleCount(); row++ ){
for( var col = 0; col < ModuleCount(); col++ ){
ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
var w = (il((col+1)*tileW) - Math.floor(col*tileW));
var h = (il((row+1)*tileW) - Math.floor(row*tileW));
ctx.und(col*tileW),und(row*tileH), w, h);
}
}
// return just built canvas
return canvas;
};
// from Jon-Carlos Rivera (github/imbcmdth)
var createTable = function(){
// create the qrcode itself
var qrcode = new peNumber, tLevel);
qrcode.);
qrcode.make();
/
/ create table element
var $table  = $('<table></table>')
.css("width", options.width+"px")
.css("height", options.height+"px")
.css("border", "0px")
.css("border-collapse", "collapse")
.css('background-color', options.background);
// compute tileS percentage
var tileW  = options.width / ModuleCount();
var tileH  = options.height / ModuleCount();
// draw in the table
for(var row = 0; row < ModuleCount(); row++ ){
var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
for(var col = 0; col < ModuleCount(); col++ ){
$('<td></td>')
.css('width', tileW+"px")
.css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)            .appendTo($row);
}
}
//⽣成logo
if( options.src ) {
options.imgWidth = options.imgWidth || options.width / 4.7;
options.imgHeight = options.imgHeight || options.height / 4.7;
var $img = $('<img>').attr("src", options.src)
.css("width", options.imgWidth)
.css("height", options.imgHeight)
.css("position", "absolute")
.css("left", (options.width - options.imgWidth) / 2)
.css("top", (options.height - options.imgHeight) / 2);
$table = $('<div ></div>')
.append($table)
.append($img);
}
/
/ return just built canvas
return $table;
};
return this.each(function(){
var element = der == "canvas" ? createCanvas() : createTable();
$(element).appendTo(this);
});
};
})( jQuery );
测试
jQuery('#qrcodeTable').qrcode({
render : "table",
text  : "中⽂://jetienne",
src: './logo32.png'
});
jQuery('#qrcodeCanvas').qrcode({
text  : "中午你://jetienne",
src: './logo32.png'
});
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。