Mindar.JS——实现AR图像追踪插入图片或视频

Mindar.JS使用方式

注意:此篇文章需要启动https才可调用相机权限

图像追踪示例

  1. 需要用到两个js库
html 复制代码
    <script src="./js/aframe.min.js"></script>
    <script src="./js/mindar-image-aframe.prod.js"></script>
  1. 下面看一下标签结构

这里是默认配置

js 复制代码
    document.addEventListener("DOMContentLoaded", function () {
      const sceneEl = document.querySelector('a-scene');
      let arSystem;
      sceneEl.addEventListener('loaded', function () {
        // alert('执行')
        arSystem = sceneEl.systems["mindar-image-system"];

      });
      const startButton = document.querySelector("#example-start-button");
      startButton.addEventListener('click', () => {
        console.log("start");
        arSystem.start(); // start AR 
        arSystem.pause(true);
      });
      sceneEl.addEventListener("arReady", (event) => {
        alert('AR引擎已启动')
      });
      sceneEl.addEventListener("arError", (event) => {
        alert('相机未启动成功!')
      });
    });

上面图片提到需要一个Mind文件,这个文件就是图像识别的图片,工具在这里图像目标编辑器

图像目标编辑器


如果是图片识别显示图片的话就很简单了

关于渲染视频

如果我想扫描图片展示视频该如何去操作呢?

我这里加了两张png图片用来做暂停按钮

我这里按顺序称谓opc.png背景2.png按钮

当我们点击背景时让视频暂停,并让按钮层级往内移动 或者 让按钮隐藏

再次点击背景让视频播放,让按钮显示

js 复制代码
const portfolio = document.querySelector("#portfolio-panel");
    const paintandquestPreviewButton = document.querySelector("#paintandquest-preview-button");
    const paintandquestPreviewButton2 = document.querySelector("#paintandquest-preview-button2");
    portfolio.setAttribute("visible", true);
    let y = 0;
    let status = false;
    paintandquestPreviewButton.addEventListener('click', () => {
      if (status == false) {
        // paintandquestPreviewButton.setAttribute("visible", false);
        const testVideo = document.createElement("video");
        const canplayWebm = testVideo.canPlayType('video/webm; codecs="vp8, vorbis"');
        if (canplayWebm == "") {
          document.querySelector("#paintandquest-video-link").setAttribute("src", "#paintandquest-video-mp4");
          document.querySelector("#paintandquest-video-mp4").play();
        } else {
          paintandquestPreviewButton2.setAttribute("visible", false);
          document.querySelector("#paintandquest-video-link").setAttribute("src", "#paintandquest-video-mp4");
          document.querySelector("#paintandquest-video-mp4").play();
          // alert('播放')
          status = true
        }
      } else {
        paintandquestPreviewButton2.setAttribute("visible", true);
        // alert('暂停')
        // portfolio.setAttribute("visible", true);
        document.querySelector("#paintandquest-video-mp4").pause();

        status = false;
      }
    });
    portfolio.setAttribute("position", "0 " + y + " -0.01");

完整代码:

html 复制代码
<!--
 * @Description: 
 * @Autor: Southern Wind
 * @Date: 2023-07-25 11:21:40
 * @LastEditors: Southern Wind
 * @LastEditTime: 2023-07-25 12:00:42
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>AR相机</title>
  <script src="./js/aframe.min.js"></script>
  <script src="./js/mindar-image-aframe.prod.js"></script>

  <script src="./js/vconsole.min.js"></script>
</head>

