index.vue 4.49 KB
<template>
  <div class="vion-player" ref="container"></div>
</template>

<script>
// import { watermarkBase64 } from './data';
import { getWatermarkCanvasImg } from './utils';

export default {
  name: 'VionPlayer',
  props: {
    type: {
      type: String,
      default: 'live', // playback
    },
    playUrl: {
      type: String,
      default: '', // 若配置,则播放器加载后,自动播放
    },
    datetimeRange: {
      type: Array,
      default: null,
    },
    watermarkText: {
      type: String,
      default: '',
    },
    controlAutoHide: {
      type: Boolean,
      default: true,
    },
    showPtz: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      // eslint-disable-next-line
      _jessibuca: null,
    }
  },
  mounted() {
    this.init();
  },
  methods:{
    init(params = {}) {
      const options = Object.assign({
        container: this.$refs.container,
        decoder: '/jessibuca-pro/decoder-pro.js',
        isResize: false,
        text: '', // TODO: 功能暂不清楚
        useMSE: true,
        useWCS: false,
        isNotMute: false,
        timeout: 10,
        keepScreenOn: true, // 屏幕常亮
        debug: false, // 是否开启控制台调试打印

        // 按钮和界面
        loadingText: '加载中...',
        controlAutoHide: this.controlAutoHide,
        supportDblclickFullscreen: false,
        showBandwidth: false, // 显示网速
        // TODO: 水印功能暂不可用
        /* watermarkConfig: {
          image: {
            src: this.getWatermarkBase64(),
            width: 150,
            height: 48
          },
          right: 10,
          top: 30
        }, */
        operateBtns: {
          fullscreen: true,
          screenshot: false,
          play: true,
          audio: true,
          ptz: true, // 云台
          zoom: true,
          performance: false,
        },
        extendOperateBtns: [],
        // 云台控制
        ptzClickType: 'click', // click mouseDownAndUp
        ptzZoomShow: true,
        ptzMoreArrowShow: true,
        ptzApertureShow: true,
        ptzFocusShow: true,
      });

      this._jessibuca = new window.JessibucaPro(options);

      this.registerEvent(this._jessibuca);

      if (this.playUrl) {
        // 示例上有延迟写法
        setTimeout(this.play(this.playUrl), 150);
      }
    },
    registerEvent(playerIns) {
      playerIns.on("log", msg => {
        console.log("on-log", msg);
      });

      playerIns.on('ptz', (arrow) => {
        // console.log('ptz', arrow);
        this.$emit('ptz-control', arrow);
      });
    },
    getWatermarkBase64() {
      const data = getWatermarkCanvasImg(150, 48, '文安智能');
      console.log('getWatermarkBase64', data);
    },

    // 暴露方法
    play(url) {
      if (url) {
        this._jessibuca.play(url);
      } else {
        console.error('The url is not valid');
      }
    },
    pause() {
      this._jessibuca.pause();
    },
    stop() {
      this._jessibuca.pause(true);
    },
    destroy() {
      if (this._jessibuca) {
        this._jessibuca.destroy().then(() => {
          this._jessibuca = null;
        });
      }
    },
    screenshot2(filename = '', format = 'png', quality = 0.92, type = 'base64') {
      // TODO: 必须播放状态,才可获取水印
      const originData = this._jessibuca.screenshot(filename, format, quality, type);
      // TODO: 动态获取宽高
      return getWatermarkCanvasImg(800, 480, '文安智能', originData);
    },
    screenshot(filename = '', format = 'png', quality = 0.92, type = 'base64') {
      return new Promise((resolve, reject) => {
        // 创建一个新的图片对象
        const image = new Image();

        // 当图像加载完成后执行
        image.onload = () => {
          // 创建一个 Canvas 元素
          const canvas = getWatermarkCanvasImg(800, 480, '文安智能', image);;

          // 将带有水印的 Canvas 转换回 base64
          const watermarkedBase64 = canvas.toDataURL('image/png');

          // 现在,watermarkedBase64 包含了带有水印的 base64 图像数据
          // console.log(watermarkedBase64);
          resolve(watermarkedBase64);
        };
        image.error = function() {
          reject(new Error(null));
        }

        // 设置图像的 src 属性,加载图像
        image.src = this._jessibuca.screenshot(filename, format, quality, type);
      });
    },
  },
}
</script>

<style lang="scss" scoped>
.vion-player {
  background-color: rgba(13, 14, 27, 0.7);
}
</style>