vue在PC端接入监控摄像头

一.本地环境接入海康威视摄像头

参考文档:海康开放平台

本码用的是WEB3.3控制开发包 V3.3

1.下载插件

链接: 百度网盘 请输入提取码

2.安装插件

3.拷贝文件

在你的项目根目录下创建public文件夹 把下载的三个js文件拷贝进去

4.引入文件

在根目录的index.html文件 引入以下两个js文件

5.定义组件

该组件目录:src/components/MonitorVideo/index.vue

<template>
    <div ref="divPlugin" :id="divPlugin" class="plugin" style="width: 100%;height:100%;"></div>
</template>
   
   
<script>
import { nextTick } from "vue"
export default {
    props: ['sysParams', 'width', 'height'],
    data() {
        return {
            divPlugin: 'video_' + this.generateUUID(),
            g_iWndIndex: null,
            szDeviceIdentify: ''
        };
    },
    created() {
    },
    mounted() {
        nextTick(() => {
            setTimeout(() => {
                this.linkVideo();
            }, 500);
        })
    },
    beforeDestroy() {
        this.hideVideo();
    },
    methods: {
        // 初始化
        linkVideo() {
            let that = this;
            WebVideoCtrl.I_InitPlugin({
                bWndFull: true, //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持
                iWndowType: 1, //表示视频组件窗口数,默认是1,我这里需要四个窗口改为了2
                cbSelWnd: function (xmlDoc) {
                    that.g_iWndIndex = parseInt(
                        $(xmlDoc).find('SelectWnd').eq(0).text(),
                        10
                    );
                    console.log('选中窗口');
                    if (that.sysParams[that.g_iWndIndex]) {
                        that.clickStartRealPlay();
                    } else {
                        that.$message.error('当前窗口无设备');
                    }
                },
                cbDoubleClickWnd: function (iWndIndex, bFullScreen) {
                    if (!bFullScreen) {
                    }
                },
                cbEvent: function (iEventType, iParam1, iParam2) {
                    if (2 == iEventType) {
                        // 回放正常结束
                        showCBInfo('窗口' + iParam1 + '回放结束!');
                    } else if (-1 == iEventType) {
                        showCBInfo('设备' + iParam1 + '网络错误!');
                    } else if (3001 == iEventType) {
                        clickStopRecord(g_szRecordType, iParam1);
                    }
                },
                cbInitPluginComplete: function () {
                    WebVideoCtrl.I_InsertOBJECTPlugin(that.divPlugin).then(
                        () => {
                            that.sysParams.map((item, index) => {
                                setTimeout(() => {
                                    that.g_iWndIndex = index;
                                    that.clickStartRealPlay();
                                }, index * 1000);
                            });
                        },
                        () => {
                            alert("插件初始化失败,请确认是否已安装插件;如果未安装,请双击开发包目录里的HCWebSDKPlugin.exe安装!")
                            console.log("插件初始化失败,请确认是否已安装插件;如果未安装,请双击开发包目录里的HCWebSDKPlugin.exe安装!");
                        }
                    );
                }
            });
        },
        // 登录
        async clickStartRealPlay() {
            let that = this;
            var oWndInfo = WebVideoCtrl.I_GetWindowStatus(that.g_iWndIndex);
            let { szIP, szPort, szUsername, szPassword } =
                this.sysParams[that.g_iWndIndex];
            that.szDeviceIdentify = szIP + '_' + szPort;
            if (oWndInfo == null) {
                WebVideoCtrl.I_Login(szIP, 1, szPort, szUsername, szPassword, {
                    success: function (xmlDoc) {
                        //成功的回调函数
                        that.getVideo();
                    },
                    error: function (oError) {
                        if (oError.errorCode == '2001') {
                            that.getVideo();
                        }
                        //失败的回调函数
                    }
                });
            } else {
                await WebVideoCtrl.I_Stop(that.g_iWndIndex);
                that.getVideo();
            }
        },
        // 打开预览视频
        getVideo() {
            console.log('渲染第' + this.g_iWndIndex + '窗口');
            WebVideoCtrl.I_StartRealPlay(this.szDeviceIdentify, {
                iWndIndex: this.g_iWndIndex,
                success: function () { },
                error: function (oError) { }
            });
        },
        // 关闭销毁
        async hideVideo() {
            await WebVideoCtrl?.I_StopAllPlay();
            await WebVideoCtrl?.I_Logout(this.szDeviceIdentify);
            await WebVideoCtrl?.I_DestroyPlugin();
        },
        // 生成uuid
        generateUUID() {
            let d = new Date().getTime();
            let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
                /[xy]/g,
                (c) => {
                    let r = (d + Math.random() * 16) % 16 | 0;
                    d = Math.floor(d / 16);
                    return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16);
                }
            );
            return uuid;
        }
    },
    watch: {
        sysParams: {
            handler(newVal, oldVal) {
                // 当items数组发生变化时,执行这里的逻辑
                console.log('items 数组发生变化:', newVal, oldVal);
                // this.hideVideo();
                WebVideoCtrl.I_StopAllPlay();
                // this.linkVideo();
                this.sysParams.map((item, index) => {
                    setTimeout(() => {
                        this.g_iWndIndex = index;
                        this.clickStartRealPlay();
                    }, index * 400);
                });
            },
            deep: true
        }
    }
};
</script>
   
