首页 > 学院 > 开发设计 > 正文

webrtc,人脸拍照小功能

2019-11-14 10:28:11
字体:
来源:转载
供稿:网友
<!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><script>        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("浏览器暂不支持")        }   </script>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表