前端接入海康威视web插件

VUE3接入海康威视web插件

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


web插件下载

首先在官网下载海康插件,打开demo文件夹可以看到需要用到的js和bin目录下的exe插件。
在这里插入图片描述


插件导入

安装HCWebSDKPlugin.exe到电脑中,然后在项目的index.html中导入需要使用的js文件,由于jsVideoPlugin-1.0.0.min.js是用于控制摄像头等功能,此处我们仅需要获取监控画面,因此按实际需求引入。

<body>
  <div id="app"></div>
  <script src="/jquery-1.7.1.min.js"></script>
  <script id="videonode" src="/webVideoCtrl.js"></script>
  <script type="module" src="/src/main.js"></script>
</body>

插件使用

在项目中引入插件后,即可在组件页面实现初始化,由于在script标签中引入,因此对象存在全局变量,在组件中直接使用即可。

需要先进行初始化,然后再登录摄像头,此处提供简单实现。

  1. 初始化插件
const startPlugin = () => {
    return new Promise((resolve, reject) => {
        WebVideoCtrl.I_InitPlugin({
            iWndowType: 2,
            bWndFull: true,  //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
            cbInitPluginComplete: function () {
                WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
                    // 检查插件是否最新
                    resolve();
                    WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
                        if (bFlag) {
                            // 提示用户安装最新的HCWebSDKPlugin.exe
                        }
                    });
                }, () => {
                    reject();
                    // 初始化失败,提示用户安装最新的HCWebSDKPlugin.exe
                });
            }
        });
    })
}
  1. 登录海康相机
// 根据实际情况修改相机配置
const cameraList = [
        {
            "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 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.map((item) => {
        return login(item);
    })
    return Promise.all(promiseMap);
}
  1. 获取画面
// 获取单个画面
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.map((item, index) => {
        return preview({ ...item, window: index });
    })
    return Promise.all(promiseMap);
}
  1. 停止及销毁
const stopAllPreview = (callBack) => {
    WebVideoCtrl.I_StopAllPlay().then(() => callBack?.());
}

const loginOut = () => {
    cameraList.forEach((item) => {
        WebVideoCtrl.I_Logout(`${item.ip}_${item.port}`)
    })
}

const breakDom = () => {
    WebVideoCtrl.I_DestroyPlugin();
}

整体实现(vue3+setup)

<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 = () => {
    return new Promise((resolve, reject) => {
        WebVideoCtrl.I_InitPlugin({
            iWndowType: 2,
            bWndFull: true,  //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
            cbInitPluginComplete: function () {
                WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(() => {
                    // 检查插件是否最新
                    resolve();
                    WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
                        if (bFlag) {
                            // 提示用户安装最新的HCWebSDKPlugin.exe
                        }
                    });
                }, () => {
                    reject();
                    // 初始化失败,提示用户安装最新的HCWebSDKPlugin.exe
                });
            }
        });
    })
}

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实现可以查看官方文档。
在这里插入图片描述

<think>好的,用户的问题是关于如何使用海康威视Web插件控制云台的示例教程。我需要先回顾已有的信息,特别是参考引用中的内容。根据引用[1],云台控制的流程和播放画面类似,但更简单,用户可以通过查看demo.html和demo.js来自学。不过用户可能需要更具体的指导。 首先,我需要确认海康威视Web插件的基本使用步骤。通常,控制云台需要调用插件提供的JavaScript API,比如初始化插件、创建实例、调用云台控制方法等。可能需要包括PTZ(Pan-Tilt-Zoom)命令,如上、下、左、右移动,以及变焦等操作。 下来,我应该组织一个结构清晰的回答,分步骤说明如何实现云台控制。需要确保代码示例正确,并且符合用户提到的demo文件中的方法。同时,要提醒用户注意插件加载、参数配置、错误处理等常见问题。 另外,用户可能对云台控制的具体参数和口不太清楚,需要详细说明每个步骤的作用。例如,调用startPTZ方法时,需要传递设备ID、通道号、命令类型和速度等参数。还要提到停止运动的stopPTZ方法,因为持续运动需要停止命令。 最后,生成相关问题,帮助用户深入理解云台控制的其他方面,比如方向控制、速度调整、预置位设置等。这些问题应该覆盖用户可能遇到的后续需求,促进他们自主学习。</think>### 海康威视Web插件控制云台实现步骤 #### 一、基础环境准备 1. 确保已安装**海康威视Web插件**(如未安装,访问设备IP时会自动提示下载) 2. 在HTML文件中引入插件调用库: ```html <script src="./webVideoCtrl.js"></script> <!-- 海康官方提供的JS库 --> ``` #### 二、核心代码示例 ```javascript // 初始化插件(需在用户主动操作中触发,如按钮点击) function initPlugin() { let iRet = WebVideoCtrl.I_InitPlugin(100, 100); if (iRet === 0) { console.log("插件初始化成功"); // 创建播放实例 let oWnd = WebVideoCtrl.I_GetWindow(0); oWnd.CreateWindow("videoContainer", 640, 480); } } // 云台控制函数(方向控制示例) function ptzControl(cmd) { let deviceId = "12345678"; // 设备序列号 let channel = 1; // 通道号 let speed = 2; // 速度等级1-7 // 调用PTZ控制口 let iRet = WebVideoCtrl.I_PTZControl( deviceId, channel, WebVideoCtrl.I_PTZCommand[cmd], // 命令类型如UP/DOWN/LEFT/RIGHT speed ); if(iRet === 0) console.log("指令发送成功"); } // 停止云台运动 function ptzStop() { WebVideoCtrl.I_PTZControl(deviceId, channel, WebVideoCtrl.I_PTZCommand.STOP); } ``` #### 三、操作界面绑定 ```html <button onclick="ptzControl('UP')">向上</button> <button onclick="ptzControl('DOWN')">向下</button> <button onclick="ptzControl('LEFT')">向左</button> <button onclick="ptzControl('RIGHT')">向右</button> <button onclick="ptzStop()">停止</button> <div id="videoContainer"></div> ``` #### 四、关键参数说明 | 参数 | 说明 | 典型值 | |------|------|--------| | `I_PTZCommand` | 预置控制指令枚举 | UP/DOWN/LEFT/RIGHT/ZOOM_IN等 | | speed | 云台移动速度 | 1(最慢)-7(最快) | | channel | 频通道号 | 从设备配置中获取 |
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Raccom

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值