webrtc,人脸拍照小功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Zph1234/article/details/54019892
<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8">  
        <title>学习webrtc-chapter2:get user media</title>  
		<style>
		video,canvas{
			border:1px solid gray;
			width:400px;
			height:400px;
			border-radius:50%;
		}	
		</style>
    </head>  
    <body>  
        <video autoplay></video> 
		<canvas id="myCanvas"></canvas>
		<button id="capture">拍照</button>
		<script src="photobooth.js"></script> 

    </body>  
</html>  


function hasUserMedia(){//判断是否支持调用设备api,因为浏览器不同所以判断方式不同哦  
	return !!(navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);  
}  
if(hasUserMedia()){  
	//alert(navigator.mozGetUserMedia)  
	navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;  
	var video=document.querySelector("video");  
	var canvas=document.querySelector("canvas");
	var streaming = false;
	navigator.getUserMedia({  
		video:true,//开启视频  
		audio:false//先关闭音频,因为会有回响,以后两台电脑通信不会有响声  
	},function(stream){//将视频流交给video  
		video.src=window.URL.createObjectURL(stream);   
		streaming = true;
	},function(err){  
		console.log("capturing",err)  
	});  
	document.querySelector("#capture").addEventListener("click",function(event){
		if(streaming){
			//alert(video.clientHeight)
			//canvas.width = video.clientWidth;
			//canvas.height= video.clientHeight;
			canvas.width = 800;
			canvas.height = 800;
			var context = canvas.getContext('2d');
			context.drawImage(video,20,20)
		}
	})
}else{  
	alert("浏览器暂不支持")  
} 








没有更多推荐了,返回首页