vue+播放rtsp流视频监控

使用工具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,secret
    if (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 = this
      var cameraIndexCode = this.id //获取输入的监控点编号值,必填
      var streamMode = 0 //主子码流标识:0-主码流,1-子码流
      var transMode = 1 //传输协议:0-UDP,1-TCP
      var 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-undef
      that.oWebControl = new WebControl({
        szPluginContainer: 'playWnd', // 指定容器id
        iServicePortStart: 15900, // 指定起止端口号,建议使用该值
        iServicePortEnd: 15909,
        szClassId: '23BF3B0A-2C56-4D97-9C03-0CB103AA8F11', // 用于IE10使用ActiveX的clsid
        cbConnectSuccess: 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 = null
          this.loading = true
          // eslint-disable-next-line no-undef
          WebControl.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 = true
          that.oWebControl = null
        }
      })
    },
    // 推送消息
    cbIntegrationCallBack() {
      // showCBInfo(JSON.stringify(oData.responseMsg))
    },
    //初始化
    init() {
      let that = this
      this.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协议,默认443
        var snapDir = 'D:\\SnapDir' //抓图存储路径
        var videoDir = 'D:\\VideoDir' //紧急录像或录像剪辑存储路径
        var layout = '1x1' //playMode指定模式的布局
        var enableHTTPS = 1 //是否启用HTTPS协议与综合安防管理平台交互,这里总是填1
        var encryptedFields = 'secret' //加密字段,默认加密领域为secret
        var 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网关提供的appkey
              secret: secret, //API网关提供的secret
              ip: 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 = this
      that.oWebControl
        .JS_RequestInterface({
          funcName: 'getRSAPubKey',
          argument: JSON.stringify({
            keyLength: 1024
          })
        })
        .then(function(oData) {
          if (oData.responseMsg.data) {
            that.pubKey = oData.responseMsg.data
            callback()
          }
        })
    },
    //RSA加密
    setEncrypt(value) {
      let that = this
      // eslint-disable-next-line no-undef
      var 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预览

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值