jquery.qrcode.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. http://jeromeetienne.github.io/jquery-qrcode
  3. https://github.com/jeromeetienne/jquery-qrcode
  4. */
  5. (function( $ ){
  6. $.fn.qrcode = function(options) {
  7. // if options is string,
  8. if( typeof options === 'string' ){
  9. options = { text: options };
  10. }
  11. // set default values
  12. // typeNumber < 1 for automatic calculation
  13. options = $.extend( {}, {
  14. render : "canvas",
  15. width : 256,
  16. height : 256,
  17. typeNumber : -1,
  18. correctLevel : QRErrorCorrectLevel.H,
  19. background : "#ffffff",
  20. foreground : "#000000"
  21. }, options);
  22. var createCanvas = function(){
  23. // create the qrcode itself
  24. var qrcode = new QRCode(options.typeNumber, options.correctLevel);
  25. qrcode.addData(options.text);
  26. qrcode.make();
  27. // create canvas element
  28. var canvas = document.createElement('canvas');
  29. canvas.width = options.width;
  30. canvas.height = options.height;
  31. var ctx = canvas.getContext('2d');
  32. // compute tileW/tileH based on options.width/options.height
  33. var tileW = options.width / qrcode.getModuleCount();
  34. var tileH = options.height / qrcode.getModuleCount();
  35. // draw in the canvas
  36. for( var row = 0; row < qrcode.getModuleCount(); row++ ){
  37. for( var col = 0; col < qrcode.getModuleCount(); col++ ){
  38. ctx.fillStyle = qrcode.isDark(row, col) ? options.foreground : options.background;
  39. var w = (Math.ceil((col+1)*tileW) - Math.floor(col*tileW));
  40. var h = (Math.ceil((row+1)*tileH) - Math.floor(row*tileH));
  41. ctx.fillRect(Math.round(col*tileW),Math.round(row*tileH), w, h);
  42. }
  43. }
  44. // return just built canvas
  45. return canvas;
  46. }
  47. // from Jon-Carlos Rivera (https://github.com/imbcmdth)
  48. var createTable = function(){
  49. // create the qrcode itself
  50. var qrcode = new QRCode(options.typeNumber, options.correctLevel);
  51. qrcode.addData(options.text);
  52. qrcode.make();
  53. // create table element
  54. var $table = $('<table></table>')
  55. .css("width", options.width+"px")
  56. .css("height", options.height+"px")
  57. .css("border", "0px")
  58. .css("border-collapse", "collapse")
  59. .css('background-color', options.background);
  60. // compute tileS percentage
  61. var tileW = options.width / qrcode.getModuleCount();
  62. var tileH = options.height / qrcode.getModuleCount();
  63. // draw in the table
  64. for(var row = 0; row < qrcode.getModuleCount(); row++ ){
  65. var $row = $('<tr></tr>').css('height', tileH+"px").appendTo($table);
  66. for(var col = 0; col < qrcode.getModuleCount(); col++ ){
  67. $('<td></td>')
  68. .css('width', tileW+"px")
  69. .css('background-color', qrcode.isDark(row, col) ? options.foreground : options.background)
  70. .appendTo($row);
  71. }
  72. }
  73. // return just built canvas
  74. return $table;
  75. }
  76. return this.each(function(){
  77. var element = options.render == "canvas" ? createCanvas() : createTable();
  78. $(element).appendTo(this);
  79. });
  80. };
  81. })( jQuery );