<body>
  <div style="position: absolute; z-index: 1000">
    <button id="example-start-button">重新加载</button>
  </div>
  <a-scene mindar-image="imageTargetSrc: ./targets.mind;" vr-mode-ui="enabled: false" device-orientation-permission-ui="enabled: false">
    <a-assets>
      <img id="paintandquest-preview" src="./images/opc.png" />
      <img id="paintandquest-preview2" src="./images/2.png" />
      <video id="paintandquest-video-mp4" autoplay="false" loop="true" src="./1234.mp4" playsinline webkit-playsinline></video>

    </a-assets>

    <a-camera position="0 0 0" look-controls="enabled:  false" cursor="fuse: false; rayOrigin: mouse;" raycaster="far: 10000; objects: .clickable"></a-camera>
    <a-entity mindar-image-target="targetIndex: 0" mytarget>
      <a-entity visible=false id="portfolio-panel" position="0 0 -0.01">
        <a-image id="paintandquest-preview-button" class="clickable" src="#paintandquest-preview" alpha-test="0.5" position="0 0 0.1" height="0.552" width="1">
        </a-image>
        <a-image id="paintandquest-preview-button2" class="clickable" src="#paintandquest-preview2" alpha-test="0.5" position="0 0 0.05" height="0.552" width="1">
        </a-image>
        <a-video id="paintandquest-video-link" webkit-playsinline playsinline width="1" height="0.552" position="0 0 0"></a-video>
      </a-entity>
    </a-entity>
  </a-scene>
  <script>
    const vConsole = new VConsole()
    console.log(vConsole);
  </script>
  <script>
    const portfolio = document.querySelector("#portfolio-panel");
    const paintandquestPreviewButton = document.querySelector("#paintandquest-preview-button");
    const paintandquestPreviewButton2 = document.querySelector("#paintandquest-preview-button2");
    portfolio.setAttribute("visible", true);
    let y = 0;
    let status = false;


    paintandquestPreviewButton.addEventListener('click', () => {
      if (status == false) {
        // paintandquestPreviewButton.setAttribute("visible", false);
        const testVideo = document.createElement("video");
        const canplayWebm = testVideo.canPlayType('video/webm; codecs="vp8, vorbis"');
        if (canplayWebm == "") {
          document.querySelector("#paintandquest-video-link").setAttribute("src", "#paintandquest-video-mp4");
          document.querySelector("#paintandquest-video-mp4").play();
        } else {
          paintandquestPreviewButton2.setAttribute("visible", false);
          document.querySelector("#paintandquest-video-link").setAttribute("src", "#paintandquest-video-mp4");
          document.querySelector("#paintandquest-video-mp4").play();
          // alert('播放')
          status = true

        }
      } else {
        paintandquestPreviewButton2.setAttribute("visible", true);
        // alert('暂停')
        // portfolio.setAttribute("visible", true);
        document.querySelector("#paintandquest-video-mp4").pause();

        status = false;
      }

    });
    portfolio.setAttribute("position", "0 " + y + " -0.01");


    document.addEventListener("DOMContentLoaded", function () {
      const sceneEl = document.querySelector('a-scene');
      let arSystem;
      sceneEl.addEventListener('loaded', function () {
        // alert('执行')
        arSystem = sceneEl.systems["mindar-image-system"];

      });
      const startButton = document.querySelector("#example-start-button");
      startButton.addEventListener('click', () => {
        console.log("start");
        arSystem.start(); // start AR 
        arSystem.pause(true);
      });
      sceneEl.addEventListener("arReady", (event) => {
        alert('AR引擎已启动')
      });
      sceneEl.addEventListener("arError", (event) => {
        alert('相机未启动成功!')
      });
    });
  </script>

</body>

</html>

gitee地址

实际效果:

手机端ar图像测试

相关推荐
你挚爱的强哥43 分钟前
✅✅✅【Vue.js】sd.js基于jQuery Ajax最新原生完整版for凯哥API版本
javascript·vue.js·jquery
LNTON羚通1 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
前端Hardy1 小时前
纯HTML&CSS实现3D旋转地球
前端·javascript·css·3d·html
susu10830189111 小时前
vue3中父div设置display flex,2个子div重叠
前端·javascript·vue.js
小镇程序员4 小时前
vue2 src_Todolist全局总线事件版本
前端·javascript·vue.js
疯狂的沙粒5 小时前
对 TypeScript 中函数如何更好的理解及使用?与 JavaScript 函数有哪些区别?
前端·javascript·typescript
瑞雨溪5 小时前
AJAX的基本使用
前端·javascript·ajax
力透键背5 小时前
display: none和visibility: hidden的区别
开发语言·前端·javascript
程楠楠&M5 小时前
node.js第三方Express 框架
前端·javascript·node.js·express
weiabc5 小时前
学习electron
javascript·学习·electron