videoContent.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div class="img-container">
  3. <!-- <img :src="url" v-if="url" /> -->
  4. <canvas class="canvas_box" ref="canvas" v-if="url" width="960" height="540" />
  5. <div class="image-slot" v-else>
  6. <i class="el-icon-video-camera"></i>
  7. <span>无信号</span>
  8. <span>{{ cameraData.name }}</span>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. import { debounce, throttle } from 'lodash-es'
  14. export default {
  15. props: {
  16. cameraData: {
  17. type: Object,
  18. default: () => ({}),
  19. },
  20. },
  21. data() {
  22. return {
  23. url: '',
  24. ws: null,
  25. startTime: null,
  26. speed: 4,
  27. presetIndex: 1,
  28. }
  29. },
  30. watch: {
  31. cameraData: {
  32. handler(val) {
  33. if (val.id) {
  34. this.updateEvent({ id: val.id }, true)
  35. }
  36. },
  37. deep: true,
  38. immediate: true,
  39. },
  40. },
  41. mounted() {
  42. this.$EventBus.$off('updateSendParam')
  43. this.$EventBus.$on('updateSendParam', (data, state) => {
  44. if (data.id === this.cameraData.id) {
  45. this.updateEvent(data, state)
  46. }
  47. })
  48. },
  49. beforeDestroy() {
  50. this.closeHandler()
  51. },
  52. methods: {
  53. init(str) {
  54. // this.socketList[`ws_${this.cameraData.id}`];
  55. const host = VUE_APP_BASE_WS() + '/VideoOverWebSocket'
  56. // const host = 'wss://' + window.location.host + BASE_URL + '/VideoOverWebSocket'
  57. this.ws = new WebSocket(host)
  58. this.ws.onmessage = evt => {
  59. if (typeof evt.data === 'string') {
  60. console.log(JSON.parse(evt.data).Msg)
  61. } else {
  62. this.url = URL.createObjectURL(evt.data)
  63. this.$nextTick(() => {
  64. this.updateUrl()
  65. })
  66. }
  67. }
  68. this.handleSend(str)
  69. // console.log(window);
  70. },
  71. updateUrl() {
  72. const canvas = this.$refs.canvas
  73. const ctx = canvas.getContext('2d')
  74. const img = new Image()
  75. // console.log('canvas', canvas.width, canvas.height)
  76. img.src = this.url
  77. img.onload = () => {
  78. // console.log('img', img.width, img.height)
  79. const canvasRatio = canvas.width / canvas.height
  80. const imgRatio = img.width / img.height
  81. if (imgRatio > canvasRatio) {
  82. // 图片宽高比大于画布宽高比,以宽度为基准进行缩放
  83. const newWidth = canvas.width
  84. const newHeight = newWidth / canvasRatio
  85. ctx.clearRect(0, 0, newWidth, newHeight)
  86. ctx.drawImage(img, 0, (canvas.height - newHeight) / 2, newWidth, newHeight)
  87. } else {
  88. // 图片宽高比小于等于画布宽高比,以高度为基准进行缩放
  89. const newHeight = canvas.height
  90. const newWidth = newHeight * canvasRatio
  91. ctx.clearRect(0, 0, newWidth, newHeight)
  92. ctx.drawImage(img, (canvas.width - newWidth) / 2, 0, newWidth, newHeight)
  93. }
  94. }
  95. },
  96. // 100),
  97. handleSend(str) {
  98. if (this.ws.readyState === WebSocket.OPEN) {
  99. this.ws.send(str)
  100. } else if (this.ws.readyState == WebSocket.CONNECTING) {
  101. // Wait for the open event, maybe do something with promises
  102. // depending on your use case. I believe in you developer!
  103. this.ws.addEventListener('open', () => this.handleSend(str))
  104. } else {
  105. // etc.
  106. }
  107. },
  108. updateEvent(data, state) {
  109. const param = JSON.stringify(data)
  110. if (this.ws && this.ws.readyState == 1) {
  111. this.handleSend(param)
  112. } else {
  113. this.init(param)
  114. }
  115. if (state) {
  116. setTimeout(() => {
  117. this.url = ''
  118. }, 500)
  119. }
  120. },
  121. closeHandler() {
  122. this.ws && this.ws.close()
  123. setTimeout(() => {
  124. if (this.url) URL.revokeObjectURL(this.url)
  125. this.url = ''
  126. this.ws.onclose = evt => {
  127. this.ws = null
  128. console.log('websocket已关闭')
  129. }
  130. }, 500)
  131. },
  132. },
  133. }
  134. </script>
  135. <style lang="less" scoped>
  136. .img-container {
  137. width: 100%;
  138. height: 100%;
  139. img {
  140. width: 100%;
  141. height: 100%;
  142. object-fit: fill;
  143. // display: block;
  144. }
  145. .canvas_box {
  146. width: 100%;
  147. height: 100%;
  148. }
  149. .image-slot {
  150. width: 100%;
  151. height: 100%;
  152. // background-color: #f5f7fa;
  153. color: #5bd6ff;
  154. font-size: 24px;
  155. display: flex;
  156. flex-direction: column;
  157. justify-content: center;
  158. align-items: center;
  159. font-weight: 500;
  160. span {
  161. font-size: 16px;
  162. }
  163. }
  164. }
  165. </style>