当前位置:   article > 正文

vue2和3 对接 海康摄像头插件 (视频WEB插件 V1.5.2)_视频web插件 v1.5.3

视频web插件 v1.5.3

前言

海康视频插件v.1.5.2版本运行环境需要安装插件VideoWebPlugin.exe,对浏览器也有兼容性要求,具体看官方文档

功能

1. 摄像头的预览与停止预览

2. 窗口布局设置

3. 页面多次调用组件唯一性

4. 初始单个预览与多个预览

5. 组件窗口自适应

6. 双击窗口全屏

对应下载插件

海康官网下载插件 里面有dome等其他需要用到的

地址:

安装插件

打开下载的文件里的bin文件 安装一下VideoWebPlugin

vue脚手架中集成插件

把官方资源包里的query-1.12.4.min.js、jsencrypt.min.js、web-control_1.2.5.min.js复制到vue项目public目录下,在index.html引入js

封装组件(vue2)

        代码:

  1. <!--
  2. 只实现预览摄像头功能
  3. 方法调用
  4. 预览监控点的方法调用 可以传值 值为监控点
  5. this.$refs.hikVideo.previewVideo('333');
  6. 停止全部预览
  7. this.$refs.hikVideo.stopAllPreview();
  8. 窗口布局
  9. this.$refs.hikVideo.setLayout('1x1');
  10. 注意:
  11. 如果要实现 点击播放摄像头 前提是组件已经在页面显示出来
  12. 例子:
  13. this.$refs.hikVideo.previewVideo(data); data为监控点编号
  14. -->
  15. <template>
  16. <div :id="id" class="playWnd" ref="playWndBox"></div>
  17. </template>
  18. <script>
  19. export default {
  20. name: 'HikVideo',
  21. props: {
  22. objData: {
  23. type: Object,
  24. required: true
  25. },
  26. cameraIndexCodes: {
  27. type: Array,
  28. required: true
  29. },
  30. // 初始视频显示画面个数
  31. cameraQuantity: {
  32. type: Number,
  33. default: 1
  34. }
  35. },
  36. data() {
  37. return {
  38. id: 'playWnd' + Math.random().toString(16).slice(2),
  39. // 视频盒子的高度
  40. playWndHeight: '',
  41. // 视频盒子的宽度
  42. playWndWidth: '',
  43. oWebControl: null,
  44. initCount: 0,
  45. pubKey: '',
  46. // 窗口编号
  47. wndId: 0
  48. }
  49. },
  50. mounted() {
  51. // 初始化播放器插件
  52. this.$nextTick(() => {
  53. // 首次加载时的到父容器的高度
  54. this.getWndCover()
  55. console.log('resize', this.playWndWidth, this.playWndHeight)
  56. // console.log(this.oWebControl)
  57. this.initPlugin()
  58. })
  59. // 监听scroll事件,使插件窗口尺寸跟随DIV窗口变化
  60. window.addEventListener('scroll', () => {
  61. if (this.oWebControl != null) {
  62. this.getWndCover()
  63. this.oWebControl.JS_Resize(this.playWndWidth, this.playWndHeight)
  64. console.log('scroll')
  65. this.setWndCover()
  66. }
  67. })
  68. // 监听resize事件,使插件窗口尺寸跟随DIV窗口变化
  69. this.monResize()
  70. },
  71. destroyed() {
  72. if (this.oWebControl != null) {
  73. // 先让窗口隐藏,规避可能的插件窗口滞后于浏览器消失问题
  74. this.oWebControl.JS_HideWnd()
  75. // // 销毁当前播放的视频
  76. // this.oWebControl.JS_RequestInterface({ funcName: 'destroyWnd' })
  77. // // 断开与插件服务连接
  78. // this.oWebControl.JS_Disconnect()
  79. }
  80. },
  81. methods: {
  82. monResize() {
  83. window.addEventListener('resize', (e) => {
  84. if (this.oWebControl != null) {
  85. this.getWndCover()
  86. console.log('resize', this.playWndWidth, this.playWndHeight)
  87. this.oWebControl.JS_Resize(this.playWndWidth, this.playWndHeight)
  88. this.setWndCover()
  89. }
  90. })
  91. },
  92. // 获取宽高
  93. getWndCover() {
  94. // 首次加载时的到父容器的高度
  95. this.playWndHeight = this.$refs.playWndBox.clientHeight
  96. // 首次加载时的到父容器的宽度
  97. this.playWndWidth = this.$refs.playWndBox.clientWidth
  98. },
  99. // 创建播放实例
  100. initPlugin() {
  101. console.log('创建播放实例')
  102. let that = this
  103. this.oWebControl = null
  104. that.oWebControl = new WebControl({
  105. szPluginContainer: this.id, // 指定容器id
  106. iServicePortStart: 15900, // 指定起止端口号,建议使用该值
  107. iServicePortEnd: 15901,
  108. szClassId: '23BF3B0A-2C56-4D97-9C03-0CB103AA8F11', // 用于IE10使用ActiveX的clsid
  109. cbConnectSuccess: () => {
  110. // 创建WebControl实例成功
  111. that.oWebControl
  112. .JS_StartService('window', {
  113. // WebControl实例创建成功后需要启动服务
  114. // 值"./VideoPluginConnect.dll"写死
  115. dllPath: './VideoPluginConnect.dll'
  116. })
  117. .then(
  118. function () {
  119. // 设置消息回调
  120. that.oWebControl.JS_SetWindowControlCallback({
  121. cbIntegrationCallBack: that.cbIntegrationCallBack
  122. })
  123. //JS_CreateWnd创建视频播放窗口,宽高可设定
  124. that.oWebControl
  125. .JS_CreateWnd(
  126. that.id,
  127. that.playWndWidth,
  128. that.playWndHeight,
  129. { bEmbed: true }
  130. )
  131. //注:that.playWndWidth, that.playWndHeight这是上边已经获取的父元素的宽高,你们要调整大小可以更改父元素大小,不然初始化插件的时候会有空白闪烁。
  132. .then(function () {
  133. // 创建播放实例成功后初始化
  134. that.init()
  135. })
  136. },
  137. function () {
  138. // 启动插件服务失败
  139. console.log('启动插件服务失败')
  140. }
  141. )
  142. },
  143. // 创建WebControl实例失败
  144. cbConnectError: function () {
  145. that.oWebControl = null
  146. console.log('插件未启动,正在尝试启动,请稍候...')
  147. that.$message.warning('插件未启动,正在尝试启动,请稍候...')
  148. // 程序未启动时执行error函数,采用wakeup来启动程序
  149. window.WebControl.JS_WakeUp('VideoWebPlugin://')
  150. that.initCount++
  151. if (that.initCount < 3) {
  152. setTimeout(function () {
  153. that.initPlugin()
  154. }, 3000)
  155. } else {
  156. console.log('插件启动失败,请检查插件是否安装!')
  157. that.$message.warning('插件启动失败,请检查插件是否安装!')
  158. }
  159. },
  160. cbConnectClose: () => {
  161. // 异常断开:bNormalClose = false
  162. // JS_Disconnect正常断开:bNormalClose = true
  163. // console.log("cbConnectClose");
  164. that.oWebControl = null
  165. }
  166. })
  167. },
  168. hide() {
  169. this.oWebControl && this.oWebControl.JS_HideWnd()
  170. },
  171. // 初始化
  172. init(callback) {
  173. console.log('初始化')
  174. let that = this
  175. that.getPubKey(() => {
  176. let appkey = that.objData.appkey //综合安防管理平台提供的appkey,必填
  177. let secret = that.setEncrypt(that.objData.secret) //综合安防管理平台提供的secret,必填
  178. let ip = that.objData.ip //综合安防管理平台IP地址,必填
  179. let playMode = that.objData.playMode //初始播放模式:0-预览,1-回放
  180. let port = that.objData.port //综合安防管理平台端口,若启用HTTPS协议,默认443
  181. let snapDir = 'D:\\SnapDir' //抓图存储路径
  182. let videoDir = 'D:\\VideoDir' //紧急录像或录像剪辑存储路径
  183. let layout = that.objData.layout //playMode指定模式的布局
  184. let enableHTTPS = 1 //是否启用HTTPS协议与综合安防管理平台交互,这里总是填1
  185. let encryptedFields = 'secret' //加密字段,默认加密领域为secret
  186. let showToolbar = that.objData.showToolbar //是否显示工具栏,0-不显示,非0-显示
  187. let showSmart = 1 //是否显示智能信息(如配置移动侦测后画面上的线框),0-不显示,非0-显示
  188. let buttonIDs =
  189. '0,16,256,257,258,259,260,512,513,514,515,516,517,768,769' //自定义工具条按钮
  190. // var toolBarButtonIDs = "2049,2304" // 工具栏上自定义按钮
  191. that.oWebControl
  192. .JS_RequestInterface({
  193. funcName: 'init',
  194. argument: JSON.stringify({
  195. appkey: appkey, //API网关提供的appkey
  196. secret: secret, //API网关提供的secret
  197. ip: ip, //API网关IP地址
  198. playMode: playMode, //播放模式(决定显示预览还是回放界面)
  199. port: port, //端口
  200. snapDir: snapDir, //抓图存储路径
  201. videoDir: videoDir, //紧急录像或录像剪辑存储路径
  202. layout: layout, //布局
  203. enableHTTPS: enableHTTPS, //是否启用HTTPS协议
  204. encryptedFields: encryptedFields, //加密字段
  205. showToolbar: showToolbar, //是否显示工具栏
  206. showSmart: showSmart, //是否显示智能信息
  207. buttonIDs //自定义工具条按钮
  208. })
  209. })
  210. .then(function (oData) {
  211. that.oWebControl.JS_Resize(that.playWndWidth, that.playWndHeight) // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题
  212. if (callback) {
  213. callback()
  214. }
  215. // 隐藏
  216. // that.oWebControl.JS_HideWnd()
  217. that.hide()
  218. for (let i = 0; i < that.cameraIndexCodes.length; i++) {
  219. console.log(that.cameraQuantity, i)
  220. if (i <= that.cameraQuantity - 1) {
  221. that.previewVideo(that.cameraIndexCodes[i], i + 1)
  222. }
  223. }
  224. })
  225. })
  226. },
  227. // 获取公钥
  228. getPubKey(callback) {
  229. // console.log('获取公钥')
  230. let that = this
  231. this.oWebControl
  232. .JS_RequestInterface({
  233. funcName: 'getRSAPubKey',
  234. argument: JSON.stringify({
  235. keyLength: 1024
  236. })
  237. })
  238. .then(function (oData) {
  239. if (oData.responseMsg.data) {
  240. that.pubKey = oData.responseMsg.data
  241. callback()
  242. }
  243. })
  244. },
  245. // RSA 加密
  246. setEncrypt(value) {
  247. let that = this
  248. let encrypt = new window.JSEncrypt()
  249. encrypt.setPublicKey(that.pubKey)
  250. return encrypt.encrypt(value)
  251. },
  252. // 回调的消息
  253. cbIntegrationCallBack(oData) {
  254. if (oData.responseMsg.type === 1) {
  255. this.wndId = oData.responseMsg.msg.wndId // 获取窗口序号
  256. console.log('当前选中窗口', oData.responseMsg.msg.wndId)
  257. } else if (oData.responseMsg.type === 7) {
  258. this.doubleClick()
  259. }
  260. },
  261. // 双击全屏
  262. doubleClick() {
  263. this.oWebControl
  264. .JS_RequestInterface({
  265. funcName: 'setFullScreen'
  266. })
  267. .then(function (oData) {
  268. console.log(oData)
  269. // showCBInfo(JSON.stringify(oData ? oData.responseMsg : ''))
  270. })
  271. },
  272. // 布局
  273. setLayout(layout) {
  274. this.oWebControl.JS_RequestInterface({
  275. funcName: 'setLayout',
  276. argument: {
  277. layout: layout //布局
  278. }
  279. })
  280. },
  281. // 视频预览功能
  282. previewVideo(data, i) {
  283. console.log('预览画面', data, i)
  284. this.oWebControl.JS_ShowWnd()
  285. let that = this
  286. let cameraIndexCode = data // 获取输入的监控点编号值,必填
  287. // if (data == undefined) {
  288. // cameraIndexCode = that.cameraIndexCode
  289. // } else {
  290. // cameraIndexCode = data
  291. // }
  292. // console.log('监控点编号值', cameraIndexCode)
  293. let streamMode = 0 // 主子码流标识:0-主码流,1-子码流
  294. let transMode = 0 // 传输协议:0-UDP,1-TCP
  295. let gpuMode = 0 // 是否启用GPU硬解,0-不启用,1-启用
  296. let wndId = i ? i : that.wndId // 播放窗口序号(在2x2以上布局下可指定播放窗口)
  297. that.oWebControl.JS_RequestInterface({
  298. funcName: 'startPreview',
  299. argument: JSON.stringify({
  300. cameraIndexCode: cameraIndexCode.trim(), // 监控点编号
  301. streamMode: streamMode, // 主子码流标识
  302. transMode: transMode, // 传输协议
  303. gpuMode: gpuMode, // 是否开启GPU硬解
  304. wndId: wndId // 可指定播放窗口
  305. })
  306. })
  307. },
  308. // 停止全部预览
  309. stopAllPreview() {
  310. // console.log('停止全部预览')
  311. this.oWebControl.JS_RequestInterface({
  312. funcName: 'stopAllPreview'
  313. })
  314. },
  315. // 格式化时间
  316. dateFormat(oDate, fmt) {
  317. let o = {
  318. 'M+': oDate.getMonth() + 1, //月份
  319. 'd+': oDate.getDate(), //日
  320. 'h+': oDate.getHours(), //小时
  321. 'm+': oDate.getMinutes(), //分
  322. 's+': oDate.getSeconds(), //秒
  323. 'q+': Math.floor((oDate.getMonth() + 3) / 3), //季度
  324. S: oDate.getMilliseconds() //毫秒
  325. }
  326. if (/(y+)/.test(fmt)) {
  327. fmt = fmt.replace(
  328. RegExp.$1,
  329. (oDate.getFullYear() + '').substr(4 - RegExp.$1.length)
  330. )
  331. }
  332. for (let k in o) {
  333. if (new RegExp('(' + k + ')').test(fmt)) {
  334. fmt = fmt.replace(
  335. RegExp.$1,
  336. RegExp.$1.length == 1
  337. ? o[k]
  338. : ('00' + o[k]).substr(('' + o[k]).length)
  339. )
  340. }
  341. }
  342. return fmt
  343. },
  344. // 设置窗口裁剪,当因滚动条滚动导致窗口需要被遮住的情况下需要JS_CuttingPartWindow部分窗口
  345. setWndCover() {
  346. var iWidth = $(window).width()
  347. var iHeight = $(window).height()
  348. var oDivRect = $(`#${this.id}`).get(0).getBoundingClientRect()
  349. var iCoverLeft = oDivRect.left < 0 ? Math.abs(oDivRect.left) : 0
  350. var iCoverTop = oDivRect.top < 0 ? Math.abs(oDivRect.top) : 0
  351. var iCoverRight =
  352. oDivRect.right - iWidth > 0 ? Math.round(oDivRect.right - iWidth) : 0
  353. var iCoverBottom =
  354. oDivRect.bottom - iHeight > 0
  355. ? Math.round(oDivRect.bottom - iHeight)
  356. : 0
  357. iCoverLeft = iCoverLeft > 2041 ? 2041 : iCoverLeft
  358. iCoverTop = iCoverTop > 945 ? 945 : iCoverTop
  359. iCoverRight = iCoverRight > 2041 ? 2041 : iCoverRight
  360. iCoverBottom = iCoverBottom > 945 ? 945 : iCoverBottom
  361. this.oWebControl.JS_RepairPartWindow(0, 0, 2041, 946) // 多1个像素点防止还原后边界缺失一个像素条
  362. if (iCoverLeft != 0) {
  363. this.oWebControl.JS_CuttingPartWindow(0, 0, iCoverLeft, 946)
  364. }
  365. if (iCoverTop != 0) {
  366. this.oWebControl.JS_CuttingPartWindow(0, 0, 2041, iCoverTop) // 多剪掉一个像素条,防止出现剪掉一部分窗口后出现一个像素条
  367. }
  368. if (iCoverRight != 0) {
  369. this.oWebControl.JS_CuttingPartWindow(
  370. 2041 - iCoverRight,
  371. 0,
  372. iCoverRight,
  373. 946
  374. )
  375. }
  376. if (iCoverBottom != 0) {
  377. this.oWebControl.JS_CuttingPartWindow(
  378. 0,
  379. 946 - iCoverBottom,
  380. 2041,
  381. iCoverBottom
  382. )
  383. }
  384. }
  385. }
  386. }
  387. </script>
  388. <style>
  389. .playWnd {
  390. width: 100%;
  391. height: 100%;
  392. }
  393. </style>

