-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencamera.html
69 lines (68 loc) · 2.32 KB
/
opencamera.html
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html>
<head>
<title>Access and capture your camera from browser</title>
</head>
<body align="center">
<video id="video" playsinline width="640" height="480" autoplay></video>
<br/><br/>
<button id="snap">Snap Photo</button>
<br/><br/>
<canvas id="canvas" width="640" height="480" style="border: 1px solid black;"></canvas>
<br/><br/>
<textarea id="base64image" style="width:640px;height:100px;"></textarea>
</body>
<script>
// Grab elements, create settings, etc.
var video = document.getElementById('video');
// Elements for taking the snapshot
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var video = document.getElementById('video');
// Get access to the camera!
if(navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
// Not adding `{ audio: true }` since we only want video now
navigator.mediaDevices.getUserMedia(
/* if you want to use back camera
{
video: {
facingMode: {
exact: 'environment'
}
}
}
*/
/* if you want to use front camera */
{ video: true }
).then(function(stream) {
//video.src = window.URL.createObjectURL(stream);
video.srcObject = stream;
video.play();
});
}
// Legacy code below: getUserMedia
else if(navigator.getUserMedia) { // Standard
navigator.getUserMedia({ video: true }, function(stream) {
video.src = stream;
video.play();
}, errBack);
} else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
navigator.webkitGetUserMedia({ video: true }, function(stream){
video.src = window.webkitURL.createObjectURL(stream);
video.play();
}, errBack);
} else if(navigator.mozGetUserMedia) { // Mozilla-prefixed
navigator.mozGetUserMedia({ video: true }, function(stream){
video.srcObject = stream;
video.play();
}, errBack);
}
// Trigger photo take
document.getElementById("snap").addEventListener("click", function() {
context.drawImage(video, 0, 0, 640, 480);
var canvasbase64=document.getElementById('canvas').toDataURL("image/jpeg");
document.getElementById('base64image').value=canvasbase64;
/* ant then you just try <img src="the value of #base64image above" /> to preview your captured*/
});
</script>
</html>