not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
13 KiB

  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. var qrcode = {};
  14. qrcode.imagedata = null;
  15. qrcode.width = 0;
  16. qrcode.height = 0;
  17. qrcode.qrCodeSymbol = null;
  18. qrcode.debug = false;
  19. qrcode.maxImgSize = 1024*1024;
  20. qrcode.sizeOfDataLengthInfo = [ [ 10, 9, 8, 8 ], [ 12, 11, 16, 10 ], [ 14, 13, 16, 12 ] ];
  21. qrcode.callback = null;
  22. qrcode.vidSuccess = function (stream)
  23. {
  24. qrcode.localstream = stream;
  25. if(qrcode.webkit)
  26. qrcode.video.src = window.webkitURL.createObjectURL(stream);
  27. else
  28. if(qrcode.moz)
  29. {
  30. qrcode.video.mozSrcObject = stream;
  31. qrcode.video.play();
  32. }
  33. else
  34. qrcode.video.src = stream;
  35. qrcode.gUM=true;
  36. qrcode.canvas_qr2 = document.createElement('canvas');
  37. qrcode.canvas_qr2.id = "qr-canvas";
  38. qrcode.qrcontext2 = qrcode.canvas_qr2.getContext('2d');
  39. qrcode.canvas_qr2.width = qrcode.video.videoWidth;
  40. qrcode.canvas_qr2.height = qrcode.video.videoHeight;
  41. setTimeout(qrcode.captureToCanvas, 500);
  42. }
  43. qrcode.vidError = function(error)
  44. {
  45. qrcode.gUM=false;
  46. return;
  47. }
  48. qrcode.captureToCanvas = function()
  49. {
  50. if(qrcode.gUM)
  51. {
  52. try{
  53. if(qrcode.video.videoWidth == 0)
  54. {
  55. setTimeout(qrcode.captureToCanvas, 500);
  56. return;
  57. }
  58. else
  59. {
  60. qrcode.canvas_qr2.width = qrcode.video.videoWidth;
  61. qrcode.canvas_qr2.height = qrcode.video.videoHeight;
  62. }
  63. qrcode.qrcontext2.drawImage(qrcode.video,0,0);
  64. try{
  65. qrcode.decode();
  66. }
  67. catch(e){
  68. console.log(e);
  69. setTimeout(qrcode.captureToCanvas, 500);
  70. };
  71. }
  72. catch(e){
  73. console.log(e);
  74. setTimeout(qrcode.captureToCanvas, 500);
  75. };
  76. }
  77. }
  78. qrcode.setWebcam = function(videoId)
  79. {
  80. var n=navigator;
  81. qrcode.video=document.getElementById(videoId);
  82. var options = true;
  83. if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices)
  84. {
  85. try{
  86. navigator.mediaDevices.enumerateDevices()
  87. .then(function(devices) {
  88. devices.forEach(function(device) {
  89. console.log("deb1");
  90. if (device.kind === 'videoinput') {
  91. if(device.label.toLowerCase().search("back") >-1)
  92. options=[{'sourceId': device.deviceId}] ;
  93. }
  94. console.log(device.kind + ": " + device.label +
  95. " id = " + device.deviceId);
  96. });
  97. })
  98. }
  99. catch(e)
  100. {
  101. console.log(e);
  102. }
  103. }
  104. else{
  105. console.log("no navigator.mediaDevices.enumerateDevices" );
  106. }
  107. if(n.getUserMedia)
  108. n.getUserMedia({video: options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  109. else
  110. if(n.webkitGetUserMedia)
  111. {
  112. qrcode.webkit=true;
  113. n.webkitGetUserMedia({video:options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  114. }
  115. else
  116. if(n.mozGetUserMedia)
  117. {
  118. qrcode.moz=true;
  119. n.mozGetUserMedia({video: options, audio: false}, qrcode.vidSuccess, qrcode.vidError);
  120. }
  121. }
  122. qrcode.decode = function(src){
  123. if(arguments.length==0)
  124. {
  125. if(qrcode.canvas_qr2)
  126. {
  127. var canvas_qr = qrcode.canvas_qr2;
  128. var context = qrcode.qrcontext2;
  129. }
  130. else
  131. {
  132. var canvas_qr = document.getElementById("qr-canvas");
  133. var context = canvas_qr.getContext('2d');
  134. }
  135. qrcode.width = canvas_qr.width;
  136. qrcode.height = canvas_qr.height;
  137. qrcode.imagedata = context.getImageData(0, 0, qrcode.width, qrcode.height);
  138. qrcode.result = qrcode.process(context);
  139. if(qrcode.callback!=null)
  140. qrcode.callback(qrcode.result);
  141. return qrcode.result;
  142. }
  143. else
  144. {
  145. var image = new Image();
  146. image.crossOrigin = "Anonymous";
  147. image.onload=function(){
  148. //var canvas_qr = document.getElementById("qr-canvas");
  149. var canvas_out = document.getElementById("out-canvas");
  150. if(canvas_out!=null)
  151. {
  152. var outctx = canvas_out.getContext('2d');
  153. outctx.clearRect(0, 0, 320, 240);
  154. outctx.drawImage(image, 0, 0, 320, 240);
  155. }
  156. var canvas_qr = document.createElement('canvas');
  157. var context = canvas_qr.getContext('2d');
  158. var nheight = image.height;
  159. var nwidth = image.width;
  160. if(image.width*image.height>qrcode.maxImgSize)
  161. {
  162. var ir = image.width / image.height;
  163. nheight = Math.sqrt(qrcode.maxImgSize/ir);
  164. nwidth=ir*nheight;
  165. }
  166. canvas_qr.width = nwidth;
  167. canvas_qr.height = nheight;
  168. context.drawImage(image, 0, 0, canvas_qr.width, canvas_qr.height );
  169. qrcode.width = canvas_qr.width;
  170. qrcode.height = canvas_qr.height;
  171. try{
  172. qrcode.imagedata = context.getImageData(0, 0, canvas_qr.width, canvas_qr.height);
  173. }catch(e){
  174. qrcode.result = "Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!";
  175. if(qrcode.callback!=null)
  176. qrcode.callback(qrcode.result);
  177. return;
  178. }
  179. try
  180. {
  181. qrcode.result = qrcode.process(context);
  182. }
  183. catch(e)
  184. {
  185. console.log(e);
  186. qrcode.result = "error decoding QR Code";
  187. }
  188. if(qrcode.callback!=null)
  189. qrcode.callback(qrcode.result);
  190. }
  191. image.onerror = function ()
  192. {
  193. if(qrcode.callback!=null)
  194. qrcode.callback("Failed to load the image");
  195. }
  196. image.src = src;
  197. }
  198. }
  199. qrcode.isUrl = function(s)
  200. {
  201. var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  202. return regexp.test(s);
  203. }
  204. qrcode.decode_url = function (s)
  205. {
  206. var escaped = "";
  207. try{
  208. escaped = escape( s );
  209. }
  210. catch(e)
  211. {
  212. console.log(e);
  213. escaped = s;
  214. }
  215. var ret = "";
  216. try{
  217. ret = decodeURIComponent( escaped );
  218. }
  219. catch(e)
  220. {
  221. console.log(e);
  222. ret = escaped;
  223. }
  224. return ret;
  225. }
  226. qrcode.decode_utf8 = function ( s )
  227. {
  228. if(qrcode.isUrl(s))
  229. return qrcode.decode_url(s);
  230. else
  231. return s;
  232. }
  233. qrcode.process = function(ctx){
  234. var start = new Date().getTime();
  235. var image = qrcode.grayScaleToBitmap(qrcode.grayscale());
  236. //var image = qrcode.binarize(128);
  237. if(qrcode.debug)
  238. {
  239. for (var y = 0; y < qrcode.height; y++)
  240. {
  241. for (var x = 0; x < qrcode.width; x++)
  242. {
  243. var point = (x * 4) + (y * qrcode.width * 4);
  244. qrcode.imagedata.data[point] = image[x+y*qrcode.width]?0:0;
  245. qrcode.imagedata.data[point+1] = image[x+y*qrcode.width]?0:0;
  246. qrcode.imagedata.data[point+2] = image[x+y*qrcode.width]?255:0;
  247. }
  248. }
  249. ctx.putImageData(qrcode.imagedata, 0, 0);
  250. }
  251. //var finderPatternInfo = new FinderPatternFinder().findFinderPattern(image);
  252. var detector = new Detector(image);
  253. var qRCodeMatrix = detector.detect();
  254. if(qrcode.debug)
  255. {
  256. for (var y = 0; y < qRCodeMatrix.bits.Height; y++)
  257. {
  258. for (var x = 0; x < qRCodeMatrix.bits.Width; x++)
  259. {
  260. var point = (x * 4*2) + (y*2 * qrcode.width * 4);
  261. qrcode.imagedata.data[point] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  262. qrcode.imagedata.data[point+1] = qRCodeMatrix.bits.get_Renamed(x,y)?0:0;
  263. qrcode.imagedata.data[point+2] = qRCodeMatrix.bits.get_Renamed(x,y)?255:0;
  264. }
  265. }
  266. ctx.putImageData(qrcode.imagedata, 0, 0);
  267. }
  268. var reader = Decoder.decode(qRCodeMatrix.bits);
  269. var data = reader.DataByte;
  270. var str="";
  271. for(var i=0;i<data.length;i++)
  272. {
  273. for(var j=0;j<data[i].length;j++)
  274. str+=String.fromCharCode(data[i][j]);
  275. }
  276. var end = new Date().getTime();
  277. var time = end - start;
  278. console.log(time);
  279. return qrcode.decode_utf8(str);
  280. //alert("Time:" + time + " Code: "+str);
  281. }
  282. qrcode.getPixel = function(x,y){
  283. if (qrcode.width < x) {
  284. throw "point error";
  285. }
  286. if (qrcode.height < y) {
  287. throw "point error";
  288. }
  289. var point = (x * 4) + (y * qrcode.width * 4);
  290. var p = (qrcode.imagedata.data[point]*33 + qrcode.imagedata.data[point + 1]*34 + qrcode.imagedata.data[point + 2]*33)/100;
  291. return p;
  292. }
  293. qrcode.binarize = function(th){
  294. var ret = new Array(qrcode.width*qrcode.height);
  295. for (var y = 0; y < qrcode.height; y++)
  296. {
  297. for (var x = 0; x < qrcode.width; x++)
  298. {
  299. var gray = qrcode.getPixel(x, y);
  300. ret[x+y*qrcode.width] = gray<=th?true:false;
  301. }
  302. }
  303. return ret;
  304. }
  305. qrcode.getMiddleBrightnessPerArea=function(image)
  306. {
  307. var numSqrtArea = 4;
  308. //obtain middle brightness((min + max) / 2) per area
  309. var areaWidth = Math.floor(qrcode.width / numSqrtArea);
  310. var areaHeight = Math.floor(qrcode.height / numSqrtArea);
  311. var minmax = new Array(numSqrtArea);
  312. for (var i = 0; i < numSqrtArea; i++)
  313. {
  314. minmax[i] = new Array(numSqrtArea);
  315. for (var i2 = 0; i2 < numSqrtArea; i2++)
  316. {
  317. minmax[i][i2] = new Array(0,0);
  318. }
  319. }
  320. for (var ay = 0; ay < numSqrtArea; ay++)
  321. {
  322. for (var ax = 0; ax < numSqrtArea; ax++)
  323. {
  324. minmax[ax][ay][0] = 0xFF;
  325. for (var dy = 0; dy < areaHeight; dy++)
  326. {
  327. for (var dx = 0; dx < areaWidth; dx++)
  328. {
  329. var target = image[areaWidth * ax + dx+(areaHeight * ay + dy)*qrcode.width];
  330. if (target < minmax[ax][ay][0])
  331. minmax[ax][ay][0] = target;
  332. if (target > minmax[ax][ay][1])
  333. minmax[ax][ay][1] = target;
  334. }
  335. }
  336. //minmax[ax][ay][0] = (minmax[ax][ay][0] + minmax[ax][ay][1]) / 2;
  337. }
  338. }
  339. var middle = new Array(numSqrtArea);
  340. for (var i3 = 0; i3 < numSqrtArea; i3++)
  341. {
  342. middle[i3] = new Array(numSqrtArea);
  343. }
  344. for (var ay = 0; ay < numSqrtArea; ay++)
  345. {
  346. for (var ax = 0; ax < numSqrtArea; ax++)
  347. {
  348. middle[ax][ay] = Math.floor((minmax[ax][ay][0] + minmax[ax][ay][1]) / 2);
  349. //Console.out.print(middle[ax][ay] + ",");
  350. }
  351. //Console.out.println("");
  352. }
  353. //Console.out.println("");
  354. return middle;
  355. }
  356. qrcode.grayScaleToBitmap=function(grayScale)
  357. {
  358. var middle = qrcode.getMiddleBrightnessPerArea(grayScale);
  359. var sqrtNumArea = middle.length;
  360. var areaWidth = Math.floor(qrcode.width / sqrtNumArea);
  361. var areaHeight = Math.floor(qrcode.height / sqrtNumArea);
  362. var buff = new ArrayBuffer(qrcode.width*qrcode.height);
  363. var bitmap = new Uint8Array(buff);
  364. //var bitmap = new Array(qrcode.height*qrcode.width);
  365. for (var ay = 0; ay < sqrtNumArea; ay++)
  366. {
  367. for (var ax = 0; ax < sqrtNumArea; ax++)
  368. {
  369. for (var dy = 0; dy < areaHeight; dy++)
  370. {
  371. for (var dx = 0; dx < areaWidth; dx++)
  372. {
  373. bitmap[areaWidth * ax + dx+ (areaHeight * ay + dy)*qrcode.width] = (grayScale[areaWidth * ax + dx+ (areaHeight * ay + dy)*qrcode.width] < middle[ax][ay])?true:false;
  374. }
  375. }
  376. }
  377. }
  378. return bitmap;
  379. }
  380. qrcode.grayscale = function()
  381. {
  382. var buff = new ArrayBuffer(qrcode.width*qrcode.height);
  383. var ret = new Uint8Array(buff);
  384. //var ret = new Array(qrcode.width*qrcode.height);
  385. for (var y = 0; y < qrcode.height; y++)
  386. {
  387. for (var x = 0; x < qrcode.width; x++)
  388. {
  389. var gray = qrcode.getPixel(x, y);
  390. ret[x+y*qrcode.width] = gray;
  391. }
  392. }
  393. return ret;
  394. }
  395. function URShift( number, bits)
  396. {
  397. if (number >= 0)
  398. return number >> bits;
  399. else
  400. return (number >> bits) + (2 << ~bits);
  401. }