当前位置:   article > 正文

uniapp或vue开发h5页面调用摄像头并且实现自动截图_uniapp 自动拍照

uniapp 自动拍照

一定是在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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/964141
推荐阅读
相关标签
  

闽ICP备14008679号