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.

186 lines
3.8 KiB

  1. // QRCODE reader Copyright 2011 Lazar Laszlo
  2. // http://www.webqr.com
  3. var gCtx = null;
  4. var gCanvas = null;
  5. var c=0;
  6. var stype=0;
  7. var gUM=false;
  8. var webkit=false;
  9. var moz=false;
  10. var v=null;
  11. var qrSize = 0;
  12. var margin = 0;
  13. var scannedCallback = null;
  14. var vidhtml = '<video id="video-stream" autoplay></video>';
  15. function handleFiles(f)
  16. {
  17. var o=[];
  18. for(var i =0;i<f.length;i++)
  19. {
  20. var reader = new FileReader();
  21. reader.onload = (function(theFile) {
  22. return function(e) {
  23. gCtx.clearRect(0, 0, gCanvas.width, gCanvas.height);
  24. qrcode.decode(e.target.result);
  25. };
  26. })(f[i]);
  27. reader.readAsDataURL(f[i]);
  28. }
  29. }
  30. function initCanvas(w,h)
  31. {
  32. gCanvas = document.getElementById("qr-canvas");
  33. gCanvas.style.width = w + "px";
  34. gCanvas.style.height = h + "px";
  35. gCanvas.width = w;
  36. gCanvas.height = h;
  37. gCtx = gCanvas.getContext("2d");
  38. gCtx.clearRect(0, 0, w, h);
  39. }
  40. function captureToCanvas() {
  41. if(stype!=1)
  42. return;
  43. if(gUM)
  44. {
  45. try{
  46. gCtx.drawImage(v,0,0);
  47. try{
  48. qrcode.decode();
  49. }
  50. catch(e){
  51. console.log(e);
  52. setTimeout(captureToCanvas, 500);
  53. };
  54. }
  55. catch(e){
  56. console.log(e);
  57. setTimeout(captureToCanvas, 500);
  58. };
  59. }
  60. }
  61. function htmlEntities(str) {
  62. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
  63. }
  64. function read(a)
  65. {
  66. if (scannedCallback) scannedCallback(a);
  67. }
  68. function isCanvasSupported(){
  69. var elem = document.createElement('canvas');
  70. return !!(elem.getContext && elem.getContext('2d'));
  71. }
  72. function success(stream)
  73. {
  74. v.srcObject = stream;
  75. v.play();
  76. gUM=true;
  77. setTimeout(captureToCanvas, 500);
  78. }
  79. function error(error) {
  80. gUM=false;
  81. return;
  82. }
  83. function load(initSize, initMargin, callback)
  84. {
  85. qrSize = initSize;
  86. margin = initMargin;
  87. scannedCallback = callback;
  88. if(isCanvasSupported() && window.File && window.FileReader)
  89. {
  90. initCanvas(800, 600);
  91. qrcode.callback = read;
  92. setwebcam();
  93. }
  94. else
  95. {
  96. console.log("Sorry your browser is not supported");
  97. }
  98. }
  99. function setwebcam()
  100. {
  101. var options = true;
  102. if(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices)
  103. {
  104. try{
  105. navigator.mediaDevices.enumerateDevices()
  106. .then(function(devices) {
  107. devices.forEach(function(device) {
  108. if (device.kind === 'videoinput') {
  109. if(device.label.toLowerCase().search("back") >-1)
  110. options={'deviceId': {'exact':device.deviceId}, 'facingMode':'environment'} ;
  111. }
  112. console.log(device.kind + ": " + device.label +" id = " + device.deviceId);
  113. });
  114. setwebcam2(options);
  115. });
  116. }
  117. catch(e)
  118. {
  119. console.log(e);
  120. }
  121. }
  122. else{
  123. console.log("no navigator.mediaDevices.enumerateDevices" );
  124. setwebcam2(options);
  125. }
  126. }
  127. function setwebcam2(options)
  128. {
  129. console.log(options);
  130. if(stype==1)
  131. {
  132. setTimeout(captureToCanvas, 500);
  133. return;
  134. }
  135. var n=navigator;
  136. document.getElementById("outdiv").innerHTML = vidhtml;
  137. v=document.getElementById("video-stream");
  138. v.style.width = qrSize + "px";
  139. v.style.marginLeft = margin + "px";
  140. if(n.mediaDevices.getUserMedia)
  141. {
  142. n.mediaDevices.getUserMedia({video: options, audio: false}).
  143. then(function(stream){
  144. success(stream);
  145. }).catch(function(error){
  146. error(error)
  147. });
  148. }
  149. else
  150. if(n.getUserMedia)
  151. {
  152. webkit=true;
  153. n.getUserMedia({video: options, audio: false}, success, error);
  154. }
  155. else
  156. if(n.webkitGetUserMedia)
  157. {
  158. webkit=true;
  159. n.webkitGetUserMedia({video:options, audio: false}, success, error);
  160. }
  161. stype=1;
  162. setTimeout(captureToCanvas, 500);
  163. }