前言
前些天,線上筆試的時候,發現需要瀏覽器同意開啟攝像頭,感覺像是 js 呼叫的,由於當時筆試,也就沒想到這麼多?。今天閒來無事,看了下自己的 todo,發現有這個呼叫攝像頭的todo,才想到?。網上查了一下,果然 js 有呼叫攝像頭的 api,為此自己寫一個 demo ,避免忘記。
正文
呼叫攝像頭
一共有兩種實現方式,一種是使用navigator.getUserMedia
(該特性已經從 Web 標準中刪除,雖然一些瀏覽器目前仍然支援它,但也許會在未來的某個時間停止支援,請儘量不要使用該特性),前面一種已經從 Web 標準中刪除,僅為了向後相容而存在,第二種是使用navigator.mediaDevices.getUserMedia
(推薦使用),這兩種方法 Safari 貌似都不支援。。。。
- 第一種方法
navigator.getUserMedia
用法詳見 mdn ,程式碼如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>攝像頭呼叫1</title>
</head>
<body>
<video id="v"></video>
<script>
!(function () {
function userMedia() {
return navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia || null;
}
if (userMedia()) {
var constraints = {
video: true,
audio: false
};
var media = navigator.getUserMedia(constraints, function (stream) {
var v = document.getElementById(`v`);
var url = window.URL || window.webkitURL;
v.src = url ? url.createObjectURL(stream) : stream;
v.play();
}, function (error) {
console.log("ERROR");
console.log(error);
});
} else {
console.log("不支援");
}
})();
</script>
</body>
</html>
- 第二種方法
navigator.mediaDevices.getUserMedia
用法詳見mdn。navigator.mediaDevices.getUserMedia
其實和第一種差不多,主要第二種返回是一個 Promise 物件,程式碼如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>攝像頭呼叫2</title>
</head>
<body>
<video id="v"></video>
<script>
!(function () {
// 老的瀏覽器可能根本沒有實現 mediaDevices,所以我們可以先設定一個空的物件
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// 首先,如果有getUserMedia的話,就獲得它
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// 一些瀏覽器根本沒實現它 - 那麼就返回一個error到promise的reject來保持一個統一的介面
if (!getUserMedia) {
return Promise.reject(new Error(`getUserMedia is not implemented in this browser`));
}
// 否則,為老的navigator.getUserMedia方法包裹一個Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
}
}
const constraints = {
video: true,
audio: false
};
let promise = navigator.mediaDevices.getUserMedia(constraints);
promise.then(stream => {
let v = document.getElementById(`v`);
// 舊的瀏覽器可能沒有srcObject
if ("srcObject" in v) {
v.srcObject = stream;
} else {
// 防止再新的瀏覽器裡使用它,應為它已經不再支援了
v.src = window.URL.createObjectURL(stream);
}
v.onloadedmetadata = function (e) {
v.play();
};
}).catch(err => {
console.error(err.name + ": " + err.message);
})
})();
</script>
</body>
</html>
拍照
思路是設定一個標誌變數 videoPlaying 看看是否 video 有在 play,監聽拍照按鈕的點選事件,如果videoPlaying 為 true ,使用一個canvas 獲取 video 的寬高(預設 canvas 是不顯示的),然後使用 canvas 的drawImage,然後使用 canvas 的 toDataURL返回一個 data url,將這個 url,設定在一個 img 標籤上即可?
- 第一種方法
navigator.getUserMedia
實現程式碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拍照1</title>
</head>
<body>
<button id="take">拍照</button>
<br />
<video id="v" style="width: 640px;height: 480px;"></video>
<canvas id="canvas" style="display:none;"></canvas>
<br />
<img src="http://placehold.it/640&text=Your%20image%20here%20..." id="photo" alt="photo">
<script>
!(function () {
function userMedia() {
return navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia || null;
}
if (userMedia()) {
let videoPlaying = false;
let constraints = {
video: true,
audio: false
};
let video = document.getElementById(`v`);
let media = navigator.getUserMedia(constraints, function (stream) {
let url = window.URL || window.webkitURL;
video.src = url ? url.createObjectURL(stream) : stream;
video.play();
videoPlaying = true;
}, function (error) {
console.log("ERROR");
console.log(error);
});
document.getElementById(`take`).addEventListener(`click`, function () {
if (videoPlaying) {
let canvas = document.getElementById(`canvas`);
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext(`2d`).drawImage(video, 0, 0);
let data = canvas.toDataURL(`image/webp`);
document.getElementById(`photo`).setAttribute(`src`, data);
}
}, false);
} else {
console.log("不支援");
}
})();
</script>
</body>
</html>
第二種navigator.mediaDevices.getUserMedia
實現方法:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>拍照2</title>
</head>
<body>
<button id="take">拍照</button>
<br />
<video id="v" style="width: 640px;height: 480px;"></video>
<canvas id="canvas" style="display:none;"></canvas>
<br />
<img src="http://placehold.it/640&text=Your%20image%20here%20..." id="photo" alt="photo">
<script>
!(function () {
// 老的瀏覽器可能根本沒有實現 mediaDevices,所以我們可以先設定一個空的物件
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// 首先,如果有getUserMedia的話,就獲得它
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// 一些瀏覽器根本沒實現它 - 那麼就返回一個error到promise的reject來保持一個統一的介面
if (!getUserMedia) {
return Promise.reject(new Error(`getUserMedia is not implemented in this browser`));
}
// 否則,為老的navigator.getUserMedia方法包裹一個Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
}
}
const constraints = {
video: true,
audio: false
};
let videoPlaying = false;
let v = document.getElementById(`v`);
let promise = navigator.mediaDevices.getUserMedia(constraints);
promise.then(stream => {
// 舊的瀏覽器可能沒有srcObject
if ("srcObject" in v) {
v.srcObject = stream;
} else {
// 防止再新的瀏覽器裡使用它,應為它已經不再支援了
v.src = window.URL.createObjectURL(stream);
}
v.onloadedmetadata = function (e) {
v.play();
videoPlaying = true;
};
}).catch(err => {
console.error(err.name + ": " + err.message);
})
document.getElementById(`take`).addEventListener(`click`, function () {
if (videoPlaying) {
let canvas = document.getElementById(`canvas`);
canvas.width = v.videoWidth;
canvas.height = v.videoHeight;
canvas.getContext(`2d`).drawImage(v, 0, 0);
let data = canvas.toDataURL(`image/webp`);
document.getElementById(`photo`).setAttribute(`src`, data);
}
}, false);
})();
</script>
</body>
</html>