Newer
Older
KaiFengPC / src / components / videoHK / index.vue
@zhangdeliang zhangdeliang on 20 May 5 KB 项目初始化
<template>
  <!-- 站点- 查看视频监控 -->
  <div class="publicVideo">
    <div id="videoEquipHK" :style="{ height: props.videoHeightCus + 'px' }"></div>
  </div>
</template>
<script setup>
const props = defineProps({
  videoHeightCus: {
    type: String,
    default: '500',
  },
  cameraIndexCode: {
    type: String,
    default: '8df2bf406a4a4eadad1d9e94ff7206ff',
  },
});
// 海康视频
const showVideos = ref({
  cameraIndexCode: props.cameraIndexCode, // 监控点编号
  streamMode: 0, // 主子码流标识:0-主码流,1-子码流
  transMode: 1, // 传输协议:0-UDP,1-TCP
  gpuMode: 0, // 是否启用GPU硬解,0-不启用,1-启用
  wndId: 1, // 播放窗口序号
});
const videoWidth = ref(800);
const videoHeight = ref(Number(props.videoHeightCus));
const Count = ref(0);
const plugKey = ref('');

/* 创建插件实例 */
function initPlugin() {
  const dll = { dllPath: './VideoPluginConnect.dll' };
  window.videoWebControl = new WebControl({
    szPluginContainer: 'videoEquipHK', // 指定容器id
    iServicePortStart: 15900,
    iServicePortEnd: 15909,
    szClassId: '23BF3B0A-2C56-4D97-9C03-0CB103AA8F11', // 用于IE10使用ActiveX的clsid

    cbConnectSuccess: () => {
      window.videoWebControl.JS_StartService('window', dll).then(() => {
        // 设置视频 web 插件消息回调
        window.videoWebControl.JS_SetWindowControlCallback({
          // cbIntegrationCallBack: msg => { }
        });
        // 启动插件服务成功
        window.videoWebControl.JS_CreateWnd('videoEquipHK', videoWidth.value, videoHeight.value).then(() => {
          // JS_CreateWnd创建视频播放窗口,宽高可设定
          initVideo(window.videoWebControl); // 创建播放实例成功后初始化
          console.log('启动插件成功!');
        });
      });
    },
    // 插件服务启动失败,弹框提示
    cbConnectError: () => {
      window.videoWebControl = null;
      Count.value++;
      if (Count.value < 2) {
        WebControl.JS_WakeUp('VideoWebPlugin://');
        setTimeout(() => {
          initPlugin();
        }, 8000);
      } else {
      }
      console.log('创建插件失败');
    },
  });
}
/* 获取公钥 */
function initVideo(videoWebControl) {
  const params = {
    funcName: 'getRSAPubKey',
    argument: JSON.stringify({ keyLength: 1024 }),
  };

  videoWebControl.JS_RequestInterface(params).then(res => {
    if (res.responseMsg.data) {
      plugKey.value = res.responseMsg.data;
      getVideoConfig(videoWebControl);
    }
  });
}
/* 视频插件配置  插件初始化 */
function getVideoConfig(videoWebControl) {
  const configObj = {
    funcName: 'init',
    argument: JSON.stringify({
      appkey: '22920329', // API网关提供的appkey
      secret: setEncrypt('ryhnhHNWRoXqGYVjsKf9'), // API网关提供的secret
      ip: '183.64.91.250', // API网关IP地址z
      playMode: 0, // 播放模式 0-预览,1-回放
      port: 8443, // 端口
      snapDir: 'D:\\SnapDir', // 抓图存储路径
      videoDir: 'D:\\VideoDir', // 紧急录像或录像剪辑存储路径
      layout: '1x1', // 布局
      enableHTTPS: 1, // 是否启用HTTPS协议 0https 1http
      encryptedFields: 'secret', // 加密字段
      showToolbar: 1, // 是否显示工具栏
      showSmart: 1, // 是否显示智能信息
      buttonIDs: '0,16,256,257,258,259,260,512,513,514,515,516,517,768,769', // 自定义工具条按钮 新的
    }),
  };
  videoWebControl.JS_RequestInterface(configObj).then(() => {
    videoWebControl.JS_Resize(videoWidth.value, videoHeight.value);
    getClickAction();
  });
}
/* 视频流RSA加密 */
function setEncrypt(value) {
  const encrypt = new JSEncrypt();
  encrypt.setPublicKey(plugKey.value);
  return encrypt.encrypt(value);
}
/* 视频预览 */
function getClickAction() {
  console.log(showVideos.value, 'showVideos');
  window.videoWebControl.JS_RequestInterface({
    funcName: 'startPreview',
    argument: JSON.stringify(showVideos.value),
  });
}
function windowResize() {
  let div = document.getElementById('videoEquipHK');
  videoWidth.value = div.offsetWidth; // 返回元素的总宽度
  videoHeight.value = div.offsetHeight; // 返回元素的总高度
  if (window.videoWebControl) {
    window.videoWebControl.JS_Resize(videoWidth.value, videoHeight.value);
  }
}

onMounted(() => {
  setTimeout(() => {
    videoWidth.value = document.getElementById('videoEquipHK').offsetWidth;
    videoHeight.value = document.getElementById('videoEquipHK').offsetHeight;
    if (window.videoWebControl == null) {
      initPlugin();
    } else {
      window.videoWebControl.JS_ShowWnd();
    }
  });
  window.onresize = () => {
    videoWidth.value = document.getElementById('videoEquipHK').offsetWidth;
    windowResize();
  };
});
onBeforeUnmount(() => {
  if (window.videoWebControl != null) {
    window.videoWebControl.JS_HideWnd();
    window.videoWebControl.JS_DestroyWnd();
    window.videoWebControl = null;
  }
});
</script>
<style lang="scss">
.publicVideo {
  width: 100%;
  #videoEquipHK {
    border: 1px solid #c03639;
    .wc-grab-open-image {
      display: none;
    }
  }
}
</style>