<template>
|
<div class="video-player">
|
<canvas id="area-canvas" ref="areaCanvas" ></canvas>
|
<video ref="videoPlayer" :poster="poster" preload="auto" muted></video>
|
<div class="controls">
|
<!-- 全屏 -->
|
<i class="el-icon-full-screen" @click="fullScreen"></i>
|
</div>
|
</div>
|
</template>
|
<script>
|
import Wfs from "./wfs";
|
import VideoRuleData from "@/Pool/VideoRuleData";
|
import TreeDataPool from "@/Pool/TreeData";
|
import { getAllPolygon } from "@/api/polygon";
|
import polygonCanvas from "@/components/canvas";
|
export default {
|
name: "CameraPlayer",
|
props: {
|
wsAddr: {
|
type: String,
|
// default: "ws://192.168.1.182:10101/ws"
|
default: `${location.protocol === "https" ? "wss" : "ws"}://${location.host}/ws`
|
},
|
cameraName: {
|
type: String,
|
default: ""
|
},
|
cameraID: {
|
type: String,
|
default: "C4668FD0-3CAE-C31F-C21E-28B7001243C4"
|
},
|
rtspUrl: {
|
type: String,
|
default: "rtsp://admin:a1234567@192.168.1.201:554/h264/ch1/main/av_stream"
|
},
|
isRunning: {
|
type: Boolean,
|
default: false
|
},
|
isGb: {
|
type: Boolean,
|
default: false
|
}
|
},
|
|
computed: {
|
|
poster() {
|
return "/images/player/player_poster.gif?t=" + Math.random()
|
}
|
},
|
data() {
|
return {
|
wfs: {},
|
wfsId: 0,
|
Camera: new VideoRuleData(),
|
showCanvas: true,
|
canvasData: {
|
line: [],
|
rect: [], // {id:'uuid', name: '矩形1', location: [{ x: 20, y: 30 }, { x: 20, y: 60 }, { x: 100, y: 60 }, { x: 100, y: 30 }] }
|
arrow: [],
|
polygon: []
|
},
|
showProportion: 3.65,
|
canvas: null,
|
ctx: null
|
};
|
},
|
watch: {
|
// "TreeDataPool.selectedNode": async function(node){
|
// if(node){
|
// console.log('TreeDataPool.selectedNode')
|
// console.log(node)
|
// //this.Camera.cameraId = node.id;
|
// const res = await getAllPolygon({cameraId: node.id});
|
|
// this.canvasData.line = res.data.line;
|
// this.canvasData.rect = res.data.rect;
|
// this.canvasData.arrow = res.data.arrow;
|
// this.canvasData.polygon = res.data.polygon;
|
// console.log(this.canvasData)
|
// this.clickSelect(this.canvasData);
|
// }
|
// },
|
|
rtspUrl: function (newVal, oldVal) {
|
if (newVal !== oldVal) {
|
if (this.wfs.config) {
|
this.wfs.destroy();
|
}
|
this.$nextTick(() => {
|
this.clickStart();
|
});
|
}
|
}
|
},
|
methods: {
|
checkConnect(id) {
|
// console.log(this.wfs)
|
if (id !== this.wfsId) {
|
return
|
}
|
|
if (this.wfs.websocketLoader && this.wfs.websocketLoader.client) {
|
if (this.wfs.websocketLoader.client.disconnected) {
|
this.clickStart();
|
console.log("实时视频已断开,正在重连")
|
return
|
}
|
}
|
|
let _this = this;
|
setTimeout(() => {
|
_this.checkConnect(id)
|
}, 10000)
|
},
|
clickStart() {
|
if (this.rtspUrl == "") {
|
return;
|
}
|
|
let cameraId = this.cameraID;
|
if (cameraId == "") {
|
cameraId = this.getUuid();
|
}
|
|
if (Wfs.isSupported()) {
|
let wsAddr = this.wsAddr;
|
let cameraInfo = {
|
cameraID: cameraId,
|
rtspUrl: this.rtspUrl,
|
isRunning: this.isRunning,
|
isGb28181: this.isGb
|
};
|
|
// let camera = document.getElementById(this.cameraID);
|
let camera = this.$refs.videoPlayer;
|
this.wfs = new Wfs();
|
let randomId = this.getUuid();
|
this.wfsId = randomId;
|
this.wfs.attachMedia(camera, "chX", "H264Raw", wsAddr, cameraInfo);
|
|
this.checkConnect(randomId)
|
}
|
},
|
getUuid() {
|
var s = [];
|
var hexDigits = "0123456789abcdefghijkopqrst";
|
for (var i = 0; i < 36; i++) {
|
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
|
}
|
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
|
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
|
s[8] = s[13] = s[18] = s[23] = "-";
|
var uuid = s.join("");
|
return uuid;
|
},
|
fullScreen() {
|
this.$refs.videoPlayer.webkitRequestFullScreen();
|
},
|
// 回显cavas数据
|
// 点击选中变色 将当前页面所有路径重绘判断当前鼠标的坐标在哪个图形内 如果不传坐标参数就是回显的方法
|
clickSelect(e) {
|
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
let _this = this; // 集合中遍历需要将this转存一下使用
|
_this.canvasData.line.forEach(function (v, i) {
|
_this.ctx.strokeStyle = "yellow";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion);
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "default";
|
if (e && _this.ctx.isPointInStroke(e.offsetX, e.offsetY)) {
|
// 如果传入了事件坐标,就用isPointInStroke判断一下
|
// 如果当前环境覆盖了该坐标,就将图形的index放到数组里
|
// 当鼠标移入之后将当前的模式切换为选中模式
|
_this.type = "0";
|
_this.delCursor.type = "1";
|
_this.delCursor.index = i;
|
// 将当前元素标红
|
_this.ctx.strokeStyle = "red";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion);
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "pointer";
|
}
|
});
|
_this.canvasData.rect.forEach(function (v, i) {
|
_this.ctx.strokeStyle = "yellow";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[2].x / _this.showProportion, v.location[2].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[3].x / _this.showProportion, v.location[3].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "default";
|
if (e && _this.ctx.isPointInPath(e.offsetX, e.offsetY)) {
|
// 如果传入了事件坐标,就用isPointInStroke判断一下
|
// 当鼠标移入之后将当前的模式切换为选中模式
|
_this.type = "0";
|
_this.delCursor.type = "2";
|
_this.delCursor.index = i;
|
// 将当前元素标红
|
_this.ctx.strokeStyle = "red";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[2].x / _this.showProportion, v.location[2].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[3].x / _this.showProportion, v.location[3].y / _this.showProportion);
|
_this.ctx.lineTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "pointer";
|
}
|
});
|
_this.canvasData.arrow.forEach(function (v, i) {
|
_this.ctx.strokeStyle = "yellow";
|
// _this.ctx.beginPath()
|
// _this.ctx.moveTo(v.location[0].x / 2, v.location[0].y / 2)
|
// _this.ctx.lineTo(v.location[1].x / 2, v.location[1].y / 2)
|
// _this.ctx.stroke()
|
_this.drawArrow(
|
_this.ctx,
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.location[1].x / _this.showProportion,
|
v.location[1].y / _this.showProportion,
|
20,
|
30,
|
"yellow"
|
); // 绘制方法
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "default";
|
if (e && _this.ctx.isPointInStroke(e.offsetX, e.offsetY)) {
|
// 如果传入了事件坐标,就用isPointInStroke判断一下
|
// 如果当前环境覆盖了该坐标,就将图形的index放到数组里
|
// 当鼠标移入之后将当前的模式切换为选中模式
|
_this.type = "0";
|
_this.delCursor.type = "4";
|
_this.delCursor.index = i;
|
// 将当前元素标红
|
_this.ctx.strokeStyle = "red";
|
// _this.ctx.beginPath()
|
// _this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion)
|
// _this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion)
|
// _this.ctx.stroke()
|
_this.drawArrow(
|
_this.ctx,
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.location[1].x / _this.showProportion,
|
v.location[1].y / _this.showProportion,
|
20,
|
30,
|
"red"
|
); // 绘制方法
|
_this.showRemarks(
|
v.location[0].x / _this.showProportion,
|
v.location[0].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "pointer";
|
}
|
});
|
_this.canvasData.polygon.forEach(function (v, i) {
|
if (v.location.length === 0) {
|
return;
|
}
|
_this.ctx.strokeStyle = "yellow";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
for (let i = 1; i < v.location.length; i++) {
|
_this.ctx.lineTo(v.location[i].x / _this.showProportion, v.location[i].y / _this.showProportion);
|
}
|
_this.ctx.closePath();
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[v.location.length - 1].x / _this.showProportion,
|
v.location[v.location.length - 1].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "default";
|
if (e && _this.ctx.isPointInPath(e.offsetX, e.offsetY)) {
|
// 如果传入了事件坐标,就用isPointInStroke判断一下
|
// 如果当前环境覆盖了该坐标,就将图形的index放到数组里
|
// 当鼠标移入之后将当前的模式切换为选中模式
|
_this.type = "0";
|
_this.delCursor.type = "5";
|
_this.delCursor.index = i;
|
// 将当前元素标红
|
_this.ctx.strokeStyle = "red";
|
_this.ctx.beginPath();
|
_this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion);
|
for (let i = 1; i < v.location.length; i++) {
|
_this.ctx.lineTo(v.location[i].x / _this.showProportion, v.location[i].y / _this.showProportion);
|
}
|
_this.ctx.closePath();
|
_this.ctx.stroke();
|
_this.showRemarks(
|
v.location[v.location.length - 1].x / _this.showProportion,
|
v.location[v.location.length - 1].y / _this.showProportion,
|
v.name
|
);
|
_this.canvas.style.cursor = "pointer";
|
}
|
});
|
},
|
// 回显图形备注
|
showRemarks(x, y, remarks) {
|
this.ctx.moveTo(x, y - 10); // 因为放大之后是y-20,所以缩小版的为y-10
|
this.ctx.fillStyle = "green"; // 设置填充颜色为绿色
|
this.ctx.font = '10px "微软雅黑"'; // 设置字体
|
this.ctx.textBaseline = "bottom"; // 设置字体底线对齐绘制基线
|
this.ctx.textAlign = "left"; // 设置字体对齐的方式
|
this.ctx.fillText(remarks, x, y - 10); // 填充文字
|
},
|
getCanvasData(data) {
|
let polyon = { ...data };
|
polyon.camera_id = this.Camera.cameraId;
|
savePolygon(polyon).then(rsp => {
|
this.Camera.getPolygon();
|
this.Camera.getCameraTask();
|
});
|
},
|
async initCanvas(id){
|
|
//this.canvas = document.querySelector("#area-canvas");
|
this.canvas = this.$refs.areaCanvas;
|
this.ctx = this.canvas.getContext("2d");
|
|
// this.Camera.getPolygon();x:758,y:280
|
// console.log(this.Camera.canvasData);
|
const rsp = await getAllPolygon({ cameraId: id });
|
debugger
|
if (rsp && rsp.success) {
|
this.canvasData = {
|
line: rsp.data.line,
|
arrow: rsp.data.arrow,
|
polygon: [{id:'uuid', name: '多边形1', location: [{ x: 735, y: 155 }, { x: 830, y: 156 }, { x: 809, y: 351 }, { x: 809, y: 364 }, { x: 753, y: 396 }, { x: 755, y: 520 },{ x: 706, y: 524 },{ x: 722, y: 392 },{x:758,y:280},{ x: 735, y: 155 }] }],
|
//rect: rsp.data.rect
|
rect: [{id:'uuid', name: '矩形1', location: [{ x: 20, y: 30 }, { x: 20, y: 60 }, { x: 100, y: 60 }, { x: 100, y: 30 }] }]
|
};
|
}
|
debugger
|
this.clickSelect(this.canvasData);
|
},
|
async initArea(){
|
console.log('init')
|
const res = await getAllPolygon({cameraId: this.TreeDataPool.selectedNode.id});
|
this.canvasData.line = res.data.line;
|
this.canvasData.rect = res.data.rect;
|
this.canvasData.arrow = res.data.arrow;
|
this.canvasData.polygon = res.data.polygon;
|
console.log(this.canvasData)
|
// this.canvasData = {
|
// line: [],
|
// arrow: [],
|
// polygon: [{id:'uuid', name: '多边形1', location: [{ x: 735, y: 155 }, { x: 830, y: 156 }, { x: 809, y: 351 }, { x: 809, y: 364 }, { x: 753, y: 396 }, { x: 755, y: 520 },{ x: 706, y: 524 },{ x: 722, y: 392 },{x:758,y:280},{ x: 735, y: 155 }] }],
|
// //rect: rsp.data.rect
|
// rect: []
|
// };
|
this.clickSelect(this.canvasData);
|
}
|
},
|
mounted() {
|
this.clickStart();
|
this.$nextTick(()=>{
|
this.canvas = this.$refs.areaCanvas;
|
this.ctx = this.canvas.getContext("2d");
|
this.ctx.lineWidth = '1'
|
this.initArea()
|
|
})
|
},
|
beforeDestroy() {
|
this.wfs.destroy();
|
this.wfsId = "";
|
}
|
};
|
</script>
|
|
<style lang="scss">
|
#area-canvas{
|
width: 100%;
|
height: 100%;
|
background: transparent;
|
position: absolute;
|
top: 0;
|
left: 0;
|
}
|
// .cavas{
|
// width: 100%;
|
// height: 100%;
|
// position: absolute;
|
// top: 0;
|
// left: 0;
|
// canvas{
|
// background: transparent!important;
|
// }
|
// }
|
video {
|
object-fit: fill;
|
width: 100%;
|
height: 100%;
|
}
|
.video-player {
|
position: relative;
|
width: 100%;
|
height: 100%;
|
.controls {
|
display: none;
|
position: absolute;
|
bottom: 0%;
|
color: #fff;
|
width: 100%;
|
height: 10%;
|
background: -webkit-linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.9));
|
|
i {
|
float: right;
|
font-size: 15px;
|
margin-right: 7%;
|
cursor: pointer;
|
}
|
i:hover {
|
color: rgb(236, 235, 235);
|
-moz-box-shadow: 2px 2px 7px #000;
|
-webkit-box-shadow: 2px 2px 7px #000;
|
box-shadow: 2px 2px 7px #000;
|
}
|
}
|
}
|
|
.video-player:hover {
|
.controls {
|
display: block;
|
}
|
}
|
</style>
|