媒介
前些天,線上筆試的時刻,發明須要瀏覽器贊同開啟攝像頭,覺得像是 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>