forked from istoan/HTML5-Webcam-Photobooth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
side-by-side.html
268 lines (228 loc) · 7.47 KB
/
side-by-side.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 and JavaScript Webcam PhotoBooth</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
body {
text-align: center;
font-family: 'Roboto', sans-serif;
width: 100%;
height: 100%;
overflow-x: hidden;
}
.content{
display: block;
width: 100%;
height: 100%;
}
.holder {
display: inline-block;
line-height: 0;
margin: 0 auto;
border: 10px #fff solid;
border-radius: 15px;
box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1);
}
#video {
float: left;
background-color: #666;
margin-right: 10px;
}
.buttons{
margin-top: 20px;
}
button{
width: 140px;
height: 45px;
font-size: 11px;
text-transform: uppercase;
letter-spacing: 2.5px;
font-weight: 500;
color: #000;
background-color: #fff;
border: none;
border-radius: 45px;
box-shadow: 0px 8px 15px rgba(0, 0, 0, 0.1);
transition: all 0.3s ease 0s;
cursor: pointer;
outline: none;
margin: 0 10px;
}
button:not([disabled]):hover{
background-color: #49BEFF;
box-shadow: 0px 15px 20px rgba(73,190,255,0.5);
color: #fff;
transform: translateY(-5px);
}
button:disabled{
color: #999;
}
#savedImages{
display: inline-block;
margin-top: 30px;
text-align: left;
max-width: 1020px;
background-color: #EDF9FF;
border-radius: 5px;
}
#savedImages h1, #savedImages h2, #savedImages h3{
color: red;
padding: 0 20px;
}
#savedImages img {
margin: 5px;
}
</style>
</head>
<body>
<div class="content">
<div class="holder">
<video autoplay id="video"></video>
<div id="fallback" style="display: none">
<div id="alternativeContent"> You need Flash Player to play this game.
<p><a href="https://www.adobe.com/go/getflashplayer"><img src="https://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
</div>
</div>
<canvas id="canvas"></canvas>
</div>
<div class="buttons">
<button id="buttonCapture" disabled>Capture</button>
<button id="buttonSave" disabled>Save</button>
</div>
<div id="savedImages"></div>
</div>
<script>
var context;
var width = 400; //set width of the video and image
var height = 300; //only used for the Flash fallback
//this function is used to receive the image from the Flash Player, if Flash is used.
function imageResult(data, videoWidth, videoHeight){
var imageData = "data:image/png;base64,"+data;
var image = new Image;
image.onload = function(){
context.drawImage(this,0,0);
};
image.src = imageData;
}
//wait for the document to load and then initiate the application
document.addEventListener("DOMContentLoaded", init);
function init(){
var isFlash = false;
var isChrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
var isSafari = /Safari/.test(navigator.userAgent) && /Apple Computer, Inc/.test(navigator.vendor);
var video = document.querySelector("#video");
video.width = width;
var canvas = document.getElementById("canvas");
canvas.style.width = width + "px";
canvas.width = width;
context = canvas.getContext("2d");
if((isChrome || isSafari) && window.location.protocol == "http:"){
document.getElementById("savedImages").innerHTML = "<h1>This browser only supports camera streams over https:</h1>";
}else{
startWebcam();
}
function startWebcam(){
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mediaDevices || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia;
if (navigator.mediaDevices){
navigator.mediaDevices.getUserMedia({video: true}, handleVideo, videoError).then(function(stream){
video.onloadedmetadata = setHeight;
document.getElementById("buttonCapture").disabled = false;
return video.srcObject = stream;
}).catch(function(e){
console.log(e.name + ": "+ e.message);
document.getElementById("buttonCapture").disabled = true;
switch(e.name){
case "NotAllowedError":
document.getElementById("savedImages").innerHTML = "<h3>You can't use this app because you denied camera access. Refresh the page and allow the camera to be used by this app.</h3>";
break;
case "NotReadableError":
document.getElementById("savedImages").innerHTML = "<h3>Camera not available. Your camera may be used by another application.</h3>";
break;
case "NotFoundError":
document.getElementById("savedImages").innerHTML = "<h3>Camera not available. Please connect a camera to your computer.</h3>";
break;
}
});
}else{
canvas.style.height = height + "px";
canvas.height = height;
document.getElementById("buttonCapture").disabled = false;
isFlash = true;
video.style.display = "none";
document.getElementById("fallback").style.display = "block";
var script = document.createElement("script");
document.getElementsByTagName("head")[0].appendChild(script);
script.type = "text/javascript";
script.onload = function(){
var flashvars = {};
var parameters = {};
parameters.scale = "noscale";
parameters.wmode = "transparent";
parameters.allowFullScreen = "true";
parameters.allowScriptAccess = "always";
parameters.bgColor = "#999999";
var attributes = {};
attributes.name = "FlashWebcam";
swfobject.embedSWF("fallback/webcam_fallback.swf", "fallback", width, height, "27", "expressInstall.swf", flashvars, parameters, attributes);
}
script.src = "fallback/swfobject.js";
}
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName];
} else {
return document[movieName];
}
}
function handleVideo(stream){
video.src = window.URL.createObjectURL(stream);
}
function videoError(e){
document.getElementById("savedImages").innerHTML = "<h3>" + e +"</h3>";
}
function setHeight(){
var ratio = video.videoWidth / video.videoHeight;
height = width/ratio;
canvas.style.height = height + "px";
canvas.height = height;
}
//add event listener and handle the capture button
document.getElementById("buttonCapture").addEventListener("mousedown", handleButtonCaptureClick);
function handleButtonCaptureClick(){
if(isFlash){
thisMovie("FlashWebcam").capture();
}else{
setHeight();
context.drawImage(video, 0, 0, width, height);
}
document.getElementById("buttonSave").innerHTML = "Save";
document.getElementById("buttonSave").disabled = false;
}
//add event listener and handle the save button
document.getElementById("buttonSave").addEventListener("mousedown", handleButtonSaveClick);
function handleButtonSaveClick(){
var dataURL = canvas.toDataURL("image/jpg");
var xhr = new XMLHttpRequest();
xhr.open("POST", "save.php"); //change this to .php or .asp, depending on your server
xhr.onload = function(){
if (xhr.readyState == 4 ){
if(xhr.status == 200){
var image = new Image();
image.src = "images/" + xhr.responseText;
document.getElementById("savedImages").insertAdjacentElement('afterbegin', image);
document.getElementById("buttonSave").innerHTML = "Saved";
document.getElementById("buttonSave").disabled = true;
}
}
};
var form = new FormData();
form.append("image", dataURL);
xhr.send(form);
}
}
}
</script>
</body>
</html>