javaScript生成支持中文帶logo的二維碼(jquery.qrcode.js)
本文實(shí)例為大家分享了支持中文,且?guī)ogo的二維碼的生成代碼,供大家參考,具體內(nèi)容如下
資料搜索

選擇star最多的兩個(gè)

第一個(gè)就是用的比較多的jquery.qrcode.js(但不支持中文,不能帶logo)啦,第二個(gè)支持ie6+,支持中文,根據(jù)第二個(gè)源代碼,使得,jquery.qrcode.js,支持中文。
支持中文
//qrcode.js
function QR8bitByte(data) {
this.mode = QRMode.MODE_8BIT_BYTE;
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);
}
}
};
修改如下(就是復(fù)制粘貼了第二份代碼的頭部):
function QR8bitByte(data) {
this.mode = QRMode.MODE_8BIT_BYTE;
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 = Array.prototype.concat.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;
},
write: function (buffer) {
for (var i = 0, l = this.parsedData.length; i < l; i++) {
buffer.put(this.parsedData[i], 8);
}
}
};
網(wǎng)上也提供的解決方案:
//在傳入文本處轉(zhuǎn)碼也可
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函數(shù)
var createCanvas = function(){
// create the qrcode itself
var qrcode = new QRCode(options.typeNumber, options.correctLevel);
qrcode.addData(options.text);
qrcode.make();
// create canvas element
var canvas = document.createElement('canvas');
canvas.width = options.width;
canvas.height = options.height;
var ctx = canvas.getContext('2d');
//增加以下代碼,把圖片畫出來(lái)
if( options.src ) {//傳進(jìn)來(lái)的圖片地址
//圖片大小
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里,圖片出不來(lái)
img.onload = function () {
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 / qrcode.getModuleCount();
var tileH = options.height / qrcode.getModuleCount();
// draw in the canvas
for( var row = 0; row < qrcode.getModuleCount(); row++ ){
for( var col = 0; col < qrcode.getModuleCount(); col++ ){
ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
}
}
// return just built canvas
return canvas;
};
修改jquery.qrcode.js,createTable函數(shù)(不支持canvas用table畫二維碼)
var createTable = function(){
// create the qrcode itself
var qrcode = new QRCode(options.typeNumber, options.correctLevel);
qrcode.addData(options.text);
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 / qrcode.getModuleCount();
var tileH = options.height / qrcode.getModuleCount();
// draw in the table
for(var row = 0; row < qrcode.getModuleCount(); row++ ){
var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
for(var col = 0; col < qrcode.getModuleCount(); col++ ){
$('<td></td>')
.css('width', tileW+"px")
.css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
.appendTo($row);
}
}
//主要思想,把table,和img標(biāo)簽放在同一個(gè)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 style="position:relative;"></div>')
.append($table)
.append($img);
}
// return just built canvas
return $table;
};
對(duì)IE做特殊判斷,大家懂的
//判斷是否IE, IE8以下,用 table,否則用 canvas
var isIE = function() {
var b = document.createElement('b');
b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
return b.getElementsByTagName('i').length === 1;
};
options.render = options.render ||
(isIE(6) || isIE(7) || isIE(8))? "table": "canvas";
改過(guò)后的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 = document.createElement('b');
b.innerHTML = '<!--[if IE]><i></i><![endif]-->';
return b.getElementsByTagName('i').length === 1;
};
options.render = options.render ||
(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 QRCode(options.typeNumber, options.correctLevel);
qrcode.addData(options.text);
qrcode.make();
// create canvas element
var canvas = document.createElement('canvas');
canvas.width = options.width;
canvas.height = options.height;
var ctx = canvas.getContext('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;
img.onload = function () {
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 / qrcode.getModuleCount();
var tileH = options.height / qrcode.getModuleCount();
// draw in the canvas
for( var row = 0; row < qrcode.getModuleCount(); row++ ){
for( var col = 0; col < qrcode.getModuleCount(); col++ ){
ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
var h = (Math.ceil((row+1)*tileW) - Math.floor(row*tileW));
ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
}
}
// return just built canvas
return canvas;
};
// from Jon-Carlos Rivera (https://github.com/imbcmdth)
var createTable = function(){
// create the qrcode itself
var qrcode = new QRCode(options.typeNumber, options.correctLevel);
qrcode.addData(options.text);
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 / qrcode.getModuleCount();
var tileH = options.height / qrcode.getModuleCount();
// draw in the table
for(var row = 0; row < qrcode.getModuleCount(); row++ ){
var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
for(var col = 0; col < qrcode.getModuleCount(); 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 style="position:relative;"></div>')
.append($table)
.append($img);
}
// return just built canvas
return $table;
};
return this.each(function(){
var element = options.render == "canvas" ? createCanvas() : createTable();
$(element).appendTo(this);
});
};
})( jQuery );
測(cè)試
jQuery('#qrcodeTable').qrcode({
render : "table",
text : "中文://jetienne.com",
src: './logo32.png'
});
jQuery('#qrcodeCanvas').qrcode({
text : "中午你://jetienne.com",
src: './logo32.png'
});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
用Javascript實(shí)現(xiàn)錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn)
本文介紹的方法,實(shí)現(xiàn)了錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn),效果非常不錯(cuò)。2009-09-09
bootstrap table列和表頭對(duì)不齊的解決方法
這篇文章主要為大家詳細(xì)介紹了bootstrap table列和表頭對(duì)不齊的解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
用 js 的 selection range 操作選擇區(qū)域內(nèi)容和圖片
本篇文章主要介紹了用js的selection range操作選擇區(qū)域內(nèi)容和圖片的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04
微信小程序長(zhǎng)按識(shí)別二維碼的幾種情況分析
最近接到需求,在小程序內(nèi)部長(zhǎng)按識(shí)別二維碼添加個(gè)人微信,下面這篇文章主要給大家分析介紹了關(guān)于微信小程序長(zhǎng)按識(shí)別二維碼的幾種情況,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
JavaScript使用鏈?zhǔn)椒椒ǚ庋bjQuery中CSS()方法示例
這篇文章主要介紹了JavaScript使用鏈?zhǔn)椒椒ǚ庋bjQuery中CSS()方法,結(jié)合具體實(shí)例形式分析了JS采用鏈?zhǔn)讲僮鞣椒ǚ庾Query中連綴操作實(shí)現(xiàn)css()方法的相關(guān)技巧,需要的朋友可以參考下2017-04-04
Javascript中String的常用方法實(shí)例分析
這篇文章主要介紹了Javascript中String的常用方法,實(shí)例分析了String常用的字符轉(zhuǎn)換、截取、分割等技巧,需要的朋友可以參考下2015-06-06
JavaScript中的await函數(shù)使用小結(jié)
async 函數(shù)是 AsyncFunction 構(gòu)造函數(shù)的實(shí)例,并且其中允許使用 await 關(guān)鍵字,async 和 await 關(guān)鍵字讓我們可以用一種更簡(jiǎn)潔的方式寫出基于 Promise 的異步行為,而無(wú)需刻意地鏈?zhǔn)秸{(diào)用 promise,這篇文章主要介紹了JavaScript中的await,需要的朋友可以參考下2024-01-01
JS來(lái)動(dòng)態(tài)的修改url實(shí)現(xiàn)對(duì)url的增刪查改
通過(guò)get方式提交post表單等方式來(lái)動(dòng)態(tài)修改url存在諸多的不妥,因此,想到了通過(guò)JS來(lái)動(dòng)態(tài)的修改url,來(lái)實(shí)現(xiàn)對(duì)url的增刪查改2014-09-09
打印Proxy對(duì)象和ref對(duì)象的包實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了打印Proxy對(duì)象和ref對(duì)象的包實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

