hanbaoshan
2020-09-08 35bc7a5d2c907d2d7ca35f518dd1f96b434068b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<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>