引用组件

  1. <template>
  2. <div style="width: 100%; height: 100%">
  3. <div style="width: 50vw; height: 40vh">
  4. <HikVideo
  5. :cameraIndexCodes="cameraIndexCodes"
  6. :objData="objData"
  7. :cameraQuantity="2"
  8. ref="hikVideo"
  9. >
  10. </HikVideo>
  11. </div>
  12. <button @click="previewVideo">预览视频</button>
  13. <button @click="stopAllPreview">停止所有预览</button>
  14. <!-- <div style="width: 30vw; height: 20vh">
  15. <HikVideo
  16. :cameraIndexCodes="cameraIndexCodes"
  17. :objData="objData"
  18. ref="hikVideo"
  19. >
  20. </HikVideo>
  21. </div> -->
  22. </div>
  23. </template>
  24. <script>
  25. import HikVideo from './VideoMain.vue'
  26. export default {
  27. name: 'ParentComponent',
  28. components: {
  29. HikVideo
  30. },
  31. data() {
  32. return {
  33. objData: {
  34. //海康初始化数据
  35. appkey: '', //综合安防管理平台提供的appkey,必填
  36. ip: '', //综合安防管理平台IP地址,必填
  37. secret: '', //综合安防管理平台提供的secret,必填
  38. port: 443, //综合安防管理平台端口,若启用HTTPS协议,默认443
  39. playMode: 0, // 0 预览 1回放
  40. layout: '2x2', //页面展示的模块数【16】
  41. showToolbar: 1 //是否显示工具栏,0-不显示,非0-显示
  42. },
  43. // 多个监控
  44. cameraIndexCodes: [
  45. '',
  46. '',
  47. ''
  48. ],
  49. // 初始预览的监控个数(注意不能比窗口数量多)
  50. cameraQuantity: 2,
  51. }
  52. },
  53. mounted() {},
  54. methods: {
  55. previewVideo() {
  56. this.$refs.hikVideo.previewVideo('ba282eb0f82543a980fd50cabe035824')
  57. },
  58. stopAllPreview() {
  59. this.$refs.hikVideo.stopAllPreview()
  60. }
  61. }
  62. }
  63. </script>
  64. <style></style>

