vue+播放rtsp流视频监控-海康摄像头
- 使用工具vue+jsencrypt+jsWebControl
- 思路
- 解决办法1:弹出新页面vue代码
- 解决办法2:采用组建的方式
使用工具vue+jsencrypt+jsWebControl
注:使用海康demo,下载地址:https://open.hikvision.com/download/5c67f20bb254d61550c2f63e?type=10
需要注册,登录。
思路
基于海康demo,在vue上实现,剔除了海康demo的jquery。
vue直接引入jsWebControl会报错!!!直接在index.html里面引入js
<script src="js/videojs-contrib-hls.min.js"></script><script src="js/jsencrypt.min.js"></script>
解决办法1:弹出新页面vue代码
刚开始将视频窗体放在dialog里面,发现无法关闭窗体,也找不到元素。最终选择 window.open方式弹出新窗体,解决的朋友可以留言。
<template><div class="webControl"><!--视频窗口展示--><div id="playWnd" class="playWnd" style="left: 109px; top: 133px;"><span v-if="loading">插件未启动,正在尝试启动,请稍候...</span></div></div>
</template><script>
export default {name: 'webControl',data: function() {return {loading: false,initCount: 0,pubKey: '',oWebControl: null,id: '',message: {ip: '',port: '',appkey: '',secret: '' }}},async mounted() {this.id = this.getQueryString('name')let res = await this.$http.get('’)//从接口请求得到所需要的ip,port,appkey,secretif (res.code == 0) {this.message = res.data} else {this.$message.error('获取数据失败: ' + res.message)}this.initPlugin()},methods: {//获取路由参数getQueryString(name) {var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')var r = window.location.search.substr(1).match(reg)if (r != null) return decodeURI(r[2])return null},//预览handelShow() {let that = thisvar cameraIndexCode = this.id //获取输入的监控点编号值,必填var streamMode = 0 //主子码流标识:0-主码流,1-子码流var transMode = 1 //传输协议:0-UDP,1-TCPvar gpuMode = 0 //是否启用GPU硬解,0-不启用,1-启用var wndId = -1 //播放窗口序号(在2x2以上布局下可指定播放窗口)cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, '')cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, '')that.oWebControl.JS_RequestInterface({funcName: 'startPreview',argument: JSON.stringify({cameraIndexCode: cameraIndexCode, //监控点编号streamMode: streamMode, //主子码流标识transMode: transMode, //传输协议gpuMode: gpuMode, //是否开启GPU硬解wndId: wndId //可指定播放窗口})})},// 创建播放实例initPlugin() {let that = this// eslint-disable-next-line no-undefthat.oWebControl = new WebControl({szPluginContainer: 'playWnd', // 指定容器idiServicePortStart: 15900, // 指定起止端口号,建议使用该值iServicePortEnd: 15909,szClassId: '23BF3B0A-2C56-4D97-9C03-0CB103AA8F11', // 用于IE10使用ActiveX的clsidcbConnectSuccess: function() {console.log('插件启动成功')this.loading = false// 创建WebControl实例成功that.oWebControl.JS_StartService('window', {// WebControl实例创建成功后需要启动服务dllPath: './VideoPluginConnect.dll' // 值"./VideoPluginConnect.dll"写死}).then(function() {// 启动插件服务成功that.oWebControl.JS_SetWindowControlCallback({// 设置消息回调cbIntegrationCallBack: that.cbIntegrationCallBack})that.oWebControl.JS_CreateWnd('playWnd', 1000, 600).then(function() {//JS_CreateWnd创建视频播放窗口,宽高可设定that.init() // 创建播放实例成功后初始化})},function() {// 启动插件服务失败})},cbConnectError: function() {// 创建WebControl实例失败that.oWebControl = nullthis.loading = true// eslint-disable-next-line no-undefWebControl.JS_WakeUp('VideoWebPlugin://') // 程序未启动时执行error函数,采用wakeup来启动程序that.initCount++if (that.initCount < 3) {setTimeout(function() {that.initPlugin()}, 3000)} else {that.$confirm('插件启动失败,请检查插件是否安装!', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning'}).then(() => {location.href = `插件没有安装,则从接口下载VideoWebPlugin.exe插件`}).catch(() => {this.$message({type: 'info',message: '已取消删除'})})}},cbConnectClose: function() {// 异常断开:bNormalClose = false// JS_Disconnect正常断开:bNormalClose = truethat.oWebControl = null}})},// 推送消息cbIntegrationCallBack() {// showCBInfo(JSON.stringify(oData.responseMsg))},//初始化init() {let that = thisthis.getPubKey(function() {// 请自行修改以下变量值 var appkey = that.message.appkey//综合安防管理平台提供的appkey,必填var secret = that.setEncrypt(that.message.secret) //综合安防管理平台提供的secret,必填var ip = that.message.ip //综合安防管理平台IP地址,必填var playMode = 0 //初始播放模式:0-预览,1-回放var port = that.message.port //综合安防管理平台端口,若启用HTTPS协议,默认443var snapDir = 'D:\\SnapDir' //抓图存储路径var videoDir = 'D:\\VideoDir' //紧急录像或录像剪辑存储路径var layout = '1x1' //playMode指定模式的布局var enableHTTPS = 1 //是否启用HTTPS协议与综合安防管理平台交互,这里总是填1var encryptedFields = 'secret' //加密字段,默认加密领域为secretvar showToolbar = 1 //是否显示工具栏,0-不显示,非0-显示var showSmart = 1 //是否显示智能信息(如配置移动侦测后画面上的线框),0-不显示,非0-显示var buttonIDs = '0,16,256,257,258,259,260,512,513,514,515,516,517,768,769' //自定义工具条按钮// 请自行修改以上变量值 that.oWebControl.JS_RequestInterface({funcName: 'init',argument: JSON.stringify({appkey: appkey, //API网关提供的appkeysecret: secret, //API网关提供的secretip: ip, //API网关IP地址playMode: playMode, //播放模式(决定显示预览还是回放界面)port: port, //端口snapDir: snapDir, //抓图存储路径videoDir: videoDir, //紧急录像或录像剪辑存储路径layout: layout, //布局enableHTTPS: enableHTTPS, //是否启用HTTPS协议encryptedFields: encryptedFields, //加密字段showToolbar: showToolbar, //是否显示工具栏showSmart: showSmart, //是否显示智能信息buttonIDs: buttonIDs //自定义工具条按钮})}).then(function() {that.oWebControl.JS_Resize(1000, 600) // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题that.handelShow()})})},//获取公钥getPubKey(callback) {let that = thisthat.oWebControl.JS_RequestInterface({funcName: 'getRSAPubKey',argument: JSON.stringify({keyLength: 1024})}).then(function(oData) {if (oData.responseMsg.data) {that.pubKey = oData.responseMsg.datacallback()}})},//RSA加密setEncrypt(value) {let that = this// eslint-disable-next-line no-undefvar encrypt = new JSEncrypt()encrypt.setPublicKey(that.pubKey)return encrypt.encrypt(value)}}
}
</script><style scoped>
.playWnd {margin: 30px 0 0 400px;width: 1000px;/*播放容器的宽和高设定*/height: 600px;
}
</style>
解决办法2:采用组建的方式
采用组件引用方式,惊喜的发现这种方式初始化时不需要过setWndCover修改定位,销毁组件使用JS_DestroyWnd,切换视频时先通过stopAllPreview停止所有预览,再次startPreview预览