asyncOpen() {
return new Promise(resolve => {
navigator.mediaDevices
.getUserMedia(Camera.constraints)
.then(MediaStream => {
// 媒体流跟踪,用于后续关闭
this.MediaStreamTrack =
typeof stream.stop === 'function' ? stream : stream.getTracks()[0];
this.video.srcObject = MediaStream;
this.video.play();
resolve(true);
})
.catch(err => {
resolve(false);
});
});
}
close() {
this.MediaStreamTrack && this.MediaStreamTrack.stop();
}
getBase64Image() {
if (!this.canvas) {
initCanvas();
}
const ctx = this.canvas.getContext('2d');
ctx.drawImage(video, 0, 0, Camera.constraints.width, Camera.constraints.height);
return this.canvas.toDataURL();
}
initCanvas() {
this.canvas = document.createElement('canvas');
this.canvas.width = Camera.constraints.width;
this.canvas.height = Camera.constraints.height;
this.canvas.style.cssText = 'display:none;';
document.body.appendChild(this.canvas);
}