海康威视实现单个监控全屏(退出全屏),其他监控隐藏

本文介绍了如何通过JavaScript插件实现窗口全屏事件的监听和处理,包括设置消息回调、监听窗口全屏和退出全屏事件,以及在全屏和退出全屏时对窗口进行隐藏和显示的控制。详细阐述了从子页面设置消息回调、父页面接收和处理消息,再到子页面响应父页面指令的完整流程。同时,还展示了Vuex状态管理中如何存储和更新全屏状态的内码。
摘要由CSDN通过智能技术生成

1.核心思想

  1. JS_SetWindowControlCallback 设置的消息回调中监听窗口全屏事件
  2. 监听到窗口全屏事件时调 JS_HideWnd 插件窗口隐藏接口对除接收到全屏事件的插件窗口外的其它窗口隐藏
  3. 监听到窗口退出全屏事件时调 JS_ShowWnd 插件窗口显示窗口对接收到退出全屏事件的插件窗口外的其它窗口显示

2.文件结构

3.核心代码

3.1 步骤一 

子页面demo_embedded_for_iframe.html设置监控操作消息回调

// ② (可选) 设置消息回调 JS_SetWindowControlCallback
        oWebControl.JS_SetWindowControlCallback({
          // cbIntegrationCallBack: console.log,
          cbIntegrationCallBack: function(oData) { // oData 是封装的视频 web 插件回调消息的消息体
            if (oData.responseMsg.type === 5) {
              // 进入全屏/退出全屏消息
              console.log('全屏', oData);
              const msg = oData.responseMsg.msg;
              if (msg.result === 1024) {
                // 进入全屏
                // oWebControl.JS_HideWnd();
                  window.parent.postMessage({
                      action: 'fullScreen',
                      msg: '全屏',
                      cameraIndexCode: myCameraIndexCode,
                  });
              } else {
                // 0x0401(1025)- 退出全屏。
                // oWebControl.JS_ShowWnd();
                  window.parent.postMessage({
                      action: 'exitFullScreen',
                      msg: '退出全屏',
                      cameraIndexCode: myCameraIndexCode,
                  });
              }

            }
          }
        });

3.2 步骤二

VideoFrame.vue文件内 接收子页面的消息并处理,每个父页面进行全屏监控的内码和退出全屏监控的内码监听,对除接收到全屏事件(退出全屏事件)的插件窗口外的其它子页面发送窗口隐藏(窗口显示)信号;

<template>
  <div class="video-frame">
    <iframe
      frameborder="0"
      ref="iframeEle"
    />
  </div>
