WebSDK3.3.8海康插件开发+vue

使用WebSDK3.3.8海康摄像头时遇到的一些问题:

1、demo的东西太多,有用的东西拆分对我这个菜鸡来说太多了

2、webVideoCtrl.js中会对登录次数和预览进行限制,要自行更改

3、由于海康监控是以插件的形式展示的,在切换其他页面的时候画面依旧存在,必须要做销毁插件的处理

解决办法如下:

由于没怎么接触过硬件设备,这一段代码的改写我是参照了别的博主的,

视频浏览参考代码

登录限制和浏览次数的限制修改要在自带的webVideoCtrl.js中进行修改

// 不限制登录次数

        (this.I_Login = function (p, h, I, f, P, m) {

          return new Promise(function (e, t) {

            var n, i, o, r, s, a, d, c, u, l;


            // Remove the check for existing login

            // if (-1 != this.findDeviceIndexByIP(p + "_" + I)) {

            //   m.error &&

            //     m.error({

            //       errorCode: 2001,

            //       errorMsg: "The device is already logged in.",

            //     });

            //   return t({

            //     errorCode: 2001,

            //     errorMsg: "The device is already logged in.",

            //   });

            // }


            // Proceed with login even if the device is already logged in

            (l = y),

            (n = new j()),

            (i = l),

            (o = n),

            (r = p),

            (s = h),

            (a = I),

            (d = f),

            (c = P),

            (u = m),

            (l = { success: null, error: null }),

            S.extend(l, u),

            S.extend(l, {

              success: function (e) {

                O(i, o, r, s, a, d, c),

                g.push(o),

                J(o).then(() => {

                  u.success && u.success(e);

                });

              },

              error: function (e) {

                u.error && u.error(e);

              },

            }),

            i.digestLogin(r, s, a, d, c, l).then(

              () => {

                e();

              },

              (e) => {

                t(e);

              }

            );

          });

        }),

(this.I_StartRealPlay = function (o, r) {

          return new Promise((resolve, reject) => {

            var n = this.findDeviceIndexByIP(o);

            var i = {

              iWndIndex: c,

              iStreamType: 1,

              iChannelID: 1,

              bZeroChannel: false,

            };


            S.extend(i, r);


            // Remove the existing device check restriction

            if (n !== -1) {

              n = g[n];

              // Allow repeated login or playback

              n.oProtocolInc.startRealPlay(n, i).then(

                function () {

                  if (r.success) {

                    r.success();

                  }

                  resolve();

                },

                function () {

                  if (r.error) {

                    r.error(_);

                  }

                  reject(_);

                }

              );

            } else {

              if (r.error) {

                r.error(w);

              }

              reject(w);

            }

          });

        }),


        (this.I_StartPlay = function (o, r) {

          return new Promise(async function (e, t) {

            var n = this.findDeviceIndexByIP(o),

              i = { iWndIndex: c };


            S.extend(i, r);


            // Allow starting play even if it's already playing

            n = g[n];


            // Remove the check for existing playback

            n.oProtocolInc.startPlay(n, i).then(

              function () {

                r.success && r.success();

                e();

              },

              function () {

                r.error && r.error(_);

                t(_);

              }

            );

          });

        }),

处理完之后的完整demo中要使用的代码如下:

<script>

