JavaScript海康视频插件使用的具体步骤总结
作者:影子信息
背景
海康视频插件v.1.5.2版本,v1.5.4版本运行环境需要安装插件VideoWebPlugin.exe【双击安装】,对浏览器也有兼容性要求,具体看官方文档。
效果展示
demo演示:
备注:
appkey: "28377425",
secret: "92UZmJaAN6TuDJaNK64i",
ip: "116.252.75.113",
port: 10443,
b100434ecfde403c878c43586e8ae3dd
7af2175a02c34e20bb923c706790013e
插件使用步骤总结
1.new 一个WebControl 插件实例
2. 启动插件服务
3.创建视频播放窗口、绑定消息回调
4.初始化参数,其中secret参数需要通过RSA加密,加密公钥通过WebControl.JS_RequestInterface获取
5.通过WebControl 插件实例调用API方法操作功能(预览,回放,抓图、录像等)
6.离开页面断开与插件服务连接
具体步骤分析
1.new 一个WebControl 插件实例
后续功能都将通过该实例操作
// 插件对象实例,初始化为null,需要创建多个插件窗口时,需要定义多个插件对象实例变量,各个变量唯一标志对应的插件实例 let oWebControl = null; // 创建插件实例,并启动本地服务建立websocket连接,创建插件窗口 function initPlugin () { oWebControl = new WebControl({ szPluginContainer: "playWnd", iServicePortStart: 15900, iServicePortEnd: 15900, szClassId:"23BF3B0A-2C56-4D97-9C03-0CB103AA8F11", // 用于IE10使用ActiveX的clsid cbConnectSuccess: function () { initCount = 0; setCallbacks(); oWebControl.JS_StartService("window", { dllPath: "./VideoPluginConnect.dll" }).then(function () { oWebControl.JS_CreateWnd("playWnd", 800, 400).then(function () { console.log("JS_CreateWnd success"); oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData) if (oData.responseMsg.data) { pubKey = oData.responseMsg.data } }); }); }, function () { }); }, cbConnectError: function () { console.log("cbConnectError"); oWebControl = null; $("#playWnd").html("插件未启动,正在尝试启动,请稍候..."); WebControl.JS_WakeUp("VideoWebPlugin://"); initCount ++; if (initCount < 3) { setTimeout(function () { initPlugin(); }, 3000) } else { $("#playWnd").html("插件启动失败,请检查插件是否安装!"); } }, cbConnectClose: function (bNormalClose) { // 异常断开:bNormalClose = false // JS_Disconnect正常断开:bNormalClose = true console.log("cbConnectClose"); oWebControl = null; $("#playWnd").html("插件未启动,正在尝试启动,请稍候..."); WebControl.JS_WakeUp("VideoWebPlugin://"); initCount ++; if (initCount < 3) { setTimeout(function () { initPlugin(); }, 3000) } else { $("#playWnd").html("插件启动失败,请检查插件是否安装!"); } } }); } initPlugin();
2.启动插件服务
创建WebControl实例成功的回调函数中通过oWebControl.JS_StartService启动插件服务:
oWebControl.JS_StartService("window", { dllPath: "./VideoPluginConnect.dll" }).then(function () { oWebControl.JS_CreateWnd("playWnd", 800, 400).then(function () { console.log("JS_CreateWnd success"); oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData) if (oData.responseMsg.data) { pubKey = oData.responseMsg.data } }); }); },
3.创建视频播放窗口、绑定消息回调
通过创建播放窗口绘制样式布局,并绑定一些事件(进出全屏、切换布局消息等)的回调
setCallbacks(); // 设置窗口控制回调 function setCallbacks() { oWebControl.JS_SetWindowControlCallback({ cbIntegrationCallBack: cbIntegrationCallBack }); } // 推送消息 function cbIntegrationCallBack(oData) { showCBInfo(JSON.stringify(oData.responseMsg)); } // 显示接口返回的消息及插件回调信息 function showCBInfo(szInfo, type) { console.log(szInfo, type); }
4.初始化参数,其中secret参数需要通过RSA加密,加密公钥通过WebControl.JS_RequestInterface获取
通过appkey、secret、ip、port端口号等参数与服务器进行数据校验,校验通过即可正常与设备链接取流,初始化参数在步骤三创建窗口成功回调后执行,其中secret参数必须通过RSA进行加密处理,加密的公钥通过WebControl.JS_RequestInterface获取
oWebControl.JS_CreateWnd("playWnd", 1000, 600).then(function () { //JS_CreateWnd创建视频播放窗口,宽高可设定 init(); // 创建播放实例成功后初始化 }); //初始化 function init() { getPubKey(function () { // 请自行修改以下变量值 var appkey = "20211111"; //综合安防管理平台提供的appkey,必填 var secret = setEncrypt("Xxxxxxxxxxxxxxxxxx"); //综合安防管理平台提供的secret,必填 var ip = "192.168.22.80"; //综合安防管理平台IP地址,必填 var playMode = 0; //初始播放模式:0-预览,1-回放 var port = 443; //综合安防管理平台端口,若启用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"; //自定义工具条按钮 // 请自行修改以上变量值 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 (oData) { oWebControl.JS_Resize(1000, 600); // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题 }); }); } //获取公钥 function getPubKey(callback) { oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData, 'odate'); if (oData.responseMsg.data) { pubKey = oData.responseMsg.data; callback() } }) //RSA加密 function setEncrypt(value) { var encrypt = new JSEncrypt(); encrypt.setPublicKey(pubKey); return encrypt.encrypt(value);
5.通过WebControl 插件实例调用API方法操作功能(预览,回放,抓图、录像等)
经过上面4个步骤,初始化工作已经全部完成,接下去就是使用api去操作实例实现对应功能
(1)预览功能,funcName: "startPreview",
var cameraIndexCode='1e48db29e22143118441b75d199999dd'; //获取输入的监控点编号值,必填,从后端接口返回获取 var streamMode = 0; //主子码流标识:0-主码流,1-子码流 var transMode = 1; //传输协议:0-UDP,1-TCP var gpuMode = 1; //是否启用GPU硬解,0-不启用,1-启用 var wndId = -1; //播放窗口序号(在2x2以上布局下可指定播放窗口) oWebControl.JS_RequestInterface({ funcName: "startPreview", argument: JSON.stringify({ cameraIndexCode:cameraIndexCode, //监控点编号 streamMode: streamMode, //主子码流标识 transMode: transMode, //传输协议 gpuMode: gpuMode, //是否开启GPU硬解 wndId:wndId //可指定播放窗口 }) }).then(res=>{ console.log(res,'res') })
// value为字符串,JS_RequestInterface仅接收json格式的变量,且需要先解析出argument,并且将argument字段的内容转为字符串 function requestInterface(value){ isJSON(value); var JsonParam = JSON.parse(value); var JsonArgument = JsonParam.argument; JsonParam.argument = JSON.stringify(JsonArgument); oWebControl.JS_RequestInterface(JsonParam).then(function (oData) { console.log(oData) }); } // 判断字符串是否为json function isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); if(typeof obj == 'object' && obj ){ return true; }else{ showCBInfo("param is not the correct JSON message"); return false; } } catch(e) { showCBInfo("param is not the correct JSON message"); return false; } } console.log('It is not a string!') }
// 单路播放轮巡函数 function SingleTourPreview() { // 解析轮巡的监控点编号 var param = $("#TourPreviewCamIdx").val(); //param = param.replace(/(\s*)/g, ""); var cameraIndexArray = new Array(); cameraIndexArray = param.split(","); var arraySize = cameraIndexArray.length; // 获取当前窗口个数 var WndNum = 1; oWebControl.JS_RequestInterface({ funcName: "getLayout" }).then(function (oData) { //分析窗口数 var Data = JSON.stringify(oData.responseMsg.data); Data = Data.replace(/\\n/g, ""); Data = Data.replace(/\\/g, ""); Data = Data.replace(/\"{/g, "{"); Data = Data.replace(/}\"/g, "}"); WndNum = JSON.parse(Data).wndNum; for (i = 0; i < WndNum; i++) { var cameraIdx = cameraIndexArray[i % arraySize]; var previewParam = JSON.stringify({ "argument": { "authUuid": "", "cameraIndexCode": cameraIdx, "ezvizDirect": 0, "gpuMode": 0, "streamMode": 0, "transMode": 1, "wndId": i+1, "cascade": 1 }, "funcName": "startPreview" }); requestInterface(previewParam); } });
// 批量播放轮巡函数 function MultiTourPreview() { // 解析轮巡的监控点编号 var param = $("#TourPreviewCamIdx").val(); //param = param.replace(/(\s*)/g, ""); var cameraIndexArray = new Array(); cameraIndexArray = param.split(","); var arraySize = cameraIndexArray.length; // 获取当前窗口个数 var WndNum = 1; oWebControl.JS_RequestInterface({ funcName: "getLayout" }).then(function (oData) { //分析窗口数 var Data = JSON.stringify(oData.responseMsg.data); Data = Data.replace(/\\n/g, ""); Data = Data.replace(/\\/g, ""); Data = Data.replace(/\"{/g, "{"); Data = Data.replace(/}\"/g, "}"); WndNum = JSON.parse(Data).wndNum; var multiPlayParam = { "argument": { "list": [] }, "funcName": "startMultiPreviewByCameraIndexCode" } for (i = 0; i < WndNum; i++) { var PlayParam = { "authUuid": "", "cameraIndexCode": cameraIndexArray[i % arraySize], "ezvizDirect": 0, "gpuMode": 0, "streamMode": 0, "transMode": 1, "wndId": i+1 } multiPlayParam.argument.list.push(PlayParam); } var previewParam = JSON.stringify(multiPlayParam); requestInterface(previewParam); }); }
(2)设置窗口布局
oWebControl.JS_RequestInterface({ funcName: " setLayout", argument: { layout:'2x2',//窗口布局 } })
(3)抓图
oWebControl.JS_RequestInterface({ funcName: "snapShot", argument:{ name:'D:\test.jpg'//图片绝对路径名称 }, });
总结:
其他功能对着开发文档API调用就行了
所有功能调用都是通过
oWebControl.JS_RequestInterface({ funcName: "",//例如:startPreview预览功能 argument:{} })这个API实现,不同功能通过funcName值区别,argument为该功能需要传递的参数
6.离开页面断开与插件服务连接
释放资源
// 标签关闭 $(window).unload(function () { if (oWebControl != null){ oWebControl.JS_HideWnd(); // 先让窗口隐藏,规避可能的插件窗口滞后于浏览器消失问题 oWebControl.JS_Disconnect().then(function(){}, function() {}); } });
四、窗口事件绑定
海康播放插件本质是ActiveX控件,无法通过css控制位置和大小,当页面窗口大小变化或者遇到页面有滚动条情况就需要手动设置插件窗口位置和大小,可以通过JS_Resize (调整插件窗口大小、位置),JS_CuttingPartWindow (扣除部分插件窗口)两个api来实现
// 监听resize事件,使插件窗口尺寸跟随DIV窗口变化 $(window).resize(function () { if (oWebControl != null) { oWebControl.JS_Resize(1000, 600); //oWebControl.JS_Resize(800, 400); setWndCover(); } }); // 监听滚动条scroll事件,使插件窗口跟随浏览器滚动而移动 $(window).scroll(function () { if (oWebControl != null) { oWebControl.JS_Resize(1000, 600); //oWebControl.JS_Resize(800, 400); setWndCover(); } }); // 设置窗口裁剪,当因滚动条滚动导致窗口需要被遮住的情况下需要JS_CuttingPartWindow部分窗口 function setWndCover() { var iWidth = $(window).width(); var iHeight = $(window).height(); var oDivRect = $("#playWnd").get(0).getBoundingClientRect(); var iCoverLeft = (oDivRect.left < 0) ? Math.abs(oDivRect.left) : 0; var iCoverTop = (oDivRect.top < 0) ? Math.abs(oDivRect.top) : 0; var iCoverRight = (oDivRect.right - iWidth > 0) ? Math.round(oDivRect.right - iWidth) : 0; var iCoverBottom = (oDivRect.bottom - iHeight > 0) ? Math.round(oDivRect.bottom - iHeight) : 0; iCoverLeft = (iCoverLeft > 1000) ? 1000 : iCoverLeft; iCoverTop = (iCoverTop > 600) ? 600 : iCoverTop; iCoverRight = (iCoverRight > 1000) ? 1000 : iCoverRight; iCoverBottom = (iCoverBottom > 600) ? 600 : iCoverBottom; oWebControl.JS_RepairPartWindow(0, 0, 1001, 600); // 多1个像素点防止还原后边界缺失一个像素条 if (iCoverLeft != 0) { oWebControl.JS_CuttingPartWindow(0, 0, iCoverLeft, 600); } if (iCoverTop != 0) { oWebControl.JS_CuttingPartWindow(0, 0, 1001, iCoverTop); // 多剪掉一个像素条,防止出现剪掉一部分窗口后出现一个像素条 } if (iCoverRight != 0) { oWebControl.JS_CuttingPartWindow(1000 - iCoverRight, 0, iCoverRight, 600); } if (iCoverBottom != 0) { oWebControl.JS_CuttingPartWindow(0, 600 - iCoverBottom, 1000, iCoverBottom); } }
五、vue脚手架中集成插件
官方插件包和开发文档下载:点击跳转
把官方资源包里的jsWebControl-1.0.0.min.js复制到vue项目public目录下,在index.html引入js
完整代码:
<template> <div> <!--视频窗口展示--> <div id="playWnd" class="playWnd" style="left: 109px; top: 133px;"></div> </div> </template> <script setup> // v1.5.2版本demo.html部分文档 //页面加载时创建播放实例初始化 $(window).load(function () { initPlugin(); }); //声明公用变量 var initCount = 0; var pubKey = ''; // 创建播放实例 function initPlugin() { oWebControl = new WebControl({ szPluginContainer: "playWnd", // 指定容器id iServicePortStart: 15900, // 指定起止端口号,建议使用该值 iServicePortEnd: 15909, szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11", // 用于IE10使用ActiveX的clsid cbConnectSuccess: function () { // 创建WebControl实例成功 oWebControl.JS_StartService("window", { // WebControl实例创建成功后需要启动服务 dllPath: "./VideoPluginConnect.dll" // 值"./VideoPluginConnect.dll"写死 }).then(function () { // 启动插件服务成功 oWebControl.JS_SetWindowControlCallback({ // 设置消息回调 cbIntegrationCallBack: cbIntegrationCallBack }); oWebControl.JS_CreateWnd("playWnd", 1000, 600).then(function () { //JS_CreateWnd创建视频播放窗口,宽高可设定 init(); // 创建播放实例成功后初始化 }); }, function () { // 启动插件服务失败 }); }, cbConnectError: function () { // 创建WebControl实例失败 console.log('xxx') oWebControl = null; $("#playWnd").html("插件未启动,正在尝试启动,请稍候..."); WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未启动时执行error函数,采用wakeup来启动程序 initCount++; if (initCount < 3) { setTimeout(function () { initPlugin(); }, 3000) } else { $("#playWnd").html("插件启动失败,请检查插件是否安装!"); } }, cbConnectClose: function (bNormalClose) { // 异常断开:bNormalClose = false // JS_Disconnect正常断开:bNormalClose = true console.log("cbConnectClose"); oWebControl = null; } }); } // 设置窗口控制回调 function setCallbacks() { oWebControl.JS_SetWindowControlCallback({ cbIntegrationCallBack: cbIntegrationCallBack }); } // 推送消息 function cbIntegrationCallBack(oData) { showCBInfo(JSON.stringify(oData.responseMsg)); } //初始化 function init() { getPubKey(function () { // 请自行修改以下变量值 var appkey = "20211111"; //综合安防管理平台提供的appkey,必填 var secret = setEncrypt("Xxxxxxxxxxxxxxxxxx"); //综合安防管理平台提供的secret,必填 var ip = "192.168.22.80"; //综合安防管理平台IP地址,必填 var playMode = 0; //初始播放模式:0-预览,1-回放 var port = 443; //综合安防管理平台端口,若启用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"; //自定义工具条按钮 // 请自行修改以上变量值 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 (oData) { oWebControl.JS_Resize(1000, 600); // 初始化后resize一次,规避firefox下首次显示窗口后插件窗口未与DIV窗口重合问题 }); }); } //获取公钥 function getPubKey(callback) { oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData, 'odate'); if (oData.responseMsg.data) { pubKey = oData.responseMsg.data; callback() } }) } //RSA加密 function setEncrypt(value) { var encrypt = new JSEncrypt(); encrypt.setPublicKey(pubKey); return encrypt.encrypt(value); } // 监听resize事件,使插件窗口尺寸跟随DIV窗口变化 $(window).resize(function () { if (oWebControl != null) { oWebControl.JS_Resize(1000, 600); setWndCover(); } }); // 监听滚动条scroll事件,使插件窗口跟随浏览器滚动而移动 $(window).scroll(function () { if (oWebControl != null) { oWebControl.JS_Resize(1000, 600); setWndCover(); } }); // 设置窗口裁剪,当因滚动条滚动导致窗口需要被遮住的情况下需要JS_CuttingPartWindow部分窗口 function setWndCover() { var iWidth = $(window).width(); var iHeight = $(window).height(); var oDivRect = $("#playWnd").get(0).getBoundingClientRect(); var iCoverLeft = (oDivRect.left < 0) ? Math.abs(oDivRect.left) : 0; var iCoverTop = (oDivRect.top < 0) ? Math.abs(oDivRect.top) : 0; var iCoverRight = (oDivRect.right - iWidth > 0) ? Math.round(oDivRect.right - iWidth) : 0; var iCoverBottom = (oDivRect.bottom - iHeight > 0) ? Math.round(oDivRect.bottom - iHeight) : 0; iCoverLeft = (iCoverLeft > 1000) ? 1000 : iCoverLeft; iCoverTop = (iCoverTop > 600) ? 600 : iCoverTop; iCoverRight = (iCoverRight > 1000) ? 1000 : iCoverRight; iCoverBottom = (iCoverBottom > 600) ? 600 : iCoverBottom; oWebControl.JS_RepairPartWindow(0, 0, 1001, 600); // 多1个像素点防止还原后边界缺失一个像素条 if (iCoverLeft != 0) { oWebControl.JS_CuttingPartWindow(0, 0, iCoverLeft, 600); } if (iCoverTop != 0) { oWebControl.JS_CuttingPartWindow(0, 0, 1001, iCoverTop); // 多剪掉一个像素条,防止出现剪掉一部分窗口后出现一个像素条 } if (iCoverRight != 0) { oWebControl.JS_CuttingPartWindow(1000 - iCoverRight, 0, iCoverRight, 600); } if (iCoverBottom != 0) { oWebControl.JS_CuttingPartWindow(0, 600 - iCoverBottom, 1000, iCoverBottom); } } //视频预览功能 $("#startPreview").click(function () { var cameraIndexCode = $("#cameraIndexCode").val(); //获取输入的监控点编号值,必填 var streamMode = 0; //主子码流标识:0-主码流,1-子码流 var transMode = 1; //传输协议:0-UDP,1-TCP var gpuMode = 1; //是否启用GPU硬解,0-不启用,1-启用 var wndId = -1; //播放窗口序号(在2x2以上布局下可指定播放窗口) cameraIndexCode = cameraIndexCode.replace(/(^\s*)/g, ""); cameraIndexCode = cameraIndexCode.replace(/(\s*$)/g, ""); cameraIndexCode = "1e48db29e22143118441b75d199999dd" oWebControl.JS_RequestInterface({ funcName: "startPreview", argument: JSON.stringify({ cameraIndexCode: cameraIndexCode, //监控点编号 streamMode: streamMode, //主子码流标识 transMode: transMode, //传输协议 gpuMode: gpuMode, //是否开启GPU硬解 wndId: wndId //可指定播放窗口 }) }).then(res => { console.log(res, 'res') }) }); //停止全部预览 $("#stopAllPreview").click(function () { oWebControl.JS_RequestInterface({ funcName: "stopAllPreview" }); }); // 标签关闭 $(window).unload(function () { if (oWebControl != null) { oWebControl.JS_HideWnd(); // 先让窗口隐藏,规避可能的插件窗口滞后于浏览器消失问题 oWebControl.JS_Disconnect().then(function () { // 断开与插件服务连接成功 }, function () { // 断开与插件服务连接失败 }); } }); </script> <script> // v1.5.4版本demo.html部分文档 // 插件对象实例,初始化为null,需要创建多个插件窗口时,需要定义多个插件对象实例变量,各个变量唯一标志对应的插件实例 let oWebControl = null; // 创建插件实例,并启动本地服务建立websocket连接,创建插件窗口 function initPlugin () { oWebControl = new WebControl({ szPluginContainer: "playWnd", iServicePortStart: 15900, iServicePortEnd: 15900, szClassId:"23BF3B0A-2C56-4D97-9C03-0CB103AA8F11", // 用于IE10使用ActiveX的clsid cbConnectSuccess: function () { initCount = 0; setCallbacks(); oWebControl.JS_StartService("window", { dllPath: "./VideoPluginConnect.dll" }).then(function () { oWebControl.JS_CreateWnd("playWnd", 800, 400).then(function () { console.log("JS_CreateWnd success"); oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData) if (oData.responseMsg.data) { pubKey = oData.responseMsg.data } }); }); }, function () { }); }, cbConnectError: function () { console.log("cbConnectError"); oWebControl = null; $("#playWnd").html("插件未启动,正在尝试启动,请稍候..."); WebControl.JS_WakeUp("VideoWebPlugin://"); initCount ++; if (initCount < 3) { setTimeout(function () { initPlugin(); }, 3000) } else { $("#playWnd").html("插件启动失败,请检查插件是否安装!"); } }, cbConnectClose: function (bNormalClose) { // 异常断开:bNormalClose = false // JS_Disconnect正常断开:bNormalClose = true console.log("cbConnectClose"); oWebControl = null; $("#playWnd").html("插件未启动,正在尝试启动,请稍候..."); WebControl.JS_WakeUp("VideoWebPlugin://"); initCount ++; if (initCount < 3) { setTimeout(function () { initPlugin(); }, 3000) } else { $("#playWnd").html("插件启动失败,请检查插件是否安装!"); } } }); } initPlugin(); // 获取公钥 function getPubKey (callback) { oWebControl.JS_RequestInterface({ funcName: "getRSAPubKey", argument: JSON.stringify({ keyLength: 1024 }) }).then(function (oData) { console.log(oData) if (oData.responseMsg.data) { pubKey = oData.responseMsg.data callback() } }) } // 设置窗口控制回调 function setCallbacks() { oWebControl.JS_SetWindowControlCallback({ cbIntegrationCallBack: cbIntegrationCallBack }); } // 推送消息 function cbIntegrationCallBack(oData) { showCBInfo(JSON.stringify(oData.responseMsg)); } // RSA加密 function setEncrypt (value) { var encrypt = new JSEncrypt(); encrypt.setPublicKey(pubKey); return encrypt.encrypt(value); } // value为字符串,JS_RequestInterface仅接收json格式的变量,且需要先解析出argument,并且将argument字段的内容转为字符串 function requestInterface(value){ isJSON(value); var JsonParam = JSON.parse(value); var JsonArgument = JsonParam.argument; JsonParam.argument = JSON.stringify(JsonArgument); oWebControl.JS_RequestInterface(JsonParam).then(function (oData) { console.log(oData) showCBInfo(JSON.stringify(oData ? oData.responseMsg : '')); }); } // 显示接口返回的消息及插件回调信息 function showCBInfo(szInfo, type) { if (type === 'error') { szInfo = "<div style='color: red;'>" + dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss") + " " + szInfo + "</div>"; } else { szInfo = "<div>" + dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss") + " " + szInfo + "</div>"; } $("#cbInfo").html(szInfo + $("#cbInfo").html()); } // 判断字符串是否为json function isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); if(typeof obj == 'object' && obj ){ return true; }else{ showCBInfo("param is not the correct JSON message"); return false; } } catch(e) { showCBInfo("param is not the correct JSON message"); return false; } } console.log('It is not a string!') } // 格式化时间 function dateFormat(oDate, fmt) { var o = { "M+": oDate.getMonth() + 1, //月份 "d+": oDate.getDate(), //日 "h+": oDate.getHours(), //小时 "m+": oDate.getMinutes(), //分 "s+": oDate.getSeconds(), //秒 "q+": Math.floor((oDate.getMonth() + 3) / 3), //季度 "S": oDate.getMilliseconds()//毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (oDate.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; } var timeStart = false; var timer; //轮巡,每隔10s执行一次 $("#startTourPreviewBtn").click(function (){ if (!timeStart) { timeStart = true; var sel = document.getElementById("tourPreviewType"); var selectedId = sel.selectedIndex; var v = sel.options[selectedId].value; if (0 == v) // 单路播放轮巡 { SingleTourPreview(); timer = window.setInterval(SingleTourPreview, 1000*10); } else{ // 批量播放轮巡 MultiTourPreview(); timer = window.setInterval(MultiTourPreview, 1000*10); } } else{ showCBInfo('start tour preview failed, please stop tour preview first'); } }) // 单路播放轮巡函数 function SingleTourPreview() { // 解析轮巡的监控点编号 var param = $("#TourPreviewCamIdx").val(); //param = param.replace(/(\s*)/g, ""); var cameraIndexArray = new Array(); cameraIndexArray = param.split(","); var arraySize = cameraIndexArray.length; // 获取当前窗口个数 var WndNum = 1; oWebControl.JS_RequestInterface({ funcName: "getLayout" }).then(function (oData) { //分析窗口数 var Data = JSON.stringify(oData.responseMsg.data); Data = Data.replace(/\\n/g, ""); Data = Data.replace(/\\/g, ""); Data = Data.replace(/\"{/g, "{"); Data = Data.replace(/}\"/g, "}"); WndNum = JSON.parse(Data).wndNum; for (i = 0; i < WndNum; i++) { var cameraIdx = cameraIndexArray[i % arraySize]; var previewParam = JSON.stringify({ "argument": { "authUuid": "", "cameraIndexCode": cameraIdx, "ezvizDirect": 0, "gpuMode": 0, "streamMode": 0, "transMode": 1, "wndId": i+1, "cascade": 1 }, "funcName": "startPreview" }); requestInterface(previewParam); } }); } // 批量播放轮巡函数 function MultiTourPreview() { // 解析轮巡的监控点编号 var param = $("#TourPreviewCamIdx").val(); //param = param.replace(/(\s*)/g, ""); var cameraIndexArray = new Array(); cameraIndexArray = param.split(","); var arraySize = cameraIndexArray.length; // 获取当前窗口个数 var WndNum = 1; oWebControl.JS_RequestInterface({ funcName: "getLayout" }).then(function (oData) { //分析窗口数 var Data = JSON.stringify(oData.responseMsg.data); Data = Data.replace(/\\n/g, ""); Data = Data.replace(/\\/g, ""); Data = Data.replace(/\"{/g, "{"); Data = Data.replace(/}\"/g, "}"); WndNum = JSON.parse(Data).wndNum; var multiPlayParam = { "argument": { "list": [] }, "funcName": "startMultiPreviewByCameraIndexCode" } for (i = 0; i < WndNum; i++) { var PlayParam = { "authUuid": "", "cameraIndexCode": cameraIndexArray[i % arraySize], "ezvizDirect": 0, "gpuMode": 0, "streamMode": 0, "transMode": 1, "wndId": i+1 } multiPlayParam.argument.list.push(PlayParam); } var previewParam = JSON.stringify(multiPlayParam); requestInterface(previewParam); }); } // 标签关闭 $(window).unload(function () { if (oWebControl != null){ oWebControl.JS_HideWnd(); // 先让窗口隐藏,规避可能的插件窗口滞后于浏览器消失问题 oWebControl.JS_Disconnect().then(function(){}, function() {}); } }); // 窗口resize $(window).resize(function () { if (oWebControl != null) { oWebControl.JS_Resize(800, 400); setWndCover(); } }); // 滚动条scroll $(window).scroll(function () { if (oWebControl != null) { oWebControl.JS_Resize(800, 400); setWndCover(); } }); </script> <style lang="scss" scoped></style>
总结
到此这篇关于JavaScript海康视频插件使用的文章就介绍到这了,更多相关js海康视频插件使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!