现状说明:
由于海康摄像头官方没有给出vue版本的组件,只有一个html开源demo,但是头疼的点在于前端项目主流使用vue想要项目中集成海康摄像头还需要引入iframe,webview等方式,这种方式引入的html存在诸多弊端,在此背景下,我将海康摄像头升级改造成了vue3版本组件
准备工作
1. 先下载安装包2. 然后安装插件webs/codebase/HCWebSDKPlugin.exe,不下载插件会导致海康摄像头诸如失败,黑色画面
3. 复制jsVideoPlugin-1.0.0.min.js 和 webVideoCtrl.js 到vue2项目中的public文件夹下,一定要放在public文件下,否则打包后读取不到js文件,无法使用,安装后重启动电脑
4.在index.html文件中加入
<script src="/p/https://blog.csdn.net/static/camera/webVideoCtrl.js"></script>
<script src="/p/https://blog.csdn.net/static/camera/jsVideoPlugin-1.0.0.min.js"></script>
这里的位置路径替换为自己的真实文件路径,也可以按照我的路径来放jsVideoPlugin-1.0.0.min.js 和 webVideoCtrl.js
组件代码
新建组件文件 HikvisionPlayer/index.vue
<template>
<div class="hk-video-preview" :style="{ height: height, width: width }">
<!-- 外层容器包裹SDK插件,解决定位脱离问题 -->
<div class="plugin-wrapper" :style="{ height: height, position: 'relative', width: width }">
<div :id="`divPlugin_${containerId}`" class="hk-video-container"></div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted, onUnmounted, onBeforeUnmount } from 'vue';
import { ElMessage } from 'element-plus';
const cameraMaxRetry = Config.cameraMaxRetry
// 定义props:接收循环时的容器ID(区分多窗口)
const props = defineProps({
containerId: {
type: [String, Number],
default: 'default',
required: false
},
deviceConfig: {
type: Object,
default: () => ({
ip: '',
port: ,
username: '',
password: '',
protocol: 1,
channelId: 1,
streamType: 2,
type: 'haikang'
})
},
projectName: {
type: String,
default: '',
required: false
},
nodeId: {
type: [String, Number],
default: '',
required: false
},
width: {
type: [Number, String],
default: '800px',
},
height: {
type: [Number, String],
default: '453px',
},
});
// 海康SDK全局变量类型声明(避免TS报错)
declare global {
interface Window {
WebVideoCtrl: any;
// 新增:全局标记SDK是否已初始化(跨组件共享)
HK_SDK_INITED: boolean;
}
}
const emit = defineEmits({
captureDataSuccess: (data: any) => true,
captureDataError: (error: any) => true,
loadError: (error: any) => true
});
// 插件初始化状态(优先读取全局标记)
const pluginInited = ref(false);
// 设备标识
const deviceIdentify = ref('');
// 当前容器ID(用于多窗口区分)
const currentContainerId = ref(`divPlugin_${props.containerId}`);
const isRecording = ref(false); // 是否正在录像
const recordFileName = ref(''); // 录像文件名
const downloadId = ref(-1); // 下载ID(用于监控下载进度)
const downloadProgress = ref(0); // 下载进度(0-100)
const downloadTimer = ref<NodeJS.Timeout | null>(null); // 下载进度监控定时器
// ========== 本地录像:开始/停止(核心) ==========
/**
* 切换录像状态(开始/停止)
*/
const toggleRecord = async (id: number) => {
// 前置校验
if (!pluginInited.value || !deviceIdentify.value) {
ElMessage.warning('请先登录并预览视频后再操作录像');
return;
}
// 开始录像
if (!isRecording.value) {
await startLocalRecord();
}
// 停止录像
else {
await stopLocalRecord();
}
};
/**
* 开始本地录像(I_StartRecord)
*/
const startLocalRecord = async () => {
try {
// 标准化录像文件名(避免特殊字符,SDK要求)
const deviceIp = props.deviceConfig.ip?.replace(/\./g, '_') || 'unknown';
recordFileName.value = `record_${props.projectName}_${Date.now()}.mp4`; // 录像默认mp4格式
// 调用开始录像接口
await window.WebVideoCtrl.I_StartRecord(recordFileName.value, {
iWndIndex: 0, // 播放窗口号
success: () => {
isRecording.value = true;
ElMessage.success(`开始录像!文件名:${recordFileName.value}`);
},
error: (err: any) => {
throw new Error(err.errorMsg || '开始录像失败');
}
});
// 获取录像保存路径
const localCfg = await window.WebVideoCtrl.I_GetLocalCfg().catch(() => ({}));
console.log('获取到的配置信息1:', localCfg);
const recordPath = localCfg.recordPath || 'C:\\Users\\[用户名]\\AppData\\Roaming\\Hikvision\\WebVideoCtrl\\Vod';
console.log('录像保存路径:', recordPath);
} catch (error) {
const errMsg = (error as Error).message || '开始录像失败';
ElMessage.error(`开始录像失败:${errMsg}`);
console.error('开始录像错误:', error);
}
};
/**
* 停止本地录像(I_StopRecord)
*/
const stopLocalRecord = async () => {
try {
// 调用停止录像接口
await window.WebVideoCtrl.I_StopRecord({
iWndIndex: 0,
success: () => {
isRecording.value = false;
// 获取录像保存路径
window.WebVideoCtrl.I_GetLocalCfg().then((cfg: any) => {
console.log('获取到的配置信息:', cfg);
const recordPath = cfg.recordPath || '本地默认录像路径';
ElMessage.success(`停止录像成功!
文件名:${recordFileName.value}
保存路径:${recordPath}/${recordFileName.value}`);
// searchResult('2026-03-12 00:00:00', '2026-03-13 01:00:00')
});
},
error: (err: any) => {
throw new Error(err.errorMsg || '停止录像失败');
}
});
} catch (error) {
const errMsg = (error as Error).message || '停止录像失败';
ElMessage.error(`停止录像失败:${errMsg}`);
console.error('停止录像错误:', error);
// 强制重置状态(防止SDK异常导致状态不一致)
isRecording.value = false;
}
};
// ========== 录像下载:按时间下载(I_StartDownloadRecordByTime) ===
180



被折叠的 条评论
为什么被折叠?



