-
Notifications
You must be signed in to change notification settings - Fork 22
/
photobooth.html
430 lines (375 loc) · 11.8 KB
/
photobooth.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Photobooth: Captures still photo. Displays on EleksTube IPS hack</title>
</head>
<body>
<div>
<h1>Photobooth</h1>
<p>Uses the browser <a href="https://developer.mozilla.org/docs/Web/API/WebRTC_API/Taking_still_photos" target="_blank">WebRTC API</a>
to capture a still image from your laptop, tablet, or mobile phone camera.
Slices the image into 6 JPEG images. Uploads and displays the images
to the EleksTube IPS hack.</p>
<div style="position:relative; height: 240px;">
<div class="camera">
<video id="video">Video stream not available.</video>
<button id="startbutton">Take photo</button>
</div>
<div class="output0">
<img id="photo0" alt="The screen capture appears in this box. 0">
</div>
<div class="output1">
<img id="photo1" alt="The screen capture appears in this box. 1">
</div>
<div class="output2">
<img id="photo2" alt="The screen capture appears in this box. 2">
</div>
<div class="output3">
<img id="photo3" alt="The screen capture appears in this box. 3">
</div>
<div class="output4">
<img id="photo4" alt="The screen capture appears in this box. 4">
</div>
<div class="output5">
<img id="photo5" alt="The screen capture appears in this box. 5">
</div>
</div>
<!-- Hidden -->
<canvas id="canvas"></canvas>
<canvas id="canvas0"></canvas>
<canvas id="canvas1"></canvas>
<canvas id="canvas2"></canvas>
<canvas id="canvas3"></canvas>
<canvas id="canvas4"></canvas>
<canvas id="canvas5"></canvas>
</div>
<br><br>
<div style="position:relative; height: 240px;">
<button id="uploadbutton">Upload images</button>
</div>
<style>
.camera {
width: 340px;
position: absolute;
}
.output0 {
left: 360px;
width: 135px;
position: absolute;
}
.output1 {
left: 515px;
width: 135px;
position: absolute;
}
.output2 {
left: 670px;
width: 135px;
position: absolute;
}
.output3 {
left: 825px;
width: 135px;
position: absolute;
}
.output4 {
left: 980px;
width: 135px;
position: absolute;
}
.output5 {
left: 1135px;
width: 135px;
position: absolute;
}
#video {
border: 1px solid black;
box-shadow: 2px 2px 3px black;
width:320px;
height:240px;
}
.sizehelper {
position:absolute;
background:rgba(0,0,128,0.50);
top: 0px;
width: 320px;
height: 60px;
text-align:center;
padding:0px;
}
#photo0, #photo1, #photo2, #photo3, #photo4, #photo5 {
border: 1px solid black;
box-shadow: 2px 2px 3px black;
width:135px;
height:240px;
}
#startbutton {
display:block;
position:absolute;
top: 0px;
width: 320px;
height: 60px;
background:rgba(0,0,128,0.50);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: 0px 0px 1px 2px rgba(0, 0, 0, 0.2);
font-size: 14px;
font-family: "Lucida Grande", "Arial", sans-serif;
color: rgba(255, 255, 255, .50);
}
#canvas {
display:none;
}
#canvas0 {
display:none;
}
#canvas1 {
display:none;
}
#canvas2 {
display:none;
}
#canvas3 {
display:none;
}
#canvas4 {
display:none;
}
#canvas5 {
display:none;
}
</style>
<script>
(function() {
// The width and height of the captured photo. We will set the
// width to the value defined here, but the height will be
// calculated based on the aspect ratio of the input stream.
var width = 135; // We will scale the photo width to this
var height = 240; // This will be computed based on the input stream
// |streaming| indicates whether or not we're currently streaming
// video from the camera. Obviously, we start at false.
var streaming = false;
// Semaphor for uploading the 6 images
var doneflag = false;
// The various HTML elements we need to configure or control. These
// will be set by the startup() function.
var video = null;
var canvas = null;
var canvas0 = null;
var canvas1 = null;
var canvas2 = null;
var canvas3 = null;
var canvas4 = null;
var canvas5 = null;
var photo0 = null;
var photo1 = null;
var photo2 = null;
var photo3 = null;
var photo4 = null;
var photo5 = null;
var startbutton = null;
var uploadbutton = null;
// Handy utility to view the contents of the JPEG data in the console
function hexdump(buffer, blockSize) {
blockSize = blockSize || 16;
var lines = [];
var hex = "0123456789ABCDEF";
for (var b = 0; b < buffer.length; b += blockSize) {
var block = buffer.slice(b, Math.min(b + blockSize, buffer.length));
var addr = ("0000" + b.toString(16)).slice(-4);
var codes = block.split('').map(function (ch) {
var code = ch.charCodeAt(0);
return " " + hex[(0xF0 & code) >> 4] + hex[0x0F & code];
}).join("");
codes += " ".repeat(blockSize - block.length);
var chars = block.replace(/[\x00-\x1F\x20]/g, '.');
chars += " ".repeat(blockSize - block.length);
lines.push(addr + " " + codes + " " + chars);
}
return lines.join("\n");
}
function startup() {
video = document.getElementById('video');
canvas = document.getElementById('canvas');
canvas0 = document.getElementById('canvas0');
canvas1 = document.getElementById('canvas1');
canvas2 = document.getElementById('canvas2');
canvas3 = document.getElementById('canvas3');
canvas4 = document.getElementById('canvas4');
canvas5 = document.getElementById('canvas5');
photo0 = document.getElementById('photo0');
photo1 = document.getElementById('photo1');
photo2 = document.getElementById('photo2');
photo3 = document.getElementById('photo3');
photo4 = document.getElementById('photo4');
photo5 = document.getElementById('photo5');
startbutton = document.getElementById('startbutton');
uploadbutton = document.getElementById('uploadbutton');
debugbutton = document.getElementById('debugbutton');
navigator.mediaDevices.getUserMedia({video: true})
.then(function(stream) {
video.srcObject = stream;
video.play();
})
.catch(function(err) {
console.log("An error occured! " + err);
});
video.addEventListener('canplay', function(ev){
if (!streaming) {
height = video.videoHeight / (video.videoWidth/width);
// Firefox currently has a bug where the height can't be read from
// the video, so we will make assumptions if this happens.
if (isNaN(height)) {
height = width / (4/3);
}
video.setAttribute('width', width);
video.setAttribute('height', height);
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
canvas0.setAttribute('width', width);
canvas0.setAttribute('height', height);
canvas1.setAttribute('width', width);
canvas1.setAttribute('height', height);
canvas2.setAttribute('width', width);
canvas2.setAttribute('height', height);
canvas3.setAttribute('width', width);
canvas3.setAttribute('height', height);
canvas4.setAttribute('width', width);
canvas4.setAttribute('height', height);
canvas5.setAttribute('width', width);
canvas5.setAttribute('height', height);
streaming = true;
}
}, false);
startbutton.addEventListener('click', function(ev){
takepicture();
ev.preventDefault();
}, false);
uploadbutton.addEventListener('click', function(ev){
uploadimages();
ev.preventDefault();
}, false);
clearphoto();
}
// Fill the photo with an indication that none has been
// captured.
function clearphoto() {
var context = canvas.getContext('2d');
context.fillStyle = "#AAA";
context.fillRect(0, 0, canvas.width, canvas.height);
var data = canvas.toDataURL("image/png");
photo0.setAttribute('src', data);
photo1.setAttribute('src', data);
photo2.setAttribute('src', data);
photo3.setAttribute('src', data);
photo4.setAttribute('src', data);
photo5.setAttribute('src', data);
}
// Capture a photo by fetching the current contents of the video
// and drawing it into a canvas, then converting that to a PNG
// format data URL. By drawing it on an offscreen canvas and then
// drawing that to the screen, we can change its size and/or apply
// other changes before drawing it.
function takepicture() {
if (width && height) {
var mywide = video.videoWidth;
mywide = mywide / 6;
canvas.width = 135;
canvas.height = 240;
canvas0.width = 135;
canvas0.height = 240;
canvas1.width = 135;
canvas1.height = 240;
canvas2.width = 135;
canvas2.height = 240;
canvas3.width = 135;
canvas3.height = 240;
canvas4.width = 135;
canvas4.height = 240;
canvas5.width = 135;
canvas5.height = 240;
canvas.getContext('2d').drawImage(video,0*mywide,0,mywide,height,0,0,135,240);
photo0.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas0.getContext('2d').drawImage(video,0*mywide,0,mywide,height,0,0,135,240);
canvas.getContext('2d').drawImage(video,1*mywide,0,mywide,height,0,0,135,240);
photo1.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas1.getContext('2d').drawImage(video,1*mywide,0,mywide,height,0,0,135,240);
canvas.getContext('2d').drawImage(video,2*mywide,0,mywide,height,0,0,135,240);
photo2.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas2.getContext('2d').drawImage(video,2*mywide,0,mywide,height,0,0,135,240);
canvas.getContext('2d').drawImage(video,3*mywide,0,mywide,height,0,0,135,240);
photo3.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas3.getContext('2d').drawImage(video,3*mywide,0,mywide,height,0,0,135,240);
canvas.getContext('2d').drawImage(video,4*mywide,0,mywide,height,0,0,135,240);
photo4.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas4.getContext('2d').drawImage(video,4*mywide,0,mywide,height,0,0,135,240);
canvas.getContext('2d').drawImage(video,5*mywide,0,mywide,height,0,0,135,240);
photo5.setAttribute('src', canvas.toDataURL('image/jpeg', 1.0) );
canvas5.getContext('2d').drawImage(video,5*mywide,0,mywide,height,0,0,135,240);
} else {
clearphoto();
}
}
function uploadimages() {
uploadslice( canvas0, 5, 0 );
}
function uploadslice( dacanvas, disnum, slicenum ) {
doneflag = false;
var base64jpg = dacanvas.toDataURL("image/jpeg", 1.0);
var dataURL = base64jpg.replace(/\s/g, '+').replace(/^data:image\/jpeg;base64,/, '');
var oAjaxReq = new XMLHttpRequest();
oAjaxReq.submittedData = dataURL;
oAjaxReq.onload = function (oEvent)
{
var httprq = new XMLHttpRequest();
httprq.onreadystatechange = function() {
if (httprq.readyState == 4 && httprq.status == 200)
console.log("0");
}
httprq.open("GET", "https://192.168.1.1/showslice?filename=/slice" + slicenum + ".jpg&display=" + disnum, true); // true for asynchronou
httprq.send(null);
console.log( "slicenum = " + slicenum );
if ( slicenum == 0 )
{
uploadslice( canvas1, 4, 1 );
}
if ( slicenum == 1 )
{
uploadslice( canvas2, 3, 2 );
}
if ( slicenum == 2 )
{
uploadslice( canvas3, 2, 3 );
}
if ( slicenum == 3 )
{
uploadslice( canvas4, 1, 4 );
}
if ( slicenum == 4 )
{
uploadslice( canvas5, 0, 5 );
}
}
oAjaxReq.open("post", "upload", true);
oAjaxReq.setRequestHeader("Decode64", "true" );
var bound = Date.now().toString(16);
var sBoundary = "----WebKitFormBoundary" + bound;
oAjaxReq.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + sBoundary );
var mydata = "--" + sBoundary + "\r\n";
mydata += "Content-Disposition: form-data; name=\"name\"; filename=\"" + "slice" + slicenum + ".jpg" + "\"\r\n";
mydata += "Content-Type: image/png";
mydata += "\r\n";
mydata += "\r\n";
mydata += dataURL;
mydata += "\r\n";
mydata += "--" + sBoundary + "--";
mydata += "\r\n";
oAjaxReq.send( mydata );
}
// Set up our event listener to run the startup process
// once loading is complete.
window.addEventListener('load', startup, false);
})();
</script>
</body>
</html>