前端接入海康威视web插件

三尺非寒  金牌会员 | 2024-12-31 01:35:01 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 894|帖子 894|积分 2682

VUE3接入海康威视web插件

接入海康的web插件实现在网页端直接表现摄像头监控画面,此办理方案必要在用户电脑上安装exe插件。

web插件下载

起首在官网下载海康插件,打开demo文件夹可以看到必要用到的js和bin目次下的exe插件。


插件导入

安装HCWebSDKPlugin.exe到电脑中,然后在项目标index.html中导入必要使用的js文件,由于jsVideoPlugin-1.0.0.min.js是用于控制摄像头等功能,此处我们仅必要获取监控画面,因此按现实需求引入。
  1. <body>
  2.   <div id="app"></div>
  3.   <script src="/jquery-1.7.1.min.js"></script>
  4.   <script id="videonode" src="/webVideoCtrl.js"></script>
  5.   <script type="module" src="/src/main.js"></script>
  6. </body>
复制代码

插件使用

在项目中引入插件后,即可在组件页面实现初始化,由于在script标签中引入,因此对象存在全局变量,在组件中直接使用即可。
必要先进行初始化,然后再登录摄像头,此处提供简单实现。

  • 初始化插件
  1. const startPlugin = () => {
  2.     return new Promise((resolve, reject) => {
  3.         WebVideoCtrl.I_InitPlugin({
  4.             iWndowType: 2,
  5.             bWndFull: true,  //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
  6.             cbInitPluginComplete: function () {
  7.                 WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
  8.                     // 检查插件是否最新
  9.                     resolve();
  10.                     WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
  11.                         if (bFlag) {
  12.                             // 提示用户安装最新的HCWebSDKPlugin.exe
  13.                         }
  14.                     });
  15.                 }, () => {
  16.                     reject();
  17.                     // 初始化失败,提示用户安装最新的HCWebSDKPlugin.exe
  18.                 });
  19.             }
  20.         });
  21.     })
  22. }
复制代码

  • 登录海康相机
  1. // 根据实际情况修改相机配置
  2. const cameraList = [
  3.         {
  4.             "ip": "xxx.xxx.xxx.xxx",
  5.             "protocol": 1,
  6.             "port": "80",
  7.             "userName": "admin",
  8.             "password": "123456"
  9.         },
  10.         {
  11.             "ip": "xxx.xxx.xxx.xxx",
  12.             "protocol": 1,
  13.             "port": "80",
  14.             "userName": "admin",
  15.             "password": "123456"
  16.         },
  17.         {
  18.             "ip": "xxx.xxx.xxx.xxx",
  19.             "protocol": 1,
  20.             "port": "80",
  21.             "userName": "admin",
  22.             "password": "123456"
  23.         },
  24.         {
  25.             "ip": "xxx.xxx.xxx.xxx",
  26.             "protocol": 1,
  27.             "port": "80",
  28.             "userName": "admin",
  29.             "password": "123456"
  30.         }
  31. ]
  32.    
  33. // 登录单个摄像头
  34. const login = (config) => {
  35.     return new Promise((resolve, reject) => {
  36.         const { ip, protocol, port, userName, password } = config;
  37.         WebVideoCtrl.I_Login(ip, protocol, port, userName, password, {
  38.             success: () => {
  39.                 resolve();
  40.             },
  41.             error: () => {
  42.                 console.log("登录失败");
  43.                 reject();
  44.             }
  45.         })
  46.     })
  47. }
  48. const cameraListLogin = () => {
  49.     const promiseMap = cameraList.map((item) => {
  50.         return login(item);
  51.     })
  52.     return Promise.all(promiseMap);
  53. }
复制代码

  • 获取画面
  1. // 获取单个画面
  2. const preview = (config) => {
  3.     return new Promise((resolve, reject) => {
  4.         const { ip, port, window } = config;
  5.         WebVideoCtrl.I_StartRealPlay(`${ip}_${port}`, {
  6.             async: true,
  7.             timeout: 900,
  8.             iWndIndex: window,
  9.             iChannelID: 1,
  10.             bZeroChannel: false,
  11.             iStreamType: 2,
  12.             success: () => {
  13.                 resolve();
  14.             },
  15.             error: () => {
  16.                 reject();
  17.                 console.log("预览失败");
  18.             }
  19.         })
  20.     })
  21. }
  22. const cameraListPreview = () => {
  23.     const promiseMap = cameraList.map((item, index) => {
  24.         return preview({ ...item, window: index });
  25.     })
  26.     return Promise.all(promiseMap);
  27. }
