app.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // 别间隔时间(毫秒), 识别服务地址
  2. const webAR = new WebAR(1000, "/webar/recognize");
  3. // Threejs简单使用类
  4. const threeHelper = new ThreeHelper();
  5. // 列出并打开设备上的摄像头
  6. document.querySelector("#openCamera").addEventListener("click", function() {
  7. const videoSelect = document.querySelector("#videoDevice");
  8. navigator.mediaDevices.enumerateDevices().then(devices => {
  9. console.log(devices);
  10. let i = devices.findIndex(d => {
  11. return d.kind === "videoinput";
  12. });
  13. console.log(i);
  14. if (i > -1) {
  15. navigator.mediaDevices
  16. .getUserMedia({
  17. audio: true,
  18. video: { facingMode: { exact: "environment" } }
  19. })
  20. .then(stream => {
  21. webAR
  22. .listCamera(videoSelect)
  23. .then(msg => {
  24. // 隐藏"打开摄像头"按钮
  25. this.style.display = "none";
  26. videoSelect.style.display = "inline-block";
  27. document.querySelector("#start").style.display =
  28. "inline-block";
  29. document.querySelector("#stop").style.display =
  30. "inline-block";
  31. videoSelect.onchange = () => {
  32. webAR.openCamera(JSON.parse(videoSelect.value));
  33. };
  34. // 打开摄像头
  35. // 打开后置摄像头参数: {audio: false, video: {facingMode: {exact: 'environment'}}}
  36. webAR
  37. .openCamera(JSON.parse(videoSelect.value))
  38. .then(msg => {
  39. console.info(msg);
  40. })
  41. .catch(err => {
  42. console.info(err);
  43. });
  44. })
  45. .catch(err => {
  46. // 没有找到摄像头
  47. console.info(err);
  48. });
  49. });
  50. }
  51. });
  52. });
  53. // 开启识别
  54. document.querySelector("#start").addEventListener(
  55. "click",
  56. () => {
  57. webAR.startRecognize(msg => {
  58. console.info(msg);
  59. // 可以将 setting 作为meta上传到EasyAR的云识别,使用方法如下
  60. // const setting = window.atob(msg.meta);
  61. const setting = {
  62. model: "asset/model/walk.fbx",
  63. scale: 0.2,
  64. position: [0, 0, 0]
  65. };
  66. threeHelper.loadObject(setting);
  67. });
  68. },
  69. false
  70. );
  71. // 暂停识别
  72. document.querySelector("#stop").addEventListener(
  73. "click",
  74. () => {
  75. webAR.stopRecognize();
  76. },
  77. false
  78. );
  79. //# sourceMappingURL=app.js.map