(function( $ ){
$.fn.qrcode = function(options) {
// if options is string,
if( typeof options === 'string' ){
options = { text: options };
}
//�����ĵĵ���
options.text = utf16to8(options.text);
// 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",
logo : ""
}, 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');
// 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);
}
}
if(options.logo != "")
{
var image = new image();
image.onload = function() {
var w = image.width;
var h = image.height;
ctx.drawimage(image,(options.width-w)/2,(options.height-h)/2);
}
image.src = options.logo;
}
// 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 = $('
')
.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 = $('
').css('height', tileh+"px").appendto($table);
for(var col = 0; col < qrcode.getmodulecount(); col++ ){
$(' | ')
.css('width', tilew+"px")
.css('background-color', qrcode.isdark(row, col) ? options.foreground : options.background)
.appendto($row);
}
}
// return just built table
if(options.logo != "")
{
$table.css("position", "absolute").css("z-index", "0");
//������table������logo��ĸ�����
var divcontainer = $('')
.css("width", options.width+"px")
.css("height", options.height+"px")
.css("position", "relative");
var logodiv = $('')
.css("width", options.width+"px")
.css("height", options.height+"px")
.css("background", "url("+options.logo+") no-repeat center center")
.css("position", "absolute")
.css("z-index", "1");
//��logo��������
divcontainer.append(logodiv);
//��table��������
divcontainer.append($table);
return divcontainer;
}
return $table;
}
return this.each(function(){
var element = options.render == "canvas" ? createcanvas() : createtable();
$(element).appendto(this);
});
};
})( jquery );
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;
}