webar.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * WebAR基础类
  3. * 摄像头设置参数请查看: https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
  4. * 如果打开摄像头后,播放视频有卡顿,请尝试设置 frameRate,height与width
  5. */
  6. class WebAR {
  7. /**
  8. * 初始化Web AR
  9. * @param interval 识别间隔(毫秒)
  10. * @param recognizeUrl 识别服务地址
  11. * @param isDebug 是否输入调试信息
  12. * @param token 非必需,使用token认证识别
  13. */
  14. constructor(interval, recognizeUrl, token) {
  15. this.isRecognizing = false;
  16. // 前/后置摄像头
  17. this.cameras = ["user", "environment"];
  18. this.interval = interval;
  19. this.recognizeUrl = recognizeUrl;
  20. this.token = token;
  21. }
  22. /**
  23. * 列表设备上的所有摄像头
  24. * @param videoDevice
  25. * @returns {Promise<T>}
  26. */
  27. listCamera(videoDevice) {
  28. return new Promise((resolve, reject) => {
  29. navigator.mediaDevices.enumerateDevices()
  30. .then((devices) => {
  31. let index = 0;
  32. devices.find((device) => {
  33. if (device.kind === 'videoinput') {
  34. const option = document.createElement('option');
  35. // 在iOS12.2上deviceId为空
  36. if (device.deviceId == '') {
  37. option.text = device.label || 'camera ' + this.cameras[index];
  38. option.value = JSON.stringify({ audio: false, video: { facingMode: { exact: this.cameras[index] } } });
  39. index++;
  40. }
  41. else {
  42. option.text = device.label || 'camera ' + (videoDevice.length + 1).toString();
  43. option.value = JSON.stringify({ audio: false, video: { deviceId: { exact: device.deviceId } } });
  44. }
  45. // 将摄像头信息存储在select元素中,方便切换前、后置摄像头
  46. videoDevice.appendChild(option);
  47. }
  48. return false;
  49. });
  50. if (videoDevice.length === 0) {
  51. reject('没有可使用的视频设备');
  52. }
  53. else {
  54. this.initVideo();
  55. this.initCanvas();
  56. resolve(true);
  57. }
  58. }).catch(err => {
  59. reject(err);
  60. });
  61. });
  62. }
  63. /**
  64. * 打开摄像头
  65. * 摄像头设置参数请查看: https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
  66. * @param videoDeviceIndex
  67. * @returns {Promise<T>}
  68. */
  69. openCamera(constraints) {
  70. // 如果是切换摄像头,则需要先关闭。
  71. if (this.videoElement && this.videoElement.srcObject) {
  72. this.videoElement.srcObject.getTracks().forEach(track => {
  73. track.stop();
  74. });
  75. }
  76. return new Promise((resolve, reject) => {
  77. navigator.mediaDevices.getUserMedia(constraints)
  78. .then(stream => {
  79. this.videoElement.srcObject = stream;
  80. this.videoElement.style.display = 'block';
  81. this.videoElement.play();
  82. this.videoElement.onloadedmetadata = () => {
  83. const cameraSize = {
  84. width: this.videoElement.offsetWidth,
  85. height: this.videoElement.offsetHeight
  86. };
  87. console.info(JSON.stringify(cameraSize));
  88. if (window.innerWidth < window.innerHeight) {
  89. // 竖屏
  90. if (cameraSize.height < window.innerHeight) {
  91. this.videoElement.setAttribute('height', window.innerHeight.toString() + 'px');
  92. }
  93. }
  94. else {
  95. // 横屏
  96. if (cameraSize.width < window.innerWidth) {
  97. this.videoElement.setAttribute('width', window.innerWidth.toString() + 'px');
  98. }
  99. }
  100. resolve(true);
  101. };
  102. })
  103. .catch(err => {
  104. reject(err);
  105. });
  106. });
  107. }
  108. /**
  109. * 截取摄像头图片
  110. * @returns {string}
  111. */
  112. captureVideo() {
  113. this.canvasContext.drawImage(this.videoElement, 0, 0, this.videoElement.offsetWidth, this.videoElement.offsetHeight);
  114. return this.canvasElement.toDataURL('image/jpeg', 0.5).split('base64,')[1];
  115. }
  116. /**
  117. * 创建视频详情元素,播放摄像头视频流
  118. */
  119. initVideo() {
  120. this.videoElement = document.createElement('video');
  121. this.videoElement.setAttribute('playsinline', 'playsinline');
  122. document.body.appendChild(this.videoElement);
  123. }
  124. /**
  125. * 创建canvas,截取摄像头图片时使用
  126. */
  127. initCanvas() {
  128. this.canvasElement = document.createElement('canvas');
  129. this.canvasElement.setAttribute('width', window.innerWidth.toString() + 'px');
  130. this.canvasElement.setAttribute('height', window.innerHeight.toString() + 'px');
  131. this.canvasContext = this.canvasElement.getContext('2d');
  132. // document.body.appendChild(this.canvasElement);
  133. }
  134. /**
  135. * 识别
  136. * @param callback
  137. */
  138. startRecognize(callback) {
  139. this.timer = window.setInterval(() => {
  140. // 等待上一次识别结果
  141. if (this.isRecognizing) {
  142. return;
  143. }
  144. this.isRecognizing = true;
  145. // 从摄像头中抓取一张图片
  146. const image = { image: this.captureVideo() };
  147. // 发送到服务器识别
  148. this.httpPost(image)
  149. .then((msg) => {
  150. this.stopRecognize();
  151. callback(msg);
  152. })
  153. .catch((err) => {
  154. this.isRecognizing = false;
  155. console.info(err);
  156. });
  157. }, this.interval);
  158. }
  159. /**
  160. * 停止识别
  161. */
  162. stopRecognize() {
  163. if (this.timer) {
  164. window.clearInterval(this.timer);
  165. this.isRecognizing = false;
  166. }
  167. }
  168. httpPost(image) {
  169. return new Promise((resolve, reject) => {
  170. const http = new XMLHttpRequest();
  171. http.onload = () => {
  172. try {
  173. const msg = JSON.parse(http.responseText);
  174. if (http.status === 200) {
  175. if (msg.statusCode === 0) {
  176. resolve(msg.result);
  177. }
  178. else {
  179. reject(msg);
  180. }
  181. }
  182. else {
  183. reject(msg);
  184. }
  185. }
  186. catch (err) {
  187. reject(err);
  188. }
  189. };
  190. http.onerror = (err) => {
  191. reject(err);
  192. };
  193. http.open('POST', this.recognizeUrl);
  194. http.setRequestHeader('Content-Type', 'application/json;Charset=UTF-8');
  195. if (this.token) {
  196. // 将云识别认证token写在请求头中
  197. http.setRequestHeader('Authorization', this.token);
  198. }
  199. http.send(JSON.stringify(image));
  200. });
  201. }
  202. }
  203. //# sourceMappingURL=webar.js.map