vue开启笔记本摄像头,实现录像,截图,预览截图,预览录像,下载截图,下载录像功能

7 篇文章 0 订阅
6 篇文章 0 订阅

vue开启笔记本摄像头,实现录像,截图,预览截图,预览录像,下载截图,下载录像功能

所有代码如下:

<template>
  <div>

    <video class="video" ref="videoElement" autoplay></video>
    <button class="button" @click="toggleCamera">{{ cameraActive ? '关闭摄像头' : '打开摄像头' }}</button>
    <button class="button" @click="takeSnapshot">截图</button>
    <button class="button" @click="toggleRecording">{{ recording ? '停止录制' : '开始录制' }}</button>
    <button class="button" @click="downloadImage">下载图片</button>
    <button class="button" @click="downloadVideo">下载视频</button>

    <canvas ref="canvasElement"></canvas>
    <img :src="snapshotUrl" v-if="snapshotUrl" alt="截图预览">
    <video :src="recordedVideoUrl" v-if="recordedVideoUrl" autoplay controls></video>
  </div>
</template>


<script>
export default {
  data() {
    return {
      cameraActive: false,
      recording: false,
      snapshotUrl: '',
      recordedVideoUrl: '',
      stream: null,
      videoElement: null,
      canvasElement: null,
      mediaRecorder: null,
      chunks: [],

      rotate: {
        x: 0,
        y: 0,
      },
    };
  },
  mounted() {
    this.videoElement = this.$refs.videoElement;
    this.canvasElement = this.$refs.canvasElement;
  },
  methods: {
    toggleCamera() {
      if (!this.cameraActive) {
        navigator.mediaDevices.getUserMedia({ video: true })
          .then((stream) => {
            this.stream = stream;
            this.videoElement.srcObject = stream;
            this.cameraActive = true;
          })
          .catch((error) => {
            console.error('无法打开摄像头:', error);
          });
      } else {
        this.videoElement.srcObject = null;
        this.stream.getTracks().forEach((track) => {
          track.stop();
        });
        this.cameraActive = false;
      }
    },
    takeSnapshot() {
      const context = this.canvasElement.getContext('2d');
      context.drawImage(this.videoElement, 0, 0, this.canvasElement.width, this.canvasElement.height);
      this.snapshotUrl = this.canvasElement.toDataURL();
    },
    toggleRecording() {
      if (!this.recording) {
        this.startRecording();
      } else {
        this.stopRecording();
      }
    },
    startRecording() {
      if (MediaRecorder.isTypeSupported('video/webm')) {
        this.chunks = [];
        this.mediaRecorder = new MediaRecorder(this.stream, { mimeType: 'video/webm' });
        this.mediaRecorder.ondataavailable = (e) => {
          if (e.data && e.data.size > 0) {
            this.chunks.push(e.data);
          }
        };
        this.mediaRecorder.onstop = () => {
          const recordedBlob = new Blob(this.chunks, { type: 'video/webm' });
          this.chunks = [];
          this.recordedVideoUrl = URL.createObjectURL(recordedBlob);
        };
        this.mediaRecorder.start();
        this.recording = true;
      } else {
        console.error('不支持录制视频');
      }
    },
    stopRecording() {
      if (this.recording) {
        this.mediaRecorder.stop();
        this.mediaRecorder = null;
        this.recording = false;
      }
    },
    downloadImage() {
      if (this.snapshotUrl) {
        const a = document.createElement('a');
        a.href = this.snapshotUrl;
        a.download = 'snapshot.png';
        a.click();
      }
    },
    downloadVideo() {
      if (this.recordedVideoUrl) {
        const a = document.createElement('a');
        a.href = this.recordedVideoUrl;
        a.download = 'recorded_video.webm';
        a.click();
      }
    },
  }
};
</script>


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ezuikit-js是一个基于Vue框架的摄像头调用录像功能实现的插件。以下是一种可能的实现方式: 首先,我们需要在Vue项目中安装ezuikit-js插件。可以通过npm命令来进行安装: ``` npm install ezuikit-js ``` 然后,在Vue组件中引入和使用ezuikit-js插件: ```javascript import ezuikit from 'ezuikit-js'; export default { data() { return { player: null, isRecording: false, }; }, mounted() { this.initPlayer(); }, methods: { initPlayer() { this.player = new ezuikit.Player({ id: 'video-player', accessToken: 'your_access_token', url: 'rtsp://your_camera_url', }); this.player.initialize(); }, startRecording() { this.isRecording = true; this.player.startRecord(); }, stopRecording() { this.player.stopRecord().then((recordData) => { console.log(recordData); // 可以在控制台输出录像数据(recordData) this.isRecording = false; }); }, }, }; ``` 上述代码中,我们首先在`mounted()`生命周期方法中初始化ezuikit的播放器。然后,我们可以在组件的模板中使用一个`video`标签来显示视频流,并将该标签的`id`设置为"video-player"。接下来,我们通过调用ezuikit的`startRecord()`方法和`stopRecord()`方法来开始和停止录像。在停止录像时,`stopRecord()`方法会返回录像的数据(recordData),可以用于后续的处理和保存。 需要注意的是,上述代码中的`your_access_token`和`your_camera_url`需要被替换为实际的访问令牌和摄像头的URL。 这只是一种实现方式,具体的实现还取决于项目的具体需求和摄像头的接口要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值