Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. function (win, doc) {
  2. function QRScan(div_id) {
  3. this.div_id = div_id;
  4. this.div_can = null;
  5. this.videos = [];
  6. this.medioConfig = {};
  7. this.can_open = false;
  8. this.init();
  9. }
  10. QRScan.prototype = {
  11. init: function () {
  12. win.URL = (win.URL || win.webkitURL || win.mozURL || win.msURL);
  13. var promisifiedOldGUM = function(constraints) {
  14. var getUserMedia = (navigator.getUserMedia ||
  15. navigator.webkitGetUserMedia || navigator.mozGetUserMedia);
  16. if (!getUserMedia) {
  17. return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  18. }
  19. return new Promise(function (resolve, reject) {
  20. getUserMedia.call(navigator, constraints, resolve, reject);
  21. });
  22. };
  23. if(navigator.mediaDevices === undefined) {
  24. navigator.mediaDevices = {};
  25. }
  26. if(navigator.mediaDevices.getUserMedia === undefined) {
  27. navigator.mediaDevices.getUserMedia = promisifiedOldGUM;
  28. }
  29. var self = this;
  30. self.div_can = doc.getElementById(self.div_id);
  31. navigator.mediaDevices.enumerateDevices().then(function(devices) {
  32. devices.forEach(function (dv) {
  33. var kind = dv.kind;
  34. if (kind.match(/^video.*/)) {
  35. self.videos.push(dv.deviceId);
  36. console.log(dv);
  37. }
  38. });
  39. var len = self.videos.length;
  40. self.can_open = true;
  41. self.medioConfig = {
  42. audio: false,
  43. video: { deviceId: self.videos[len - 1] }
  44. }
  45. });
  46. },
  47. openScan: function () {
  48. var self = this;
  49. if (self.can_open) {
  50. var vd = doc.createElement('video');
  51. vd.setAttribute('id', 'video_id');
  52. navigator.mediaDevices.getUserMedia(self.medioConfig).then(function (stream) {
  53. vd.src = win.URL.createObjectURL(stream);
  54. self.div_can.appendChild(vd);
  55. }).catch(function (err) {
  56. var p = doc.createElement('p');
  57. p.innerHTML = 'ERROR: ' + err.name +
  58. '<br>该浏览器不支持调用摄像头,请使用夸克浏览器';
  59. self.div_can.appendChild(p);
  60. });
  61. //vd.play();
  62. }
  63. },
  64. closeScan: function () {
  65. this.div_can.innerHTML = '';
  66. },
  67. // 截图上传
  68. getImgDecode: function (func) {
  69. var self = this;
  70. var video = doc.getElementById('video_id');
  71. var canvas = doc.createElement('canvas');
  72. canvas.width = 340;
  73. canvas.height = 305;
  74. var ctx = canvas.getContext('2d');
  75. ctx.drawImage(video, 0, 0, 340, 305);
  76. var scan=this;
  77. blob2text(self.Base64ToBlob(canvas.toDataURL())).then(
  78. function(code){
  79. alert(code.data);
  80. this.closeScan();
  81. }
  82. );
  83. // alert(qrcode.decode(,1024));
  84. // if (canvas.toBlob === undefined) {
  85. // var base64 = canvas.toDataURL();
  86. // var blob = self.Base64ToBlob(base64);
  87. // self.sendBlob(blob, func);
  88. // } else {
  89. // canvas.toBlob(function (blob) {
  90. // self.sendBlob(blob, func);
  91. // });
  92. // }
  93. },
  94. Base64ToBlob: function (base64) {
  95. var code = win.atob(base64.split(',')[1]);
  96. var len = code.length;
  97. var as = new Uint8Array(len);
  98. for (var i = 0; i < len; i++) {
  99. as[i] = code.charCodeAt(i);
  100. }
  101. return new Blob([as], {type: 'image/png'});
  102. }
  103. }
  104. win.QRScan = QRScan;
  105. }(window, document));
  106. function blob2text(blob) {
  107. return new Promise((resolve, reject) => {
  108. const myReader = new FileReader();
  109. myReader.readAsArrayBuffer(blob);
  110. myReader.addEventListener('loadend', e => {
  111. const buffer = e.srcElement.result; // arraybuffer object
  112. const img = UPNG.decode(buffer); // put ArrayBuffer of the PNG file into UPNG.decode
  113. const rgba = UPNG.toRGBA8(img)[0]; // UPNG.toRGBA8 returns array of frames, size: width * height * 4 bytes.
  114. const code = jsQR(new Uint8ClampedArray(rgba), img.width, img.height);
  115. if (code) {
  116. resolve(code);
  117. } else {
  118. reject(new Error('decode failed'));
  119. }
  120. });
  121. });
  122. }