fe-qrcode.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * QR码生成器
  3. */
  4. baidu.qrcode = (function () {
  5. "use strict";
  6. /**
  7. * 二维码上携带的文字
  8. * @type {String}
  9. */
  10. var text = '';
  11. /**
  12. * 创建二维码生成的参数
  13. * @private
  14. */
  15. var _createOptions = function () {
  16. var width = $("#opt_width").val();
  17. var foreground = $('#opt_fc').val();
  18. var defaultOptions = {
  19. // render method: 'canvas', 'image' or 'div'
  20. render: 'canvas',
  21. // version range somewhere in 1 .. 40
  22. minVersion: 1,
  23. maxVersion: 40,
  24. // error correction level: 'L', 'M', 'Q' or 'H'
  25. ecLevel: 'L',
  26. // offset in pixel if drawn onto existing canvas
  27. left: 0,
  28. top: 0,
  29. // size in pixel
  30. size: +width || 200,
  31. // code color or image element
  32. fill: foreground,
  33. // background color or image element, null for transparent background
  34. background: '#fff',
  35. // corner radius relative to module width: 0.0 .. 0.5
  36. radius: 0,
  37. // quiet zone in modules
  38. quiet: 0,
  39. text: text,
  40. // modes
  41. // 0: normal
  42. // 1: label strip
  43. // 2: label box
  44. // 3: image strip
  45. // 4: image box
  46. mode: 0,
  47. mSize: 0.15,
  48. mPosX: 0.5,
  49. mPosY: 0.5,
  50. label: 'FH',
  51. fontname: 'sans',
  52. fontcolor: '#f00',
  53. image: false
  54. };
  55. // 判断是否需要设置icon
  56. var iconType = $('input[name=qr_icon]:checked').val();
  57. if (iconType == 1) {
  58. defaultOptions.mode = 4;
  59. defaultOptions.image = $('#logo_default')[0];
  60. } else if (iconType == 2) {
  61. defaultOptions.mode = 4;
  62. defaultOptions.image = $('#logo')[0];
  63. }
  64. return defaultOptions;
  65. };
  66. /**
  67. * 创建二维码
  68. */
  69. var _createQrCode = function () {
  70. text = $('#codeSource').val();
  71. if (text) {
  72. $('#preview').html('').qrcode(_createOptions());
  73. } else {
  74. $('#preview').html('再在输入框里输入一些内容,就能生成二维码了哦~')
  75. }
  76. $('#fieldset_qr').show();
  77. };
  78. /**
  79. * 设置ICON
  80. * @param file
  81. * @private
  82. */
  83. var _drawIcon = function (file) {
  84. if (/image\//.test(file.type)) {
  85. var reader = new FileReader();
  86. reader.onload = function (evt) {
  87. $('#logo').attr('src', evt.target.result);
  88. _createQrCode();
  89. };
  90. reader.readAsDataURL(file);
  91. } else {
  92. alert('请选择图片文件!');
  93. }
  94. };
  95. /**
  96. * 可以拖拽一张图片上来,生成小icon
  97. * @private
  98. */
  99. var _dragAndDrop = function () {
  100. $(document).bind('drop', function (e) {
  101. e.preventDefault();
  102. e.stopPropagation();
  103. var files = e.originalEvent.dataTransfer.files;
  104. if (files.length) {
  105. _drawIcon(files[0]);
  106. }
  107. }).bind('dragover', function (e) {
  108. e.preventDefault();
  109. e.stopPropagation();
  110. });
  111. };
  112. /**
  113. * 绑定事件
  114. * @private
  115. */
  116. var _bindEvents = function () {
  117. $("#confirm_button").click(function () {
  118. _createQrCode();
  119. });
  120. $("#opt_fc").colorpicker({
  121. fillcolor: true,
  122. success: function (obj, color) {
  123. _createQrCode();
  124. }
  125. });
  126. $('#remove_icon,#default_icon').click(function (e) {
  127. _createQrCode();
  128. });
  129. $('#upload_icon').click(function (e) {
  130. $('#file').trigger('click');
  131. });
  132. $('#file').change(function (e) {
  133. if (this.files.length) {
  134. _drawIcon(this.files[0]);
  135. this.value = '';
  136. }
  137. });
  138. _dragAndDrop();
  139. };
  140. var _init = function () {
  141. // 在tab创建或者更新时候,监听事件,看看是否有参数传递过来
  142. chrome.runtime.onMessage.addListener(function (request, sender, callback) {
  143. if (request.type == MSG_TYPE.TAB_CREATED_OR_UPDATED && request.event == 'qrcode') {
  144. if (request.content) {
  145. document.getElementById('codeSource').value = (request.content);
  146. setTimeout(_createQrCode, 50);
  147. }
  148. }
  149. });
  150. $(function () {
  151. _bindEvents();
  152. $('#codeSource').focus();
  153. });
  154. };
  155. return {
  156. init: _init
  157. };
  158. })();
  159. baidu.qrcode.init();