本文出現單單是前端小白蝸牛公司的一個業務場景,因爲以前有了解過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