本文出现单单是前端小白蜗牛公司的一个业务场景,因为以前有了解过navigator这个API,因此在公司微信产品场景上作的一次尝试。若是本文引发你任何的不适,请迅速撤离!!!前端
老板:咱们须要上线一个一键公司注册,自动拍摄身份证的,你去搞个页面出来!vue
我:好的老板,使命必达(ps:内心一万头。。。路过)!!! 既然有了需求,咱们就开始撸文档,撸百度,撸掘金,撸谷歌。vue-cli
咱们先来看效果图:canvas
updated() {
this.video = this.$refs.video;
this.canvas = this.$refs.cnavas; //能够把视频画一帧出来,做为预览图和上传图片
console.log("this.vidoe", this.video);
},
复制代码
let that = this;
let constraints = { video: { facingMode: { exact: "environment" } } };
/**
后置摄像头设置,
*/
navigator.mediaDevices
.getUserMedia(constraints)
.then(function(stream) {
that.mediaStreamTrack = stream.getTracks()[0];
/* 使用这个stream stream */
that.openVideo = true;
if (that.openVideo) { //这里是状态判断 显示页面的video标签,把流传过去。
that.$nextTick(() => {
that.video.srcObject = stream;//that.video是video标签节点,请自行获取节点,updated周期里能够拿到!
that.video.play();
});
}
})
.catch(function(err) {
/* 处理error */
console.log("error");
});
}
复制代码
界面须要有:api
<input
type="file"
id="file"
accept="image/*"
capture="camera"
style="display:none"
@change="savePic"
>
复制代码
js浏览器
let events = navigator.userAgent;
if (events.indexOf("iPhone") > -1) {
document.getElementById("file").click();
} else {
navigator.mediaDevices
.getUserMedia(constraints)
.then(function(stream) {
that.mediaStreamTrack = stream.getTracks()[0];
/* 使用这个stream stream */
that.openVideo = true;
if (that.openVideo) {
that.$nextTick(() => {
that.video.srcObject = stream;
that.video.play();
});
}
})
.catch(function(err) {
/* 处理error */
console.log("error");
});
}
复制代码
最后最后,本次是蜗牛在掘金第一次写文,潜水潜了2年多,但愿在这个社区能结实到一块儿研究前端的同窗。 微信号:lmhone1024bash