前言
前些天,線上筆試的時候,發(fā)現(xiàn)需要瀏覽器同意開啟攝像頭,感覺像是 js 調(diào)用的,由于當(dāng)時筆試,也就沒想到這么多問題。今天閑來無事,看了下自己的 todo,發(fā)現(xiàn)有這個調(diào)用攝像頭的todo,才想到😂。網(wǎng)上查了一下,果然 js 有調(diào)用攝像頭的 api,為此自己寫一個 demo ,避免忘記。
正文
調(diào)用攝像頭
一共有兩種實現(xiàn)方式,一種是使用navigator.getUserMedia
(該特性已經(jīng)從 Web 標(biāo)準(zhǔn)中刪除,雖然一些瀏覽器目前仍然支持它,但也許會在未來的某個時間停止支持,請盡量不要使用該特性),前面一種已經(jīng)從 Web 標(biāo)準(zhǔn)中刪除,僅為了向后兼容而存在,第二種是使用navigator.mediaDevices.getUserMedia
(推薦使用),這兩種方法 Safari 貌似都不支持。。。。
第一種方法navigator.getUserMedia
用法詳見mdn ,代碼如下:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>攝像頭調(diào)用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>攝像頭調(diào)用2</title>
</head>
<body>
<video id="v"></video>
<script>
!(function () {
// 老的瀏覽器可能根本沒有實現(xiàn) mediaDevices,所以我們可以先設(shè)置一個空的對象
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// 首先,如果有g(shù)etUserMedia的話,就獲得它
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// 一些瀏覽器根本沒實現(xiàn)它 - 那么就返回一個error到promise的reject來保持一個統(tǒng)一的接口
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 {
// 防止再新的瀏覽器里使用它,應(yīng)為它已經(jīng)不再支持了
v.src = window.URL.createObjectURL(stream);
}
v.onloadedmetadata = function (e) {
v.play();
};
}).catch(err => {
console.error(err.name + ": " + err.message);
})
})();
</script>
</body>
</html>
拍照
思路是設(shè)置一個標(biāo)志變量 videoPlaying 看看是否 video 有在 play,監(jiān)聽拍照按鈕的點擊事件,如果videoPlaying 為 true ,使用一個canvas 獲取 video 的寬高(默認(rèn) canvas 是不顯示的),然后使用 canvas 的drawImage,然后使用 canvas 的 toDataURL返回一個 data url,將這個 url,設(shè)置在一個 img 標(biāo)簽上即可😀
第一種方法navigator.getUserMedia
實現(xiàn)代碼:
<!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
實現(xiàn)方法:
<!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 () {
// 老的瀏覽器可能根本沒有實現(xiàn) mediaDevices,所以我們可以先設(shè)置一個空的對象
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// 首先,如果有g(shù)etUserMedia的話,就獲得它
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
// 一些瀏覽器根本沒實現(xiàn)它 - 那么就返回一個error到promise的reject來保持一個統(tǒng)一的接口
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 {
// 防止再新的瀏覽器里使用它,應(yīng)為它已經(jīng)不再支持了
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。