前端接入海康威视web插件

VUE3接入海康威视web插件

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

web插件下载

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

插件导入

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

html 复制代码
<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. 初始化插件
js 复制代码
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. 登录海康相机
js 复制代码
// 根据实际情况修改相机配置
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. 获取画面
js 复制代码
// 获取单个画面
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. 停止及销毁
js 复制代码
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)

html 复制代码
<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实现可以查看官方文档。

相关推荐
lolo大魔王1 分钟前
Go 语言 Web 框架 Gin 入门详解
前端·golang·gin
喵个咪15 分钟前
一套Schema,生成全部代码|Kratos高效开发新范式
前端·后端·架构
Dewyze同学19 分钟前
我用 Cursor 三天从零到可上线:uni-app + Fastify 全栈小程序复盘
前端
菊风 Juphoon21 分钟前
如何让车载通话从“能用”变“好用”?请看菊风智能车载音视频解决方案
音视频
qq_3813385024 分钟前
前端虚拟列表与无限滚动性能优化实战:从万级数据到丝滑体验
前端·javascript·html·优化
hexu_blog28 分钟前
前端vue后端springboot如何实现图片格式转换
前端·javascript·vue.js
代码煮茶28 分钟前
Vue3 项目规范实战 | ESLint+Prettier+Git Hooks 搭建前端代码规范体系
前端·javascript·vue.js
米丘29 分钟前
新一代代码格式化工具 Oxfmt/Oxlint
前端·rust·前端工程化
韭菜炒大葱37 分钟前
讲讲 浏览器的缓存机制
前端·面试·浏览器
AI砖家39 分钟前
DeepSeek TUI 保姆级安装配置全指南 -Windows||macOS双平台全覆盖
服务器·前端·人工智能·windows·macos·ai编程·策略模式