通过getUserMedia
方法获取用户媒体设备(摄像头)的视频流,将视频帧绘制到canvas上
<template>
<div>
<video ref="videoElement" width="640" height="480" autoplay></video>
<button @click="takeScreenshot">Take Screenshot</button>
<canvas ref="canvasElement" width="640" height="480"></canvas>
</div>
</template>
<script>
export default {
mounted() {
// 获取video和canvas元素
this.video = this.$refs.videoElement;
this.canvas = this.$refs.canvasElement;
this.context = this.canvas.getContext('2d');
// 获取用户媒体设备(摄像头)
navigator.mediaDevices.getUserMedia({ video: true })
.then(stream => {
this.video.srcObject = stream;
})
.catch(error => {
console.error('Error accessing media devices.', error);
});
},
methods: {
takeScreenshot() {
// 将视频帧绘制到canvas上
this.context.drawImage(this.video, 0, 0, this.canvas.width, this.canvas.height);
// 获取截取的图片数据URL
const screenshotDataUrl = this.canvas.toDataURL();
// 在控制台输出截取的图片数据URL
console.log(screenshotDataUrl);
// 可以将截取的图片数据URL发送到服务器保存或显示在页面上等操作
}
}
}
</script>