2016-01-19 22:21:07 +00:00
|
|
|
( function ( $ ) {
|
|
|
|
$.fn.qrcode = function ( options ) {
|
2014-07-21 12:31:37 +00:00
|
|
|
// if options is string,
|
2016-01-19 22:21:07 +00:00
|
|
|
if ( typeof options === 'string' ){
|
2012-05-07 21:54:44 +00:00
|
|
|
options = { text: options };
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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');
|
|
|
|
|
|
|
|
// 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));
|
2014-07-21 12:31:37 +00:00
|
|
|
ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
|
|
|
|
}
|
2012-05-07 21:54:44 +00:00
|
|
|
}
|
|
|
|
// return just built canvas
|
|
|
|
return canvas;
|
2014-07-21 12:31:37 +00:00
|
|
|
};
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
// 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();
|
2014-07-21 12:31:37 +00:00
|
|
|
|
2012-05-07 21:54:44 +00:00
|
|
|
// 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);
|
2014-07-21 12:31:37 +00:00
|
|
|
|
2012-05-07 21:54:44 +00:00
|
|
|
// 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);
|
2014-07-21 12:31:37 +00:00
|
|
|
|
2012-05-07 21:54:44 +00:00
|
|
|
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);
|
2014-07-21 12:31:37 +00:00
|
|
|
}
|
2012-05-07 21:54:44 +00:00
|
|
|
}
|
|
|
|
// return just built canvas
|
|
|
|
return $table;
|
2014-07-21 12:31:37 +00:00
|
|
|
};
|
2012-05-07 21:54:44 +00:00
|
|
|
|
|
|
|
return this.each(function(){
|
2016-01-19 22:21:07 +00:00
|
|
|
var element = options.render === "canvas" ? createCanvas() : createTable();
|
|
|
|
$(element).appendTo(this);
|
|
|
|
} );
|
2012-05-07 21:54:44 +00:00
|
|
|
};
|
2016-01-19 22:21:07 +00:00
|
|
|
}( jQuery ) );
|