[Vue] Use Vue to call up the camera, take pictures and save them locally

1. Using Vue.js

Save the web content Ctrl+s to the local and add it to the project

https://cdn.jsdelivr.net/npm/vue/dist/vue.jshttps://cdn.jsdelivr.net/npm/vue/dist/vue.js

2. Create a directory

3. Realize:

1. index.html

 Code:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="vue.js"></script>
	</head>
	<body>
		
		<div id="vueapp">
		<video ref="video" autoplay width="400" height="300"></video>
		<button @click="btnTakePhotoClicked()">Take photo</button>
		<canvas ref="canvas"width="400" height="300"></canvas>
		
		<a href="" download="canvas.jpeg" id="save_herf">
		        <img src="" id="save_img" alt="">
		</a>
		</div>
		
		<script src="app.js"></script>
		
	</body>
</html>

2. app.js

Code: 


new Vue({
	el:"#vueapp",
	mounted(){
		this._initVueApp();
		this.btnTakePhotoClicked();
	},
	
	methods:{
		async _initVueApp(){
			this.$refs.video.srcObject= await navigator.mediaDevices.getUserMedia({video:true,audio:false});			
			this._context2d=this.$refs.canvas.getContext("2d");
			this.canvas=this.$refs.canvas;
		},
		
		btnTakePhotoClicked(){
			this._context2d.drawImage(this.$refs.video,0,0,400,300)
			var img = document.createElement("img"); // 创建img元素
			img.src =this.canvas.toDataURL("image/png"); // 截取视频第一帧
			var svaeHref = document.getElementById("save_herf");
			console.log(img.src)
			
			var sd=document.getElementById("save_img");
			svaeHref.href = img.src;
			sd.src=img.src
			
		},
	}
});

4. Effects

Guess you like

Origin blog.csdn.net/m0_56233309/article/details/124058122