webar.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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
  30. .enumerateDevices()
  31. .then(devices => {
  32. devices.forEach(function(device) {
  33. if (device.kind === "videoinput") {
  34. console.log(
  35. device.kind +
  36. ": " +
  37. device.label +
  38. " id = " +
  39. device.deviceId
  40. );
  41. const option = document.createElement("option");
  42. option.text = device.label;
  43. option.value = JSON.stringify({
  44. audio: false,
  45. video: {
  46. deviceId: { exact: device.deviceId }
  47. }
  48. });
  49. videoDevice.appendChild(option);
  50. }
  51. });
  52. if (videoDevice.length === 0) {
  53. reject("没有可使用的视频设备");
  54. } else {
  55. this.initVideo();
  56. this.initCanvas();
  57. resolve(true);
  58. }
  59. })
  60. .catch(err => {
  61. reject(err);
  62. });
  63. });
  64. }
  65. /**
  66. * 打开摄像头
  67. * 摄像头设置参数请查看: https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints
  68. * @param videoDeviceIndex
  69. * @returns {Promise<T>}
  70. */
  71. openCamera(constraints) {
  72. // 如果是切换摄像头,则需要先关闭。
  73. if (this.videoElement && this.videoElement.srcObject) {
  74. this.videoElement.srcObject.getTracks().forEach(track => {
  75. track.stop();
  76. });
  77. }
  78. return new Promise((resolve, reject) => {
  79. navigator.mediaDevices
  80. .getUserMedia(constraints)
  81. .then(stream => {
  82. this.videoElement.srcObject = stream;
  83. this.videoElement.style.display = "block";
  84. this.videoElement.play();
  85. this.videoElement.onloadedmetadata = () => {
  86. const cameraSize = {
  87. width: this.videoElement.offsetWidth,
  88. height: this.videoElement.offsetHeight
  89. };
  90. console.info(JSON.stringify(cameraSize));
  91. if (window.innerWidth < window.innerHeight) {
  92. // 竖屏
  93. if (cameraSize.height < window.innerHeight) {
  94. this.videoElement.setAttribute(
  95. "height",
  96. window.innerHeight.toString() + "px"
  97. );
  98. }
  99. } else {
  100. // 横屏
  101. if (cameraSize.width < window.innerWidth) {
  102. this.videoElement.setAttribute(
  103. "width",
  104. window.innerWidth.toString() + "px"
  105. );
  106. }
  107. }
  108. resolve(true);
  109. };
  110. })
  111. .catch(err => {
  112. reject(err);
  113. });
  114. });
  115. }
  116. /**
  117. * 截取摄像头图片
  118. * @returns {string}
  119. */
  120. captureVideo() {
  121. this.canvasContext.drawImage(
  122. this.videoElement,
  123. 0,
  124. 0,
  125. this.videoElement.offsetWidth,
  126. this.videoElement.offsetHeight
  127. );
  128. return this.canvasElement
  129. .toDataURL("image/jpeg", 0.5)
  130. .split("base64,")[1];
  131. }
  132. /**
  133. * 创建视频详情元素,播放摄像头视频流
  134. */
  135. initVideo() {
  136. this.videoElement = document.createElement("video");
  137. this.videoElement.setAttribute("playsinline", "playsinline");
  138. document.body.appendChild(this.videoElement);
  139. }
  140. /**
  141. * 创建canvas,截取摄像头图片时使用
  142. */
  143. initCanvas() {
  144. this.canvasElement = document.createElement("canvas");
  145. this.canvasElement.setAttribute(
  146. "width",
  147. window.innerWidth.toString() + "px"
  148. );
  149. this.canvasElement.setAttribute(
  150. "height",
  151. window.innerHeight.toString() + "px"
  152. );
  153. this.canvasContext = this.canvasElement.getContext("2d");
  154. // document.body.appendChild(this.canvasElement);
  155. }
  156. /**
  157. * 识别
  158. * @param callback
  159. */
  160. startRecognize(callback) {
  161. this.timer = window.setInterval(() => {
  162. // 等待上一次识别结果
  163. if (this.isRecognizing) {
  164. return;
  165. }
  166. this.isRecognizing = true;
  167. // 从摄像头中抓取一张图片
  168. const image = { image: this.captureVideo() };
  169. // 发送到服务器识别
  170. this.httpPost(image)
  171. .then(msg => {
  172. this.stopRecognize();
  173. callback(msg);
  174. })
  175. .catch(err => {
  176. this.isRecognizing = false;
  177. console.info(err);
  178. });
  179. }, this.interval);
  180. }
  181. /**
  182. * 停止识别
  183. */
  184. stopRecognize() {
  185. if (this.timer) {
  186. window.clearInterval(this.timer);
  187. this.isRecognizing = false;
  188. }
  189. }
  190. httpPost(image) {
  191. return new Promise((resolve, reject) => {
  192. const http = new XMLHttpRequest();
  193. http.onload = () => {
  194. try {
  195. const msg = JSON.parse(http.responseText);
  196. if (http.status === 200) {
  197. if (msg.statusCode === 0) {
  198. resolve(msg.result);
  199. } else {
  200. reject(msg);
  201. }
  202. } else {
  203. reject(msg);
  204. }
  205. } catch (err) {
  206. reject(err);
  207. }
  208. };
  209. http.onerror = err => {
  210. reject(err);
  211. };
  212. http.open("POST", this.recognizeUrl);
  213. http.setRequestHeader(
  214. "Content-Type",
  215. "application/json;Charset=UTF-8"
  216. );
  217. if (this.token) {
  218. // 将云识别认证token写在请求头中
  219. http.setRequestHeader("Authorization", this.token);
  220. }
  221. http.send(JSON.stringify(image));
  222. });
  223. }
  224. }
  225. //# sourceMappingURL=webar.js.map