web网页调用摄像头拍照
2019-04-29 08:49:42来源:博客园 阅读 ()
页面内容
<video id="video" class="video-bg"></video> <canvas id="qrCanvas" class="qr-canvas"></canvas> <div id="imageWarp" class="image-warp"></div> <button type="button" onclick="window.MyCamera.openMedia()">开启</button> <button type="button" onclick="window.MyCamera.closeMedia()">关闭</button> <button type="button" onclick="window.MyCamera.drawMediaAndToImg()">拍摄</button>
js内容
var CameraInit=(function(window,document,undefined){ function MyCamera(videoDom,canvasDom) { this.mediaOpts = { audio: false, video: true, video: { facingMode: "environment"} // 或者 "user" // video: { width: 1280, height: 720 } // video: { facingMode: { exact: "environment" } }// 或者 "user" } this.video=videoDom; this.canvas1=canvasDom; this.context1 = this.canvas1.getContext('2d'); this.mediaStreamTrack = null; this.checkEnvironment(); } MyCamera.prototype = { checkEnvironment: function() { window.URL = (window.URL || window.webkitURL || window.mozURL || window.msURL); if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {}; } if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = function(constraints) { var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; if (!getUserMedia) { return Promise.reject(new Error('getUserMedia is not implemented in this browser')); } return new Promise(function(resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); } } }, // 回调 successFunc: function(stream) { this.mediaStreamTrack = stream; this.video = document.querySelector('video'); if ("srcObject" in this.video) { this.video.srcObject = stream } else { this.video.src = window.URL && window.URL.createObjectURL(stream) || stream } this.video.play(); }, errorFunc:function(err) { alert(err.name); }, // 正式启动摄像头 openMedia: function(){ navigator.mediaDevices.getUserMedia(this.mediaOpts).then(this.successFunc.bind(this)).catch(this.errorFunc); }, //关闭摄像头 closeMedia: function(){ var that=this; that.mediaStreamTrack.getVideoTracks().forEach(function (track) { track.stop(); that.context1.clearRect(0, 0,that.context1.width, that.context1.height);//清除画布 }); }, //截取视频并转为图片 drawMediaAndToImg: function(){ this.canvas1.setAttribute("width", this.video.videoWidth); this.canvas1.setAttribute("height", this.video.videoHeight); this.context1.drawImage(this.video, 0, 0, this.video.videoWidth, this.video.videoHeight);//显示在canvas中 var image = new Image(); image.src = this.canvas1.toDataURL('image/png') var imageWarp = document.getElementById("imageWarp"); if(imageWarp.hasChildNodes()) { document.getElementById("imageWarp").replaceChild(image, imageWarp.firstChild); }else{ document.getElementById("imageWarp").appendChild(image); } }, }; return MyCamera; })(window,document)
实例化
window.MyCamera=new CameraInit(document.getElementById("video"), document.getElementById("qrCanvas"));
参考内容:https://www.jb51.net/article/160015.htm
原文链接:https://www.cnblogs.com/jjlinsblog/p/10784154.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- NiftyCube实现圆角边框的方法 2020-03-20
- 根据分辨率调用css文件的方法 2020-03-19
- javascript特殊文本输入框网页特效 2020-03-16
- JS实现标签页切换效果 2020-03-12
- js调用刷新界面的几种方式 2020-03-05
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash