-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera.html
107 lines (90 loc) · 3.17 KB
/
camera.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<title>My Web Cam</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
width: 1000px;
margin: 10px;
overflow-x: hidden;
padding: 10px;
}
.btn {
min-width: 150px;
margin: 5px 2px;
background-color: #3340f3;
color: #fff;
border-radius: 2px;
}
</style>
</head>
<body>
<div class="row" style="border: 1px #000 solid;">
<div class="col-lg-5">
<div class="container">
<video autoplay="true" id="camera" width="320" height="240"></video>
</div>
</div>
<div class="col-lg-2">
<input class="btn" type="button" id="btnStart" onclick="StartCamera();" value="Start Camera">
<input class="btn" type="button" onclick="snapshot();" value="Click Image">
<input class="btn" type="button" onclick="download();" value="Download Image">
</div>
<div class="col-lg-5">
<div class="container">
<canvas id="myCanvas" width="320" height="240"></canvas>
</div>
</div>
</div>
<script>
var video = document.getElementById("camera");
var canvas = document.getElementById("myCanvas");
var camera = false;
let currentStream = null;
var capturedImage = false;
var btnStart = document.getElementById("btnStart");
var ctx = canvas.getContext('2d');
function TurnOnCamera() {
// Turn on the camera upon loading the page
if (!camera) {
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({
video: true
}).then(function (stream) {
currentStream = stream;
video.srcObject = currentStream;
video.style.display = 'block';
camera = true;
;
}).catch(function (err) {
console.log(err);
});
}
}
}
function snapshot() {
if (camera) {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
capturedImage = true;
//store the things to the db now
//reroute to the next page
} else {
window.alert('Please start the camera');
}
}
function download() {
if (capturedImage) {
var link = document.createElement('a');
link.download = 'Capture' + (Math.floor(Date.now() / 1000)) + '.jpg';
link.href = document.getElementById('myCanvas').toDataURL('image/jpeg');
link.click();
} else {
window.alert('Please capture an image to download it');
}
}
</script>
</body>
</html>