// 调用摄像头权限
getCompetence() {
this.visible = true;
this.$nextTick(() => {
const _this = this;
this.thisCancas = document.getElementById('canvasCamera');
this.thisContext = this.thisCancas.getContext('2d');
this.thisVideo = document.getElementById('videoCamera');
// 旧版本浏览器可能根本不支持mediaDevices,我们首先设置一个空对象
if (navigator.mediaDevices === undefined) {
navigator.menavigatordiaDevices = {}
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
let getUserMedia = navigator['webkitGetUserMedia'] || navigator['mozGetUserMedia'] || navigator.getUserMedia;
if (!getUserMedia) {
return Promise.reject(new Error())
}
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject)
})
}
}
const constraints = {audio: false, video: true};
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
if ('srcObject' in _this.thisVideo) {
_this.thisVideo.srcObject = stream
} else {
_this.thisVideo.src = window.URL.createObjectURL(stream)
}
_this.thisVideo.onloadedmetadata = function () {
_this.thisVideo.play();
_this.confirmLoading = false;
}
}).catch(() => {
_this.$message.info("摄像头调取失败!");
_this.confirmLoading = false;
})
});
},
//绘制图片
drawImage() {
// 点击,canvas画图
this.thisContext.drawImage(this.thisVideo, 0, 0, this.videoWidth, this.videoHeight);
this.imgSrc = this.thisCancas.toDataURL('image/png', 0.5);
this.thisVideo.pause();
},
//清空画布
clearCanvas(id) {
let c = document.getElementById(id);
let cxt = c.getContext("2d");
cxt.clearRect(0, 0, c.width, c.height);
},
//重置画布
resetCanvas() {
this.imgSrc = "";
this.clearCanvas('canvasCamera');
this.getCompetence();
},
//关闭摄像头
stopNavigator() {
if (this.thisVideo && this.thisVideo.srcObject) {
this.thisVideo.srcObject.getTracks()[0].stop();
}
},
/*调用摄像头拍照结束*/
2020-11-01 js调用摄像头js
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...