export default {

  data() {

    return {

      oLiveView: {

        iProtocol: 1, // http 协议,1 表示 http 协议 2 表示 https 协议

        szIP: window.deviceConfig.ip, // 设备的 IP 地址或者普通域名(比如花生壳域名)

        szPort: window.deviceConfig.port, // 登录设备的 http/https 端口号,根据 iPrototocol 选择传入不同的端口

        szUsername: 'admin', // 登录用户名称

        szPassword: '123456', // 登录用户密码

        iStreamType: 1, // 码流类型 1-主码流,2-子码流,默认使用主码流预览

        iChannelID: 1, // 播放通道号,默认通道 1

        bZeroChannel: false, // 是否播放零通道,默认为 false

        g_bPTZAuto: false, // PTZ自动控制状态

        g_iWndIndex: 0, // 窗口索引

        ptzSpeed: 0, // PTZ速度

      },

      isLoggedIn: false,

      operationInfo: '',

      pluginVisible: false,

    }

  },

  mounted() {

    this.initPlugin()

    this.showPlugin() // 组件挂载后显示插件

    this.pluginVisible = true

  },

  beforeDestroy() {

    this.destroyPlugin() // Destroy the plugin before the component is destroyed

    this.clickStopRealPlay()

    this.pluginVisible = false

    this.hidePlugin() // Hide plugin before component is destroyed

  },

  beforeRouteLeave(to, from, next) {

    this.pluginVisible = false

    next()

  },

  watch: {

    $route(to, from) {

      // this.pluginVisible = false;

      this.showPlugin()

    },

  },

  methods: {

    // 初始化插件参数及插入插件

    initPlugin() {

      let this_ = this

      let oLiveView = this.oLiveView

      window.WebVideoCtrl.I_InitPlugin({

        bWndFull: true, //是否支持单窗口双击全屏,默认支持 true:支持 false:不支持

        iWndowType: 1,

        cbInitPluginComplete: function () {

          console.log('初始化')

          window.WebVideoCtrl.I_InsertOBJECTPlugin('divPlugin').then(

            () => {

              // 检查插件是否最新

              window.WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {

                if (bFlag) {

                  console.log('检测到新的插件版本,双击开发包目录里的WebComponentsKit.exe升级!')

                }

                this_.clickLogin(oLiveView)

              })

            },

            () => {

              console.log(

                '插件初始化失败,请确认是否已安装插件;如果未安装,请双击开发包目录里的HCWebSDKPlugin.exe升级!'

              )

            }

          )

        },

      })

    },

    // 登录

    clickLogin(oLiveView) {

      let this_ = this

      window.WebVideoCtrl.I_Login(

        oLiveView.szIP, //设备的 IP 地址

        oLiveView.iProtocol, //http 协议,1 表示 http 协议 2 表示 https 协议

        oLiveView.szPort, //登录设备的 http/https 端口号,根据 iPrototocol 选择传入不同的端口

        oLiveView.szUsername, //登录用户名称

        oLiveView.szPassword, //用户密码

        {

          timeout: 3000,

          success: function () {

            console.log(' 登录成功!')

            this_.clickGetDigitalChannelInfo(oLiveView)

          },

          error: function (oError) {

            console.log(' 登录失败!', oError.errorCode, oError.errorMsg)

          },

        }

      )

    },

    showOPInfo(szInfo, status, xmlDoc) {

      let szTip = `<div>${this.formatDate(new Date())} ${szInfo}`

      if (typeof status !== 'undefined' && status !== 200) {

        const szStatusString = $(xmlDoc).find('statusString').eq(0).text()

        const szSubStatusCode = $(xmlDoc).find('subStatusCode').eq(0).text()

        if (szSubStatusCode === '') {

          szTip += szStatusString === '' ? `(${status})` : `(${status}, ${szStatusString})`

        } else {

          szTip += `(${status}, ${szSubStatusCode})`

        }

      }

      szTip += '</div>'

      // Update the reactive property

      this.operationInfo = szTip + this.operationInfo

    },

    formatDate(date) {

      // Your date formatting function

      return date.toISOString().slice(0, 19).replace('T', ' ') // Example format

    },

    // 获取 端口 数字通道 (必须先获取端口 数字通道 或 模拟通道 才能开始预览)

    clickGetDigitalChannelInfo(oLiveView) {

      let this_ = this

      var szDeviceIdentify = oLiveView.szIP + '_' + oLiveView.szPort

      // 获取端口

      window.WebVideoCtrl.I_GetDevicePort(szDeviceIdentify).then(

        (oPort) => {

          console.log(' 获取端口成功!')

          // 获取数字通道

          window.WebVideoCtrl.I_GetAnalogChannelInfo(szDeviceIdentify, {

            async: false,

            success: function () {

              this_.clickStartRealPlay(oLiveView)

              console.log(' 获取数字通道成功!')

            },

            error: function () {

              console.log(' 获取数字通道失败!')

            },

          })

        },

        (oError) => {

          console.log(' 获取端口失败!')

        }

      )

    },

    // 开始预览

    clickStartRealPlay(oLiveView) {

      var szDeviceIdentify = oLiveView.szIP + '_' + oLiveView.szPort

      window.WebVideoCtrl.I_StartRealPlay(szDeviceIdentify, {

        // iWndIndex: i,

        iStreamType: oLiveView.iStreamType,

        // iChannelID: i + 1,

        bZeroChannel: oLiveView.bZeroChannel,

        success: function () {

          console.log('预览成功!')

        },

        error: function (status) {

          if (403 === status) {

            console.log('设备不支持Websocket取流!')

          } else {

            console.log('预览失败!')

          }

        },

      })

    },

    // 停止预览

    clickStopRealPlay() {

      const oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      let szInfo = ''

      if (oWndInfo != null) {

        WebVideoCtrl.I_Stop({

          success: () => {

            szInfo = '停止预览成功!'

            this.showOPInfo(oWndInfo.szDeviceIdentify + ' ' + szInfo)

          },

          error: (oError) => {

            this.showOPInfo(oWndInfo.szDeviceIdentify + ' 停止预览失败!', oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

    // PTZ控制

    mouseDownPTZControl(iPTZIndex) {

      const oWndInfo = window.WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      const bZeroChannel = this.oLiveView.bZeroChannel

      if (bZeroChannel) {

        return // 零通道不支持云台

      }

      if (oWndInfo != null) {

        if (9 === iPTZIndex && this.g_bPTZAuto) {

          this.ptzSpeed = 0 // 自动开启后,速度置为0可以关闭自动

        } else {

          this.g_bPTZAuto = false // 点击其他方向,自动肯定会被关闭

        }

        window.WebVideoCtrl.I_PTZControl(iPTZIndex, false, {

          iPTZSpeed: this.ptzSpeed,

          success: (xmlDoc) => {

            const message =

              9 === iPTZIndex && this.g_bPTZAuto

                ? `${oWndInfo.szDeviceIdentify} 停止云台成功!`

                : `${oWndInfo.szDeviceIdentify} 开启云台成功!`

            console.log(message)

            if (9 === iPTZIndex) {

              this.g_bPTZAuto = !this.g_bPTZAuto // 切换自动

            }

          },

          error: (oError) => {

            console.log(`${oWndInfo.szDeviceIdentify} 开启云台失败!`, oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

    // PTZ停止

    mouseUpPTZControl() {

      const oWndInfo = window.WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      if (oWndInfo != null) {

        window.WebVideoCtrl.I_PTZControl(1, true, {

          success: (xmlDoc) => {

            console.log(`${oWndInfo.szDeviceIdentify} 停止云台成功!`)

          },

          error: (oError) => {

            console.log(`${oWndInfo.szDeviceIdentify} 停止云台失败!`, oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

    // 变倍

    PTZZoomIn() {

      const oWndInfo = window.WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      if (oWndInfo != null) {

        window.WebVideoCtrl.I_PTZControl(10, false, {

          iWndIndex: this.g_iWndIndex,

          success: (xmlDoc) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦+成功!`)

          },

          error: (oError) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦+失败!`, oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

    destroyPlugin() {

      WebVideoCtrl.I_DestroyPlugin()

        .then(() => {

          this.showOPInfo('清除成功!')

        })

        .catch(() => {

          this.showOPInfo('清除失败!')

        })

    },

//下面两个方法是对插件的显示和隐藏做处理

    showPlugin() {

      WebVideoCtrl.I_ShowPlugin()

        .then(() => {

          console.log('插件展示成功')

          this.pluginVisible = true

        })

        .catch(() => {

          console.log('插件展示失败')

        })

    },

    hidePlugin() {

      WebVideoCtrl.I_HidPlugin()

        .then(() => {

          console.log('插件展示成功')

          this.pluginVisible = false // 隐藏插件

        })

        .catch(() => {

          console.log('插件展示失败')

        })

    },

//下方是变倍的操作

    PTZZoomOut() {

      const oWndInfo = window.WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      if (oWndInfo != null) {

        window.WebVideoCtrl.I_PTZControl(11, false, {

          iWndIndex: this.g_iWndIndex,

          success: (xmlDoc) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦-成功!`)

          },

          error: (oError) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦-失败!`, oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

    PTZZoomStop() {

      const oWndInfo = window.WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex)

      if (oWndInfo != null) {

        window.WebVideoCtrl.I_PTZControl(11, true, {

          iWndIndex: this.g_iWndIndex,

          success: (xmlDoc) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦停止成功!`)

          },

          error: (oError) => {

            this.showOPInfo(`${oWndInfo.szDeviceIdentify} 调焦停止失败!`, oError.errorCode, oError.errorMsg)

          },

        })

      }

    },

  },

}

</script>

代码中包含对云台和变倍的使用,有一个问题就是修改了变倍的写法后,直接到最大值和最小值,没有找到解决办法。

ps:有个小问题,取消了限制登录和限制预览的操作后webVideoCtrl.js会一直报403和404的错,但不影响视频的出现所以我没有做修改。

取消这个限制是因为,登录操作如果连续登录会被该文件拦截,就只能登录和预览一次。

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容