一定是在https环境下

<template>
	<view class="container">
		<video :src="videoSrc" autoplay ref="video" :controls="false" objectFit="fill"></video>
		<img :src="imgUrl" alt="" />
		<button @click="canvas1">拍照</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				videoSrc: '',
				video: '',
				imgUrl: ''
			};
		},
		mounted() {
			this.video = document.querySelector('video')
			console.log(this.video, "viede");
			this.connectCamera();
		},
		methods: {
			connectCamera(groupId) {
				navigator.mediaDevices.getUserMedia({
						video: true
					})
					.then((stream) => {
						this.video.srcObject = stream;
						this.video.onloadedmetadata = (e) => {
							this.video.play();
							setTimeout(() => {
								this.canvas1()
							}, 5000)
						};
					})
					.then((devices) => {
						// 处理设备列表
						console.log(devices);
					})
					.catch((err) => {
						console.error('获取设备失败:', err);
					});
			},
			connectCamera2(groupId) {
				console.log(groupId, "groupid");
				navigator.mediaDevices
					.getUserMedia({
						video: {
							groupId,
							width: 800,
							height: 600
						},
					})
					.then((stream) => {
						console.log(stream, 'stream');
						this.video.srcObject = stream;
						// 定时五秒自动拍照
						this.video.onloadedmetadata = (e) => {
							this.video.play();
							setTimeout(() => {
								this.canvas1()
							}, 5000)
						};
					})
					.catch(console.log);
			},
			canvas1() {
				let video = document.getElementsByTagName("video")[0] // 获取video节点
				let canvas = document.createElement("canvas") // 创建canvas节点
				let w = video?.clientWidth
				let h = video?.clientHeight
				canvas.width = w
				canvas.height = h // 设置宽高
				const ctx = canvas.getContext("2d")
				ctx?.drawImage(video, 0, 0, w, h) // video写入到canvas  clientHeight
				let imgUrl = canvas.toDataURL("image/png") // 生成截图地
				this.imgUrl = imgUrl
			}
		}
	};
</script>
GitHub 加速计划 / vu / vue
207.55 K
33.66 K
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:3 个月前 )
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 5 个月前
e428d891 Updated Browser Compatibility reference. The previous currently returns HTTP 404. 5 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