vue3 代码:

  1. <!-- 海康威视组件 -->
  2. <template>
  3. <div :id="state.id" class="playWnd"></div>
  4. </template>
  5. <script setup lang="ts">
  6. import {
  7. ref,
  8. reactive,
  9. onMounted,
  10. onDeactivated,
  11. onActivated,
  12. onUnmounted
  13. } from 'vue'
  14. const WebControl = window.WebControl //index.html引入 直接从window里面拿到sdk
  15. const JSEncrypt = window.JSEncrypt //index.html引入 直接从window里面拿到sdk
  16. const props = defineProps({
  17. wsUrl: {
  18. type: String //视频监控code
  19. },
  20. objData: {}
  21. })
  22. const objData = props.objData
  23. console.log(props.wsUrl, '====>wsurl')
  24. const state = reactive({
  25. id: 'playWnd' + Math.random().toString(16).slice(2), //多个监控同时显示需要不同的id
  26. idWidth: 0 as any,
  27. idHeight: 0 as any,
  28. initCount: 0,
  29. pubKey: '',
  30. oWebControl: null as any
  31. })
  32. console.log(state)
  33. //创建播放实例
  34. const initPlugin = () => {
  35. state.oWebControl = new WebControl({
  36. szPluginContainer: state.id, // 指定容器id
  37. iServicePortStart: 15900, // 指定起止端口号,建议使用该值
  38. iServicePortEnd: 15900,
  39. szClassId: '23BF3B0A-2C56-4D97-9C03-0CB103AA8F11', // 用于IE10使用ActiveX的clsid
  40. cbConnectSuccess: function () {
  41. // 创建WebControl实例成功
  42. state.oWebControl
  43. .JS_StartService('window', {
  44. // WebControl实例创建成功后需要启动服务
  45. dllPath: './VideoPluginConnect.dll' // 值"./VideoPluginConnect.dll"写死
  46. })
  47. .then(
  48. () => {
  49. console.log('启动成功')
  50. // 启动插件服务成功
  51. state.oWebControl.JS_SetWindowControlCallback({
  52. // 设置消息回调
  53. cbIntegrationCallBack: cbIntegrationCallBack
  54. })
  55. state.oWebControl
  56. .JS_CreateWnd(state.id, state.idWidth, state.idHeight)
  57. .then(() => {
  58. //JS_CreateWnd创建视频播放窗口,宽高可设定
  59. init() // 创建播放实例成功后初始化
  60. })
  61. },
  62. function () {
  63. console.log('启动失败')
  64. // 启动插件服务失败
  65. }
  66. )
  67. },
  68. cbConnectError: function () {
  69. // 创建WebControl实例失败
  70. state.oWebControl = null
  71. console.log('插件未启动,正在尝试启动,请稍候...')
  72. WebControl.JS_WakeUp('VideoWebPlugin://') // 程序未启动时执行error函数,采用wakeup来启动程序
  73. state.initCount++
  74. if (state.initCount < 3) {
  75. setTimeout(function () {
  76. initPlugin()
  77. }, 3000)
  78. } else {
  79. console.log('插件启动失败,请检查插件是否安装!')
  80. }
  81. },
  82. cbConnectClose: function (bNormalClose) {
  83. // 异常断开:bNormalClose = false
  84. // JS_Disconnect正常断开:bNormalClose = true
  85. console.log('cbConnectClose')
  86. state.oWebControl = null
  87. console.log('插件未启动,正在尝试启动,请稍候...')
  88. WebControl.JS_WakeUp('VideoWebPlugin://')
  89. state.initCount++
  90. if (state.initCount < 3) {
  91. setTimeout(function () {
  92. initPlugin()
  93. }, 3000)
  94. } else {
  95. console.log('插件启动失败,请检查插件是否安装!')
  96. }
  97. }
  98. })
  99. }
  100. // 设置窗口控制回调
  101. const setCallbacks = () => {
  102. state.oWebControl.JS_SetWindowControlCallback({
  103. cbIntegrationCallBack: cbIntegrationCallBack
  104. })
  105. }
  106. // 推送消息
  107. const cbIntegrationCallBack = (oData) => {
  108. console.log(oData.responseMsg, '--oData')
  109. if (oData.responseMsg.type === 7) {
  110. doubleClick()
  111. }
  112. }
  113. // 双击窗口放大
  114. const doubleClick = () => {
  115. state.oWebControl
  116. .JS_RequestInterface({
  117. funcName: 'setFullScreen'
  118. })
  119. .then(function (oData) {
  120. console.log(oData)
  121. // showCBInfo(JSON.stringify(oData ? oData.responseMsg : ''))
  122. })
  123. }
  124. //初始化
  125. const init = () => {
  126. getPubKey(() => {
  127. // 请自行修改以下变量值
  128. let appkey = objData.appkey //综合安防管理平台提供的appkey,必填
  129. let secret = setEncrypt(objData.secret) //综合安防管理平台提供的secret,必填
  130. let ip = objData.ip //综合安防管理平台IP地址,必填
  131. let playMode = objData.playMode //初始播放模式:0-预览,1-回放
  132. let port = objData.port //综合安防管理平台端口,若启用HTTPS协议,默认443
  133. let snapDir = 'D:\\SnapDir' //抓图存储路径
  134. let videoDir = 'D:\\VideoDir' //紧急录像或录像剪辑存储路径
  135. let layout = objData.layout //playMode指定模式的布局
  136. let enableHTTPS = 1 //是否启用HTTPS协议与综合安防管理平台交互,这里总是填1
  137. let encryptedFields = 'secret' //加密字段,默认加密领域为secret
  138. let showToolbar = objData.showToolbar //是否显示工具栏,0-不显示,非0-显示
  139. let showSmart = 0 //是否显示移动框线框,0-不显示,非0-显示
  140. let buttonIDs = '0,16,256,257,258,259,260,512,513,514,515,516,517,768,769' //自定义工具条按钮
  141. // 请自行修改以上变量值
  142. state.oWebControl
  143. .JS_RequestInterface({
  144. funcName: 'init',
  145. argument: JSON.stringify({
  146. appkey: appkey, //API网关提供的appkey
  147. secret: secret, //API网关提供的secret
  148. ip: ip, //API网关IP地址
  149. playMode: playMode, //播放模式(决定显示预览还是回放界面)
  150. port: port, //端口
  151. snapDir: snapDir, //抓图存储路径
  152. videoDir: videoDir, //紧急录像或录像剪辑存储路径
  153. layout: layout, //布局
  154. enableHTTPS: enableHTTPS, //是否启用HTTPS协议
  155. encryptedFields: encryptedFields, //加密字段
  156. showToolbar: showToolbar, //是否显示工具栏
  157. showSmart: showSmart, //是否显示智能信息
  158. buttonIDs: buttonIDs //自定义工具条按钮
  159. })
  160. })
  161. .then(function (oData: any) {
  162. console.log(oData, 'oData')
  163. state.oWebControl.JS_Resize(state.idWidth, state.idHeight) // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题
  164. previewClick()
  165. })
  166. })
  167. }
  168. //获取公钥
  169. const getPubKey = (callback: any) => {
  170. state.oWebControl
  171. .JS_RequestInterface({
  172. funcName: 'getRSAPubKey',
  173. argument: JSON.stringify({
  174. keyLength: 1024
  175. })
  176. })
  177. .then((oData: { responseMsg: { data: string } }) => {
  178. console.log(oData)
  179. if (oData.responseMsg.data) {
  180. state.pubKey = oData.responseMsg.data
  181. callback()
  182. }
  183. })
  184. }
  185. //RSA加密
  186. const setEncrypt = (value: string) => {
  187. var encrypt = new JSEncrypt()
  188. encrypt.setPublicKey(state.pubKey)
  189. return encrypt.encrypt(value)
  190. }
  191. // 监听resize事件,使插件窗口尺寸跟随DIV窗口变化
  192. window.addEventListener('resize', () => {
  193. console.log('resize')
  194. if (state.oWebControl != null) {
  195. getElementWidth()
  196. state.oWebControl.JS_Resize(state.idWidth, state.idHeight)
  197. // setWndCover();
  198. }
  199. })
  200. // 监听滚动条scroll事件,使插件窗口跟随浏览器滚动而移动
  201. window.addEventListener('scroll', () => {
  202. if (state.oWebControl != null) {
  203. getElementWidth()
  204. state.oWebControl.JS_Resize(state.idWidth, state.idHeight)
  205. // setWndCover();
  206. }
  207. })
  208. //视频预览功能
  209. const previewClick = (data) => {
  210. var cameraIndexCode = data ? data : (props.wsUrl as any)
  211. // var cameraIndexCode = props.wsUrl as any //获取输入的监控点编号值,必填
  212. var streamMode = 0 //主子码流标识:0-主码流,1-子码流
  213. var transMode = 1 //传输协议:0-UDP,1-TCP
  214. var gpuMode = 0 //是否启用GPU硬解,0-不启用,1-启用
  215. var wndId = -1 //播放窗口序号(在2x2以上布局下可指定播放窗口)
  216. state.oWebControl.JS_RequestInterface({
  217. funcName: 'startPreview',
  218. argument: JSON.stringify({
  219. cameraIndexCode: cameraIndexCode, //监控点编号
  220. streamMode: streamMode, //主子码流标识
  221. transMode: transMode, //传输协议
  222. gpuMode: gpuMode, //是否开启GPU硬解
  223. wndId: wndId //可指定播放窗口
  224. })
  225. })
  226. console.log(state.oWebControl.JS_RequestInterface, ' ------face')
  227. console.log('当前的编号', cameraIndexCode)
  228. console.log('执行完成')
  229. }
  230. // 停止全部预览
  231. const stopAllPreview = () => {
  232. state.oWebControl.JS_RequestInterface({
  233. funcName: 'stopAllPreview'
  234. })
  235. }
  236. // 获取元素宽高
  237. const getElementWidth = () => {
  238. state.idWidth = document.getElementById(state.id)?.offsetWidth
  239. state.idHeight = document.getElementById(state.id)?.offsetHeight
  240. }
  241. onMounted(() => {
  242. getElementWidth()
  243. initPlugin()
  244. })
  245. onActivated(() => {
  246. getElementWidth()
  247. initPlugin()
  248. })
  249. onDeactivated(() => {
  250. console.log('销毁了')
  251. state.oWebControl.JS_HideWnd()
  252. state.oWebControl.JS_Disconnect().then(
  253. function () {
  254. //断开与插件服务连接成功
  255. console.log('断开与插件服务连接成功')
  256. },
  257. function () {
  258. //断开与插件服务连接失败
  259. console.log('断开与插件服务连接失败')
  260. }
  261. )
  262. })
  263. onUnmounted(() => {
  264. console.log('销毁了')
  265. state.oWebControl.JS_HideWnd()
  266. state.oWebControl.JS_Disconnect().then(
  267. function () {
  268. //断开与插件服务连接成功
  269. console.log('断开与插件服务连接成功')
  270. },
  271. function () {
  272. //断开与插件服务连接失败
  273. console.log('断开与插件服务连接失败')
  274. }
  275. )
  276. })
  277. // 暴露给父组件调用
  278. defineExpose({
  279. previewClick,
  280. stopAllPreview
  281. })
  282. </script>
  283. <style>
  284. .playWnd {
  285. width: 100%;
  286. height: 100%;
  287. }
  288. </style>