复制代码

  • 停止及销毁
  1. const stopAllPreview = (callBack) => {
  2.     WebVideoCtrl.I_StopAllPlay().then(() => callBack?.());
  3. }
  4. const loginOut = () => {
  5.     cameraList.forEach((item) => {
  6.         WebVideoCtrl.I_Logout(`${item.ip}_${item.port}`)
  7.     })
  8. }
  9. const breakDom = () => {
  10.     WebVideoCtrl.I_DestroyPlugin();
  11. }
复制代码

团体实现(vue3+setup)

  1. <template>    <div class="monitor-container">        <div id="divPlugin" v-if="cameraInitComplete"/>        <div v-else>            <p>无数据</p>        </div>    </div></template><script setup>import { onBeforeRouteLeave } from 'vue-router';const cameraInitComplete = ref(true);const cameraInitLoading = ref(false);const cameraList = ref([        {            "ip": "xxx.xxx.xxx.xxx",            "protocol": 1,            "port": "80",            "userName": "admin",            "password": "123456"        },        {            "ip": "xxx.xxx.xxx.xxx",            "protocol": 1,            "port": "80",            "userName": "admin",            "password": "123456"        },        {            "ip": "xxx.xxx.xxx.xxx",            "protocol": 1,            "port": "80",            "userName": "admin",            "password": "123456"        },        {            "ip": "xxx.xxx.xxx.xxx",            "protocol": 1,            "port": "80",            "userName": "admin",            "password": "123456"        }]);const init = async () => {        cameraInitLoading.value = true;    try {        await startPlugin();        await cameraListLogin();        await cameraListPreview();        cameraInitComplete.value = true;    } catch (error) {        cameraInitComplete.value = false;    } finally {        cameraInitLoading.value = false;    }}const startPlugin = () => {
  2.     return new Promise((resolve, reject) => {
  3.         WebVideoCtrl.I_InitPlugin({
  4.             iWndowType: 2,
  5.             bWndFull: true,  //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
  6.             cbInitPluginComplete: function () {
  7.                 WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
  8.                     // 检查插件是否最新
  9.                     resolve();
  10.                     WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
  11.                         if (bFlag) {
  12.                             // 提示用户安装最新的HCWebSDKPlugin.exe
  13.                         }
  14.                     });
  15.                 }, () => {
  16.                     reject();
  17.                     // 初始化失败,提示用户安装最新的HCWebSDKPlugin.exe
  18.                 });
  19.             }
  20.         });
  21.     })
  22. }
  23. const login = (config) => {    return new Promise((resolve, reject) => {        const { ip, protocol, port, userName, password } = config;        WebVideoCtrl.I_Login(ip, protocol, port, userName, password, {            success: () => {                resolve();            },            error: () => {                console.log("登录失败");                reject();            }        })    })}const cameraListLogin = () => {    const promiseMap = cameraList.value.map((item) => {        return login(item);    })    return Promise.all(promiseMap);}const preview = (config) => {    return new Promise((resolve, reject) => {        const { ip, port, window } = config;        WebVideoCtrl.I_StartRealPlay(`${ip}_${port}`, {            async: true,            timeout: 900,            iWndIndex: window,            iChannelID: 1,            bZeroChannel: false,            iStreamType: 2,            success: () => {                resolve();            },            error: () => {                reject();                console.log("预览失败");            }        })    })}const cameraListPreview = () => {    const promiseMap = cameraList.value.map((item, index) => {        return preview({ ...item, window: index });    })    return Promise.all(promiseMap);}const stopAllPreview = (callBack) => {    WebVideoCtrl.I_StopAllPlay().then(() => callBack?.());}const loginOut = () => {    cameraList.value.forEach((item) => {        WebVideoCtrl.I_Logout(`${item.ip}_${item.port}`)    })}const breakDom = () => {    WebVideoCtrl.I_DestroyPlugin();}onBeforeRouteLeave((to, from, next) => {    if (cameraInitLoading.value) {         console.log("请等待摄像头初始化")    } else next()})onActivated(() => {    init();})onDeactivated(() => {    loginOut();    stopAllPreview(breakDom);})</script><style lang='scss' scoped>.monitor-container {    position: relative;        width: 900px;        height: 450px;        #divPlugin {                width: 900px;                height: 450px;        }}</style>
复制代码
更多Api实现可以查看官方文档。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

三尺非寒

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表