知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
您當(dāng)前位置>首頁 » 新聞資訊 » 網(wǎng)站建設(shè) >
??低暤囊曨lweb端(vue開發(fā))的實(shí)時預(yù)覽、錄像回放和頁面拖拽
發(fā)表時間:2019-11-20
發(fā)布人:葵宇科技
瀏覽次數(shù):704
效果圖
實(shí)現(xiàn)步驟
1. 安裝海康web插件,去??低暪倬W(wǎng)上下載,官方不支持Mac,只支持Windows
https://open.hikvision.com/
2. 在index.html引入js文件,安裝包里面有
<script src="static/jquery-1.12.4.min.js"></script>
<script src="static/jsencrypt.min.js"></script>
<script src="static/jsWebControl-1.0.0.min.js"></script>
3. 代碼實(shí)現(xiàn),可兼容谷歌、火狐、360、edge
<template>
<div id="videoPlayerBox" ref="videoPlayerBox"
:style="{'width': width + 'px', 'height': height + 'px', 'top': top + 'px', 'left': left + 'px'}">
<div class="header">
<div class="el-dialog__title" @mousedown="onmousedown" @mouseleave="onmouseleave">
<!-- {{ monitorDeviceName }} -->
監(jiān)控點(diǎn)
</div>
<span class="closeBtn" @click="handleClose">×</span>
</div>
<div class="topbutton">
<span class="topBtns" :class="playMode === 0 ? 'activeBtn' : ''" @click="previewClick">預(yù)覽</span>
<span class="topBtns" :class="playMode === 1 ? 'activeBtn' : ''" @click="playBack">回放</span>
</div>
<div id="playBox" v-html="oWebControl === null ? playText : ''">
</div>
</div>
</template>
<script>
export default {
name: 'videoPlayer',
props: [
'videoVisible',
'monitorDeviceNo',
'monitorDeviceName'
],
data() {
return {
oWebControl: null,
pubKey: '', // 公鑰
appkey: '',
secret: '',
ip: '',
port: '',
width: 700,
height: 400, // 彈框高度
playHeight: 360,
layout: '1x1',
left: '',
top: '',
buttonIDs: '0,16,256,257,515',
initCount: 0,
playMode: 0, // 0 預(yù)覽 1回放
playText: '啟動中。。。',
cameraIndexCode: this.monitorDeviceNo || '' // 監(jiān)控點(diǎn)編號
}
},
methods: {
handleClose() {
if (this.oWebControl) {
this.oWebControl.JS_RequestInterface({
funcName: "stopAllPreview"
});
this.oWebControl.JS_HideWnd();
this.oWebControl.JS_Disconnect().then(
() => { // 斷開與插件服務(wù)連接成功
},
() => { // 斷開與插件服務(wù)連接失敗
});
this.oWebControl = null;
}
this.$emit('close');
},
// 推送消息
cbIntegrationCallBack(oData) {
console.log(oData, '推送消息');
},
// RSA加密
setEncrypt (value) {
/* eslint-disable */
let encrypt = new JSEncrypt();
encrypt.setPublicKey(this.pubKey);
return encrypt.encrypt(value);
},
initPlugin(callback) {
/* eslint-disable */
let that = this;
this.oWebControl = new WebControl({
szPluginContainer: "playBox", // 指定容器id
iServicePortStart: 15900, // 指定起止端口號,建議使用該值
iServicePortEnd: 15909,
szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11", // 用于IE10使用ActiveX的clsid
cbConnectSuccess: function () { // 創(chuàng)建WebControl實(shí)例成功
that.oWebControl.JS_StartService("window", { // WebControl實(shí)例創(chuàng)建成功后需要啟動服務(wù)
dllPath: "./VideoPluginConnect.dll" // 值"./VideoPluginConnect.dll"寫死
}).then(function () { // 啟動插件服務(wù)成功
that.oWebControl.JS_SetWindowControlCallback({ // 設(shè)置消息回調(diào)
cbIntegrationCallBack: that.cbIntegrationCallBack
});
that.oWebControl.JS_CreateWnd("playBox", that.width, that.playHeight).then(function () { // JS_CreateWnd創(chuàng)建視頻播放窗口,寬高可設(shè)定
that.init(callback); // 創(chuàng)建播放實(shí)例成功后初始化
});
}, function () { // 啟動插件服務(wù)失敗
});
},
cbConnectError: function () { // 創(chuàng)建WebControl實(shí)例失敗
that.oWebControl = null;
that.playText = '插件未啟動,正在嘗試啟動,請稍候...';
WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未啟動時執(zhí)行error函數(shù),采用wakeup來啟動程序
that.initCount++;
if (that.initCount < 3) {
setTimeout(function () {
that.initPlugin();
}, 3000)
} else {
that.playText = '插件啟動失敗,請檢查插件是否安裝!<a target="_blank" style="color: #30a8ff;text-decoration: underline;" href="http://xx.com/VideoWebPlugin.zip">下載地址(軟件大小:62.7MB)</a>';
}
},
cbConnectClose: function (bNormalClose) {
// 異常斷開:bNormalClose = false
// JS_Disconnect正常斷開:bNormalClose = true
console.log("cbConnectClose");
that.oWebControl = null;
}
});
},
// 獲取公鑰
getPubKey (callback) {
this.oWebControl.JS_RequestInterface({
funcName: "getRSAPubKey",
argument: JSON.stringify({
keyLength: 1024
})
}).then((oData) => {
if (oData.responseMsg.data) {
this.pubKey = oData.responseMsg.data;
callback()
}
})
},
init(callback) {
let that = this;
this.getPubKey(() => {
// 請自行修改以下變量值
let appkey = this.appkey; // 綜合安防管理平臺提供的appkey,必填
let secret = that.setEncrypt(this.secret);// 綜合安防管理平臺提供的secret,必填
let ip = this.ip; // 綜合安防管理平臺IP地址,必填
let playMode = this.playMode; // 初始播放模式:0-預(yù)覽,1-回放
let port = 443; // 綜合安防管理平臺端口,若啟用HTTPS協(xié)議,默認(rèn)443
let snapDir = "D:\\SnapDir"; // 抓圖存儲路徑
let videoDir = "D:\\VideoDir"; // 緊急錄像或錄像剪輯存儲路徑
let layout = this.layout; // playMode指定模式的布局
let enableHTTPS = 1; // 是否啟用HTTPS協(xié)議與綜合安防管理平臺交互,是為1,否為0
let encryptedFields = 'secret'; // 加密字段,默認(rèn)加密領(lǐng)域?yàn)閟ecret
let showToolbar = 1; // 是否顯示工具欄,0-不顯示,非0-顯示
let showSmart = 1; // 是否顯示智能信息(如配置移動偵測后畫面上的線框),0-不顯示,非0-顯示
let buttonIDs = this.buttonIDs; // 自定義工具條按鈕
// /// 請自行修改以上變量值
that.oWebControl.JS_RequestInterface({
funcName: "init",
argument: JSON.stringify({
appkey: appkey, // API網(wǎng)關(guān)提供的appkey
secret: secret, // API網(wǎng)關(guān)提供的secret
ip: ip, // API網(wǎng)關(guān)IP地址
playMode: playMode, // 播放模式(決定顯示預(yù)覽還是回放界面)
port: port, // 端口
snapDir: snapDir, // 抓圖存儲路徑
videoDir: videoDir, // 緊急錄像或錄像剪輯存儲路徑
layout: layout, // 布局
enableHTTPS: enableHTTPS, // 是否啟用HTTPS協(xié)議
encryptedFields: encryptedFields, // 加密字段
showToolbar: showToolbar, // 是否顯示工具欄
showSmart: showSmart, // 是否顯示智能信息
buttonIDs: buttonIDs // 自定義工具條按鈕
})
}).then((oData) => {
that.oWebControl.JS_Resize(that.width, that.playHeight); // 初始化后resize一次,規(guī)避firefox下首次顯示窗口后插件窗口未與DIV窗口重合問題
if (callback) {
callback();
}
});
});
},
// 視頻預(yù)覽功能
previewClick() {
if (!this.oWebControl) {
return;
}
// 如果是回放,重新初始化
if (this.playMode === 1) {
this.playMode = 0;
this.oWebControl.JS_HideWnd();
this.initPlugin(() => {
this.previewVideo();
});
} else if (this.playMode === 0) {
this.previewVideo();
}
},
previewVideo() {
let cameraIndexCode = this.cameraIndexCode; // 獲取輸入的監(jiān)控點(diǎn)編號值,必填
let streamMode = 0; // 主子碼流標(biāo)識:0-主碼流,1-子碼流
let transMode = 0; // 傳輸協(xié)議:0-UDP,1-TCP
let gpuMode = 0; // 是否啟用GPU硬解,0-不啟用,1-啟用
let wndId = -1; // 播放窗口序號(在2x2以上布局下可指定播放窗口)
this.oWebControl.JS_RequestInterface({
funcName: "startPreview",
argument: JSON.stringify({
cameraIndexCode: cameraIndexCode.trim(), // 監(jiān)控點(diǎn)編號
streamMode: streamMode, // 主子碼流標(biāo)識
transMode: transMode, // 傳輸協(xié)議
gpuMode: gpuMode, // 是否開啟GPU硬解
wndId: wndId // 可指定播放窗口
})
})
},
// 回放
playBack() {
if (!this.oWebControl) {
return;
}
// 如果是預(yù)覽
if (this.playMode === 0) {
this.playMode = 1;
this.oWebControl.JS_HideWnd();
this.initPlugin(() => {
this.backVideo();
});
} else if (this.playMode === 1) {
this.backVideo();
}
},
backVideo() {
let cameraIndexCode = this.cameraIndexCode;
// 前30天
let date = new Date(new Date().getTime() - 30 * 24 * 60 * 60 * 1000);
let month = date.getMonth() + 1 < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth()+ 1);
// 開始時間當(dāng)天00點(diǎn)
let str = date.getFullYear() + '/' + month + '/' + date.getDate() + ' 00:00:00'
let startTime = String(parseInt(new Date(str).getTime() / 1000) - 3 * 60 * 60);
let endTime = String(parseInt(date.getTime() / 1000));
this.oWebControl.JS_RequestInterface({
funcName: "startPlayback",
argument: JSON.stringify({
cameraIndexCode: cameraIndexCode.trim(), // 監(jiān)控點(diǎn)編號
startTimeStamp: startTime, // 錄像查詢開始時間戳,單位:秒
endTimeStamp: endTime, // 錄像查詢結(jié)束時間戳,單位:秒
recordLocation: 1, // 錄像存儲類型 0-中心存儲 1-設(shè)備存儲
transMode: 0, // 傳輸協(xié)議 ,0-UDP 1-TCP
gpuMode: 0, // 是否開啟 GPU 硬解,0-不開啟 1-開啟
wndId: -1 //可指定播放窗口
})
});
},
// 設(shè)置窗口裁剪,當(dāng)因滾動條滾動導(dǎo)致窗口需要被遮住的情況下需要JS_CuttingPartWindow部分窗口
setWndCover() {
let iWidth = $(window).width();
let iHeight = $(window).height();
let oDivRect = $("#playBox").get(0).getBoundingClientRect();
let iCoverLeft = (oDivRect.left < 0) ? Math.abs(oDivRect.left) : 0;
let iCoverTop = (oDivRect.top < 0) ? Math.abs(oDivRect.top) : 0;
let iCoverRight = (oDivRect.right - iWidth > 0) ? Math.round(oDivRect.right - iWidth) : 0;
let iCoverBottom = (oDivRect.bottom - iHeight > 0) ? Math.round(oDivRect.bottom - iHeight) : 0;
iCoverLeft = (iCoverLeft > this.width) ? this.width : iCoverLeft;
iCoverTop = (iCoverTop > this.playHeight) ? this.playHeight : iCoverTop;
iCoverRight = (iCoverRight > this.width) ? this.width : iCoverRight;
iCoverBottom = (iCoverBottom > this.playHeight) ? this.playHeight : iCoverBottom;
this.oWebControl.JS_RepairPartWindow(0, 0, (this.width + 1), this.playHeight); // 多1個像素點(diǎn)防止還原后邊界缺失一個像素條
if (iCoverLeft != 0) {
this.oWebControl.JS_CuttingPartWindow(0, 0, iCoverLeft, this.playHeight);
}
if (iCoverTop != 0) {
this.oWebControl.JS_CuttingPartWindow(0, 0, (this.width + 1), iCoverTop); // 多剪掉一個像素條,防止出現(xiàn)剪掉一部分窗口后出現(xiàn)一個像素條
}
if (iCoverRight != 0) {
this.oWebControl.JS_CuttingPartWindow(this.width - iCoverRight, 0, iCoverRight, this.playHeight);
}
if (iCoverBottom != 0) {
this.oWebControl.JS_CuttingPartWindow(0, this.playHeight - iCoverBottom, this.width, iCoverBottom);
}
},
// 拖拽窗口
onmousedown (e) {
let that = this;
let cWidth = document.body.clientWidth;
let cHeight = document.body.clientHeight;
this.$refs.videoPlayerBox.onmousemove = function (el) {
let ev = el || window.event;
ev.preventDefault();
// 解決點(diǎn)擊標(biāo)題窗口抖動的問題
if (Math.abs(ev.movementX) === 0 && Math.abs(ev.movementY) === 0) {
return;
}
that.left += ev.movementX;
that.top += ev.movementY;
// 頂部不能超出,左側(cè)、右側(cè)、底部可以超出一半
if (that.top < 0) {
that.top = 0;
}
if (that.top > (cHeight - that.height / 2)) {
that.top = cHeight - that.height / 2;
}
if (that.left < -that.width / 2) {
that.left = -that.width / 2;
}
if (that.left > (cWidth - that.width / 2)) {
that.left = (cWidth - that.width / 2);
}
that.oWebControl.JS_Resize(that.width, that.playHeight);
}
this.$refs.videoPlayerBox.onmouseup = function () {
this.onmousemove = null;
this.onmouseup = null;
}
// 阻止默認(rèn)事件
if (e.preventDefault) {
e.preventDefault()
} else {
return false
}
},
onmouseleave(e) {
// 拖拽過快鼠標(biāo)劃出標(biāo)題位置,重新拖拽是,鼠標(biāo)還未移入標(biāo)題,由于上一次的onmouseup方法沒有執(zhí)行,導(dǎo)致鼠標(biāo)靠近彈框,彈框移動,解決方法,給onmousemove和onmouseup賦值為null
this.$refs.videoPlayerBox.onmousemove = null;
this.$refs.videoPlayerBox.onmouseup = null;
// 解決拖拽過快,播放器殘影問題
this.oWebControl.JS_Resize(this.width, this.playHeight);
}
},
mounted() {
let hksystem = JSON.parse(sessionStorage.getItem('hksystem'));
this.appkey = hksystem.appKey;
this.secret = hksystem.appSecret;
this.ip = hksystem.host.split(':')[0];
this.port = hksystem.host.split(':')[1];
// 設(shè)置top left
let bodyW = document.body.clientWidth;
let bodyH = document.body.clientHeight;
this.left = bodyW / 2 - this.width / 2;
this.top = bodyH / 2 - this.height / 2;
this.initPlugin(() => {
this.previewVideo();
});
// 監(jiān)聽resize事件,使插件窗口尺寸跟隨DIV窗口變化
$(window).resize(() => {
if (this.oWebControl != null) {
this.oWebControl.JS_Resize(this.width, this.playHeight);
this.setWndCover();
}
});
// 監(jiān)聽滾動條scroll事件,使插件窗口跟隨瀏覽器滾動而移動
$(window).scroll(() => {
if (this.oWebControl != null) {
this.oWebControl.JS_Resize(this.width, this.playHeight);
this.setWndCover();
}
});
// 標(biāo)簽關(guān)閉
$(window).unload(() => {
if (this.oWebControl != null) {
this.oWebControl.JS_HideWnd(); // 先讓窗口隱藏,規(guī)避可能的插件窗口滯后于瀏覽器消失問題
this.oWebControl.JS_Disconnect().then(
() => { // 斷開與插件服務(wù)連接成功
},
() => { // 斷開與插件服務(wù)連接失敗
}
);
}
});
},
watch: {
monitorDeviceNo: {
handler(newV, oldV) {
this.cameraIndexCode = newV;
if (newV && this.playMode === 0) {
this.previewVideo();
} else if (newV && this.playMode === 1) {
this.backVideo();
}
}
}
}
}
</script>
<style lang="stylus" rel="stylesheet/stylus" scoped>
#videoPlayerBox
position fixed
z-index 4012
background-color #0D1F3A
.header
padding 10px 20px
background-color #142C50
position relative
.el-dialog__title
height 24px
text-align center
background-color #142C50
color #fff
font-size 14px
cursor pointer
.closeBtn
position absolute
top 10px
right 10px
padding 2px 4px
border 1px dotted #ccc
color #fff
font-size 18px
cursor pointer
&:hover
color #4BA2FF
.topbutton
height 26px
padding-top 6px
padding-left 14px
background-color #0D1F3A
cursor pointer
.topBtns
padding 0 12px 6px
border-bottom 1px solid #0D1F3A
color #fff
.activeBtn
color #4BA2FF
border-color #4BA2FF
#playBox
width 100%
height 360px
color red
</style>
開發(fā)中遇到的問題及解決方法
1. 創(chuàng)建實(shí)例失敗時重新喚起插件
文檔錯誤 oWebControl.JS_WakeUp("VideoWebPlugin://")
正確方法如下
WebControl.JS_WakeUp("VideoWebPlugin://"); // 程序未啟動時執(zhí)行
2. 產(chǎn)品要求彈框顯示后直接進(jìn)行預(yù)覽,不需要再次點(diǎn)擊
實(shí)現(xiàn)過程
先初始化 this.initPlugin()
再執(zhí)行預(yù)覽方法 this.previewVideo()
未能實(shí)現(xiàn),因?yàn)槌跏蓟椒ㄊ钱惒降?/p>
解決方法,給初始化方法傳遞一個回調(diào)函數(shù)
this.initPlugin(() => {
this.previewVideo();
});
3. 多窗口實(shí)時預(yù)覽實(shí)現(xiàn),效果圖如下
切換到2x2模式,監(jiān)聽父組件傳遞過來的監(jiān)控點(diǎn)編號,調(diào)預(yù)覽方法
watch: {
monitorDeviceNo: {
handler(newV, oldV) {
this.cameraIndexCode = newV;
if (newV && this.playMode === 0) {
this.previewVideo();
} else if (newV && this.playMode === 1) {
this.backVideo();
}
}
}
}
關(guān)閉彈框的是,需要調(diào)用停止所有預(yù)覽的API
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站開發(fā)
- web前端
- 開發(fā)框架
- 電商網(wǎng)站建設(shè)
- 云南網(wǎng)站建設(shè)快速優(yōu)化
- 小程序被騙退款成功
- APP
- 網(wǎng)站建設(shè)方案 doc
- 云南小程序代建
- 云南軟件公司
- 汽車報(bào)廢拆解管理系統(tǒng)
- 網(wǎng)站建設(shè)公司網(wǎng)站
- 汽車回收管理
- 人人商城
- 河南小程序制作
- 曲靖小程序開發(fā)
- 云南小程序商城
- 云南網(wǎng)站建設(shè)專業(yè)品牌
- 云南小程序開發(fā)費(fèi)用
- 開發(fā)微信小程序
- 云南小程序開發(fā)公司
- 云南網(wǎng)站建設(shè)首選
- 汽車報(bào)廢系統(tǒng)
- 云南小程序哪家好
- 百度小程序
- 百度快速排名
- 小程序定制開發(fā)
- 小程序分銷商城
- 云南衛(wèi)視小程序
- 云南網(wǎng)站建設(shè)優(yōu)化