</template>
<script>
import { mapMutations, mapGetters } from 'vuex';
export default {
  props: {
    cameraIndexCode: {
      type: String,
      default: '',
    },
  },
  watch: {
    cameraIndexCode() {
      const iframeWin = this.$refs.iframeEle;
      iframeWin.contentWindow.postMessage({
        action: 'updateCameraIndexCode',
        msg: '更新cameraIndexCode',
        cameraIndexCode: this.cameraIndexCode,
      }, '*');
    },
    fullScreenCameraIndexCode() {
      if (this.fullScreenCameraIndexCode) {
        if (this.fullScreenCameraIndexCode !== this.cameraIndexCode) {
          const iframeWin = this.$refs.iframeEle;
          iframeWin.contentWindow.postMessage({
            action: 'hideWin',
            msg: '隐藏窗口',
          }, '*');
        }
      }
    },
    exitFullScreenCameraIndexCode() {
      if (this.exitFullScreenCameraIndexCode) {
        if (this.exitFullScreenCameraIndexCode !== this.cameraIndexCode) {
          const iframeWin = this.$refs.iframeEle;
          iframeWin.contentWindow.postMessage({
            action: 'showWin',
            msg: '展示窗口',
          }, '*');
        }
      }
    },
  },
  computed: {
    ...mapGetters(['fullScreenCameraIndexCode', 'exitFullScreenCameraIndexCode']),
  },
  data() {
    return {
      scaleX: 1,
      scaleY: 1,
      videoUrl: '/video/demo_embedded_for_iframe.html',
    };
  },
  mounted() {
    // console.log(this);
    const onResize = this.onResize.bind(this); // onResize 绑定新函数 可以使用当前组件的属性和方法
    const changeVideoPos = this.changeVideoPos.bind(this);
    console.log(window);
    window.globalScale.add(onResize);
    window.addEventListener('message', changeVideoPos); // 接收来自子页面的消息,
    this.$once('hook:beforeDestroy', () => {
      window.globalScale.remove(onResize);
      window.removeEventListener('message', changeVideoPos);
    });
    this.setIframe();
  },

  methods: {
    ...mapMutations(['setFullScreenCameraIndexCode', 'setExitFullScreenCameraIndexCode']),
    setIframe() {
      // console.log(this.cameraIndexCode);
      const iframeWin = this.$refs.iframeEle;
      const { clientWidth, clientHeight } = iframeWin.parentNode;
      iframeWin.style.width = clientWidth * this.scaleX + 'px';
      iframeWin.style.height = clientHeight * this.scaleY + 'px';
      setTimeout(() => {
        iframeWin.src = this.videoUrl + '#' + this.cameraIndexCode;
      }, 1000);
    },
    onResize(scaleX = 1, scaleY = 1) {
      this.scaleX = scaleX;
      this.scaleY = scaleY;
    },
    changeVideoPos(e) {
      const iframeWin = this.$refs.iframeEle;
      // console.log(iframeWin);
      // console.log(iframeWin.getBoundingClientRect());
      if (e && e.data) {
        switch (e.data.action) {
          case 'updateInitParam':
            // 用iframe嵌套页面时,如果父页面要获取子页面里面的内容,可以使用contentWindow或者contentDocument
            // contentWindow这是个只读属性
            iframeWin.contentWindow.postMessage({
              action: 'updateInitParam',
              msg: '更新Pos',
              iframeClientPos: iframeWin.getBoundingClientRect(), // 方法返回元素的大小及其相对于视口的位置。
            });
            break;
          case 'fullScreen': {
            const fullScreenCameraIndexCode = e.data.cameraIndexCode;
            this.setFullScreenCameraIndexCode(fullScreenCameraIndexCode);
            this.setExitFullScreenCameraIndexCode('');
          }
            break;
          case 'exitFullScreen': {
            const fullScreenCameraIndexCode = e.data.cameraIndexCode;
            this.setExitFullScreenCameraIndexCode(fullScreenCameraIndexCode);
            this.setFullScreenCameraIndexCode('');
          }
            break;
          default:
            break;
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.video-frame {
  //height: 42.5rem;
  height: 31.5rem;
  position: relative;
  iframe {
    position: absolute;
    // outline: 5px solid red;
    width: 100%;
    height: 100%;
  }
}
</style>

vuex中

import Vue from 'vue';
import Vuex from 'vuex';
import digitCounty from './digitCounty';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    fullScreenCameraIndexCode: '', // 全屏监控的内码
    exitFullScreenCameraIndexCode: '',// 退出全屏监控的内码
  },
  mutations: {
    setFullScreenCameraIndexCode(state, msg) {
      state.fullScreenCameraIndexCode = msg;
    },
    setExitFullScreenCameraIndexCode(state, msg) {
      state.exitFullScreenCameraIndexCode = msg;
    },
  },
  getters: {
    fullScreenCameraIndexCode: (state) => state.fullScreenCameraIndexCode,
    exitFullScreenCameraIndexCode: (state) => state.exitFullScreenCameraIndexCode,
  },
  actions: {},
  modules: {
  },
});

3.3 步骤三

子页面demo_embedded_for_iframe.html中接收父页面发送的窗口隐藏(窗口显示)信号;并进行对应处理;

// 步骤1:监听父页面的消息
  window.addEventListener('message', function (e) {
    console.log(e);
    if (e && e.data) {
      switch (e.data.action) {
        case 'updateInitParam':
          var iframeClientPos = e.data.iframeClientPos;    // iframe相对视窗的位置
          // console.log(iframeClientPos);
          oWebControl.JS_SetDocOffset({
            left: iframeClientPos.left,
            top: iframeClientPos.top,
          });  // 更新插件窗口位置
          oWebControl.JS_Resize(divWidth, divHeight);
          // setWndCover();
          break;
        case 'updateCameraIndexCode':
          var cameraIndexCode = e.data.cameraIndexCode; // 获得更新后的cameraIndexCode
            myCameraIndexCode = cameraIndexCode;
          // initPlugin();
          updataCameraIndexCode(cameraIndexCode);
          break;
        case 'hideWin':
            oWebControl.JS_HideWnd();
          break;
        case 'showWin':
            oWebControl.JS_ShowWnd();
          break;
        default:
          break;
      }
    }
  });

根据提供的引用内容,海康的web3.2控件开发包支持使用IP地址或监控点编号进行对接。然而,需要确认产品是否支持websockets协议。如果不支持,只能使用web3.0插件。web3.0插件功能全面,但不支持谷歌浏览器,而web3.2插件需要支持websocket取流。此外,web3.2插件不支持直接将截图文件保存到指定路径地址,不支持双击全屏、自动云台自转以及视频的快放慢放控制等功能。[1] 关于海康提供的demo,如果使用https服务,虽然登录接口中包含了协议类型,选择了https协议只能登录成功,无法进行预览。经过咨询海康客服得知,他们不支持https协议。然而,经过排查发现是因为websocket连接使用的是wss协议,而硬盘录像机没有域名和可信的SSL证书,初步怀疑这是导致wss连接失败的原因。为了解决这个问题,可以通过让JS发起的websocket请求发送给自己,然后由nginx进行转发。需要修改webVideoCtrl.js文件中生成wss连接地址的部分来实现这个目的。[2] 对于web3.2回放失败的问题,如果项目对接摄像头,摄像头需要支持插卡功能,否则只能使用web3.0插件来查看回放视频。如果项目对接NVR,NVR需要支持websocket协议,并且需要有硬盘,并配置了计划模板(例如全天录制)。如果满足这些条件仍然无法查看回放视频,可以登录NVR确认是否有回放视频,如果NVR中有但控件中查不到,需要确认对接方式。对接NVR需要通过登录NVR的IP和端口通过通道查看,而不能通过登录摄像头的IP和端口查看。[3] 综上所述,根据提供的信息,海康的web3.2控件开发包支持使用IP地址或监控点编号进行对接,但需要确认产品是否支持websockets协议。如果不支持,可以考虑使用web3.0插件。此外,对于https服务的情况,需要注意wss连接可能会失败,可以通过修改代码或使用转发方式解决。对于web3.2回放失败的问题,需要确保摄像头或NVR满足相应的要求,并确认对接方式是否正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值