组件使用:

先引入这个组件到项目页面

  1. <template>
  2. <div>
  3. <div class="monitor-main">
  4. <HikVideo
  5. :wsUrl="cameraIndexCode"
  6. :objData="HKobjData"
  7. ref="hkplayer"
  8. ></HikVideo>
  9. </div>
  10. <button @click="previewVideo">预览视频</button>
  11. <button @click="stopAllPreview">停止所有预览</button>
  12. <el-select
  13. v-model="value"
  14. placeholder="设置布局"
  15. size="large"
  16. style="width: 240px"
  17. @change="setLayout"
  18. >
  19. <el-option
  20. v-for="item in options"
  21. :key="item.value"
  22. :label="item.value"
  23. :value="item.value"
  24. />
  25. </el-select>
  26. </div>
  27. </template>
  28. <script setup>
  29. import HikVideo from './largeScreen/components/VideoMain.vue'
  30. import { ref } from 'vue'
  31. const cameraIndexCode = ref('') // 摄像头索引code
  32. const HKobjData = ref({
  33. // 海康初始化数据
  34. appkey: '', //综合安防管理平台提供的appkey,必填
  35. ip: '', //综合安防管理平台IP地址,必填
  36. secret: '', //综合安防管理平台提供的secret,必填
  37. port: 443, //综合安防管理平台端口,若启用HTTPS协议,默认443
  38. playMode: 0, // 0 预览 1回放
  39. layout: '1x1', //页面展示的模块数【16】
  40. showToolbar: 1 //是否显示工具栏,0-不显示,非0-显示
  41. })
  42. const hkplayer = ref(null)
  43. const value = ref('1x1')
  44. const options = [
  45. { value: '1x1' },
  46. { value: '2x2' },
  47. { value: '3x3' },
  48. { value: '4x4' },
  49. { value: '4+9' },
  50. { value: '1+1+12' },
  51. { value: '1+16' }
  52. ]
  53. const previewVideo = () => {
  54. console.log('预览视频')
  55. // 预览视频 传参(参数为摄像头索引code)
  56. hkplayer.value.previewClick('')
  57. }
  58. const stopAllPreview = () => {
  59. console.log('停止所有预览')
  60. hkplayer.value.stopAllPreview()
  61. }
  62. const setLayout = (e) => {
  63. console.log(e)
  64. hkplayer.value.setLayout(e)
  65. }
  66. </script>
  67. <style>
  68. .monitor-main {
  69. width: 600px;
  70. height: 400px;
  71. }
  72. </style>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号