webar.js 9.7 KB

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