qrcode.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Copyright 2011 Lazar Laszlo (lazarsoft@gmail.com, www.lazarsoft.info)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import Detector from './detector';
  14. import Decoder from './decoder';
  15. export var qrcode = {};
  16. qrcode.sizeOfDataLengthInfo = [[10, 9, 8, 8], [12, 11, 16, 10], [14, 13, 16, 12]];
  17. export default function QrCode() {
  18. this.imagedata = null;
  19. this.width = 0;
  20. this.height = 0;
  21. this.qrCodeSymbol = null;
  22. this.debug = false;
  23. this.callback = null;
  24. }
  25. QrCode.prototype.decode = function(src, data) {
  26. var decode = (function() {
  27. try {
  28. this.error = undefined;
  29. this.result = this.process(this.imagedata);
  30. } catch (e) {
  31. this.error = e;
  32. this.result = undefined;
  33. }
  34. if (this.callback != null) {
  35. this.callback(this.error, this.result);
  36. }
  37. return this.result;
  38. }).bind(this);
  39. if (src != undefined && src.width != undefined) {
  40. /* decode from canvas canvas.context.getImageData */
  41. this.width = src.width;
  42. this.height = src.height;
  43. this.imagedata = {"data": data || src.data};
  44. this.imagedata.width = src.width;
  45. this.imagedata.height = src.height;
  46. decode();
  47. } else {
  48. if (typeof Image === "undefined") {
  49. throw new Error("This source format is not supported in your environment, you need to pass an image buffer with width and height (see https://github.com/edi9999/jsqrcode/blob/master/test/qrcode.js)");
  50. }
  51. /* decode from URL */
  52. var image = new Image();
  53. image.crossOrigin = "Anonymous";
  54. image.onload = (function() {
  55. var canvas_qr = document.createElement('canvas');
  56. var context = canvas_qr.getContext('2d');
  57. var canvas_out = document.getElementById("out-canvas");
  58. if (canvas_out != null) {
  59. var outctx = canvas_out.getContext('2d');
  60. outctx.clearRect(0, 0, 320, 240);
  61. outctx.drawImage(image, 0, 0, 320, 240);
  62. }
  63. canvas_qr.width = image.width;
  64. canvas_qr.height = image.height;
  65. context.drawImage(image, 0, 0);
  66. this.width = image.width;
  67. this.height = image.height;
  68. try {
  69. this.imagedata = context.getImageData(0, 0, image.width, image.height);
  70. } catch (e) {
  71. this.result = "Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!";
  72. if (this.callback != null) return this.callback(null, this.result);
  73. }
  74. decode();
  75. }).bind(this);
  76. image.src = src;
  77. }
  78. };
  79. QrCode.prototype.decode_utf8 = function(s) {
  80. return decodeURIComponent(escape(s));
  81. };
  82. QrCode.prototype.process = function(imageData) {
  83. var start = new Date().getTime();
  84. var image = this.grayScaleToBitmap(this.grayscale(imageData));
  85. var detector = new Detector(image);
  86. var qRCodeMatrix = detector.detect();
  87. /*for (var y = 0; y < qRCodeMatrix.bits.height; y++)
  88. {
  89. for (var x = 0; x < qRCodeMatrix.bits.width; x++)
  90. {
  91. var point = (x * 4*2) + (y*2 * imageData.width * 4);
  92. imageData.data[point] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  93. imageData.data[point+1] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  94. imageData.data[point+2] = qRCodeMatrix.bits.get_Renamed(x,y)?255:0;
  95. }
  96. }*/
  97. var reader = Decoder.decode(qRCodeMatrix.bits);
  98. var data = reader.DataByte;
  99. var str = "";
  100. for (var i = 0; i < data.length; i++) {
  101. for (var j = 0; j < data[i].length; j++)
  102. str += String.fromCharCode(data[i][j]);
  103. }
  104. var end = new Date().getTime();
  105. var time = end - start;
  106. if (this.debug) {
  107. console.log('QR Code processing time (ms): ' + time);
  108. }
  109. return {result: this.decode_utf8(str), points: qRCodeMatrix.points};
  110. };
  111. QrCode.prototype.getPixel = function(imageData, x, y) {
  112. if (imageData.width < x) {
  113. throw "point error";
  114. }
  115. if (imageData.height < y) {
  116. throw "point error";
  117. }
  118. var point = (x * 4) + (y * imageData.width * 4);
  119. return (imageData.data[point] * 33 + imageData.data[point + 1] * 34 + imageData.data[point + 2] * 33) / 100;
  120. };
  121. QrCode.prototype.binarize = function(th) {
  122. var ret = new Array(this.width * this.height);
  123. for (var y = 0; y < this.height; y++) {
  124. for (var x = 0; x < this.width; x++) {
  125. var gray = this.getPixel(x, y);
  126. ret[x + y * this.width] = gray <= th;
  127. }
  128. }
  129. return ret;
  130. };
  131. QrCode.prototype.getMiddleBrightnessPerArea = function(imageData) {
  132. var numSqrtArea = 4;
  133. //obtain middle brightness((min + max) / 2) per area
  134. var areaWidth = Math.floor(imageData.width / numSqrtArea);
  135. var areaHeight = Math.floor(imageData.height / numSqrtArea);
  136. var minmax = new Array(numSqrtArea);
  137. for (var i = 0; i < numSqrtArea; i++) {
  138. minmax[i] = new Array(numSqrtArea);
  139. for (var i2 = 0; i2 < numSqrtArea; i2++) {
  140. minmax[i][i2] = [0, 0];
  141. }
  142. }
  143. for (var ay = 0; ay < numSqrtArea; ay++) {
  144. for (var ax = 0; ax < numSqrtArea; ax++) {
  145. minmax[ax][ay][0] = 0xFF;
  146. for (var dy = 0; dy < areaHeight; dy++) {
  147. for (var dx = 0; dx < areaWidth; dx++) {
  148. var target = imageData.data[areaWidth * ax + dx + (areaHeight * ay + dy) * imageData.width];
  149. if (target < minmax[ax][ay][0])
  150. minmax[ax][ay][0] = target;
  151. if (target > minmax[ax][ay][1])
  152. minmax[ax][ay][1] = target;
  153. }
  154. }
  155. }
  156. }
  157. var middle = new Array(numSqrtArea);
  158. for (var i3 = 0; i3 < numSqrtArea; i3++) {
  159. middle[i3] = new Array(numSqrtArea);
  160. }
  161. for (var ay = 0; ay < numSqrtArea; ay++) {
  162. for (var ax = 0; ax < numSqrtArea; ax++) {
  163. middle[ax][ay] = Math.floor((minmax[ax][ay][0] + minmax[ax][ay][1]) / 2);
  164. }
  165. }
  166. return middle;
  167. };
  168. QrCode.prototype.grayScaleToBitmap = function(grayScaleImageData) {
  169. var middle = this.getMiddleBrightnessPerArea(grayScaleImageData);
  170. var sqrtNumArea = middle.length;
  171. var areaWidth = Math.floor(grayScaleImageData.width / sqrtNumArea);
  172. var areaHeight = Math.floor(grayScaleImageData.height / sqrtNumArea);
  173. for (var ay = 0; ay < sqrtNumArea; ay++) {
  174. for (var ax = 0; ax < sqrtNumArea; ax++) {
  175. for (var dy = 0; dy < areaHeight; dy++) {
  176. for (var dx = 0; dx < areaWidth; dx++) {
  177. grayScaleImageData.data[areaWidth * ax + dx + (areaHeight * ay + dy) * grayScaleImageData.width] = (grayScaleImageData.data[areaWidth * ax + dx + (areaHeight * ay + dy) * grayScaleImageData.width] < middle[ax][ay]);
  178. }
  179. }
  180. }
  181. }
  182. return grayScaleImageData;
  183. };
  184. QrCode.prototype.grayscale = function(imageData) {
  185. var ret = new Array(imageData.width * imageData.height);
  186. for (var y = 0; y < imageData.height; y++) {
  187. for (var x = 0; x < imageData.width; x++) {
  188. var gray = this.getPixel(imageData, x, y);
  189. ret[x + y * imageData.width] = gray;
  190. }
  191. }
  192. return {
  193. height: imageData.height,
  194. width: imageData.width,
  195. data: ret
  196. };
  197. };
  198. export function URShift(number, bits) {
  199. if (number >= 0)
  200. return number >> bits;
  201. else
  202. return (number >> bits) + (2 << ~bits);
  203. }