<style lang='scss' scoped>
.plugin {}
</style>

6.页面使用

<template>
	<div class="layout-padding">
		<div class="layout-padding-view layout-padding-auto">
			<div class="surveillanceVideo">
				<!-- 本地的 -->
				<MonitorVideo v-if="mode === 'development' && videoShow" :sysParams="list" ref="monitorVideoRef">
				</MonitorVideo>
				<!-- 线上的 -->
				<!-- <PlayerVideo v-else-if="mode === 'production' && videoShow" ref="playerVideoRef">
				</PlayerVideo> -->
			</div>
		</div>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, defineComponent, getCurrentInstance, onDeactivated, onActivated, onBeforeUnmount, } from 'vue';
import MonitorVideo from "/@/components/MonitorVideo/index.vue"
// import PlayerVideo from "/@/components/PlayerVideo/index.vue"
export default defineComponent({
	components: {
		MonitorVideo,
		// PlayerVideo
	},
	setup() {
		const data = reactive({
			videoShow: false,
			list: [{
				szIP: '192.168.0.15', // ip地址
				szPort: '80', // 端口号
				szUsername: 'admin', // 账号
				szPassword: '密码' // 密码
			}],
			// mode: import.meta.env.MODE,
			mode: "development",
		});

		// 页面加载时
		onMounted(async () => {
			console.log("onMounted");
			openVideoShow()
		});

		onBeforeUnmount(() => {
			console.log("onBeforeUnmount");
			closeVideoShow()
		})


		onActivated(() => {
			console.log("onActivated");
			openVideoShow()
		})
		onDeactivated(() => {
			console.log("onDeactivated");
			closeVideoShow()
		})

		const { proxy }: any = getCurrentInstance();

		const openVideoShow = () => {
			data.videoShow = true
		}

		const closeVideoShow = () => {
			proxy.$refs.monitorVideoRef?.hideVideo().catch((err: any) => {
			})
			data.videoShow = false
		}

		return {
			...toRefs(data),
		};
	},
});
</script>

<style  lang="scss" scoped>
.surveillanceVideo {
	width: 600px;
	height: 400px;
}
</style>

效果图

二.生产环境(线上)接入萤石云监控

参考文档:文档概述 · 萤石开放平台API文档

1.安装依赖

npm i  ezuikit-js

2.定义组件

该组件目录:src/components/PlayerVideo/index.vue

<template>
	<div id="video-container" style="width: 100%;"></div>
</template>
<script lang="ts">
import { reactive, getCurrentInstance, toRefs, onMounted, nextTick, defineComponent } from 'vue';
import EZUIKit from 'ezuikit-js';

