WebCamera

A way to use camera in website

Posted by MichaelChen on 2020-11-29
Estimated Reading Time 1 Minutes
Words 108 In Total
Viewed Times

A Way To Use Camera In Website

Need Chrome or FireFox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<video id="webcam"></video>

<script type="text/javascript">
function onSuccess(stream) {
var video = document.getElementById('webcam');
if (window.URL) {

try{
video.src = window.URL.createObjectURL(stream);
}catch (e){
console.log(e);
video.srcObject = stream;
}

} else {
video.src = stream;
}

video.autoplay = true;
// 或者 video.play();
}

if (navigator.getUserMedia) {
navigator.getUserMedia({video:true}, onSuccess,onError);
} else {
document.getElementById('webcam').src = 'somevideo.mp4';
}

function onError(error) {
console.log("navigator.getUserMedia error: ", error);
}
</script>

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !