在网站上,我需要从用户的相机中捕获照片,然后下载。这是我当前的代码(我正在使用Vue):
initCamera() {
let video = this.$refs.video;
return new Promise((resolve, reject) => {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia(this.constraints)
.then(stream => {
video.src = window.URL.createObjectURL(stream);
video.play();
resolve(stream);
})
.catch(error => reject(error));
}
});
},
capture(badgeId) {
let video = this.$refs.video;
let canvas = this.$refs.canvas;
let context = canvas.getContext("2d");
context.drawImage(video, 0, 0, canvas.width, canvas.height);
let link = document.createElement("a");
link.download = badgeId + "_" + Date.now().toString();
link.href = canvas.toDataURL("image/jpeg");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
创建页面时调用 initCamera()
,而用户单击时调用capture()
。这适用于Windows和android设备上的网络摄像头。但是在IOS(Safari和chrome)上,而不是下载图片浏览器,只需打开一个新标签页,其中包含window.URL.createObjectURL()
中的字符串作为url。为什么它不能在IOS上运行?