var player = null as any;
export default defineComponent({
	name: 'PlayerVideo',
	setup(props: any, { emit }: any) {
		var themeData = {
			"header": {
				"color": "#1890ff",
				"activeColor": "#FFFFFF",
				"backgroundColor": "#000000",
				"btnList": [
					{
						"iconId": "deviceID",
						"part": "left",
						"defaultActive": 0,
						"memo": "顶部设备序列号",
						"isrender": 1
					},
					{
						"iconId": "deviceName",
						"part": "left",
						"defaultActive": 0,
						"memo": "顶部设备名称",
						"isrender": 1
					},
					{
						"iconId": "cloudRec",
						"part": "right",
						"defaultActive": 0,
						"memo": "云存储",
						"isrender": 0
					},
					{
						"iconId": "rec",
						"part": "right",
						"defaultActive": 0,
						"memo": "SD卡回放",
						"isrender": 0
					}
				]
			},
			"footer": {
				"color": "#FFFFFF",
				"activeColor": "#1890FF",
				"backgroundColor": "#00000021",
				"btnList": [
					{
						"iconId": "play",
						"part": "left",
						"defaultActive": 1,
						"memo": "播放",
						"isrender": 1
					},
					{
						"iconId": "capturePicture",
						"part": "left",
						"defaultActive": 0,
						"memo": "截屏按钮",
						"isrender": 1
					},
					{
						"iconId": "sound",
						"part": "left",
						"defaultActive": 0,
						"memo": "声音按钮",
						"isrender": 1
					},
					{
						"iconId": "pantile",
						"part": "left",
						"defaultActive": 0,
						"memo": "云台控制按钮",
						"isrender": 1
					},
					{
						"iconId": "recordvideo",
						"part": "left",
						"defaultActive": 0,
						"memo": "录制按钮",
						"isrender": 1
					},
					{
						"iconId": "talk",
						"part": "left",
						"defaultActive": 0,
						"memo": "对讲按钮",
						"isrender": 1
					},
					{
						"iconId": "zoom",
						"part": "left",
						"defaultActive": 0,
						"memo": "电子放大",
						"isrender": 1
					},
					{
						"iconId": "hd",
						"part": "right",
						"defaultActive": 0,
						"memo": "清晰度切换按钮",
						"isrender": 1
					},
					{
						"iconId": "webExpend",
						"part": "right",
						"defaultActive": 0,
						"memo": "网页全屏按钮",
						"isrender": 1
					},
					{
						"iconId": "expend",
						"part": "right",
						"defaultActive": 0,
						"memo": "全局全屏按钮",
						"isrender": 1
					}
				]
			}
		};


		onMounted(() => {
			nextTick(() => {
				initPlayer()
			});
		});

		const { proxy } = getCurrentInstance() as any;

		const data = reactive({

		});

		const initPlayer = async () => {
				player = new EZUIKit.EZUIKitPlayer({
					id: 'video-container', // 视频容器ID
					accessToken: "at.67jjm3xg1nqavrv3ch6wuhgl3hjzwvly-6t6ynn896j-0ronkpk-ci0ef9om3",
					url: "ezopen://open.ys7.com/FA6803244/1.hd.live",
					// simple - 极简版; pcLive-pc直播;pcRec-pc回放;	mobileLive-移动端直播;mobileRec-移动端回放;security - 安防版;voice-语音版;
					themeData: themeData,
				})
		}

		const destroyPlayer = () => {
			if (player) {
				player.stop();
				player.destroy();
				player = null;
			}
		}

		return { ...toRefs(data), destroyPlayer }
	}
})
</script>
<style scoped lang="scss"></style>

3.页面使用

<template>
	<div class="layout-padding">
		<div class="layout-padding-view layout-padding-auto">
			<div class="surveillanceVideo">
				<!-- 本地的 -->
				<!-- <MonitorVideo v-if="mode === 'development' && videoShow" :sysParams="list" ref="monitorVideoRef">
				</MonitorVideo> -->
				<!-- 线上的 -->
				<PlayerVideo v-if="mode === 'production' && videoShow" ref="playerVideoRef">
				</PlayerVideo>
			</div>
		</div>
	</div>
</template>

<script lang="ts">
import { toRefs, reactive, onMounted, defineComponent, getCurrentInstance, onDeactivated, onActivated, onBeforeUnmount, } from 'vue';
// import MonitorVideo from "/@/components/MonitorVideo/index.vue"
import PlayerVideo from "/@/components/PlayerVideo/index.vue"
export default defineComponent({
	components: {
		// MonitorVideo,
		PlayerVideo
	},
	setup() {
		const data = reactive({
			videoShow: false,
			list: [{
				szIP: '192.168.0.15',
				szPort: '80',
				szUsername: 'admin',
				szPassword: 'Qjy16888'
			}],
			// mode: import.meta.env.MODE,
			mode: "production",
		});

		// 页面加载时
		onMounted(async () => {
			console.log("onMounted");
			openVideoShow()
		});

		onBeforeUnmount(() => {
			console.log("onBeforeUnmount");
			closeVideoShow()
		})


		onActivated(() => {
			console.log("onActivated");
			openVideoShow()
		})
		onDeactivated(() => {
			console.log("onDeactivated");
			closeVideoShow()
		})

		const { proxy }: any = getCurrentInstance();

		const openVideoShow = () => {
			data.videoShow = true
		}

		const closeVideoShow = () => {
			// proxy.$refs.monitorVideoRef?.hideVideo().catch((err: any) => {
			// })
			data.videoShow = false
		}

		return {
			...toRefs(data),
		};
	},
});
</script>

<style  lang="scss" scoped>
.surveillanceVideo {
	width: 600px;
	height: 400px;
}
</style>

效果图

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值