-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbootstrap-capture.html
129 lines (112 loc) · 4.11 KB
/
bootstrap-capture.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
<head>
<meta charset="utf-8">
</head>
<video autoplay></video>
<canvas style=""></canvas>
<input>
<p id="processed" style="color: black; font-size: 3em;">.</p>
<script type="text/javascript">
var video = document.querySelector('video');
var canvas = document.querySelector('canvas');
var output = document.querySelector('input');
var proc = document.getElementById('processed');
var ctx = canvas.getContext('2d');
var localMediaStream = null;
function processResults(arr) {
var noPadding = arr.filter(function (a) {
return a !== 2;
});
console.log(noPadding)
if ((noPadding.length % 8) != 0) {
var res = fromBinaryString(noPadding);
proc.innerText=output.value="Sorry, couldn't decode the message.";
} else {
var res = fromBinaryString(noPadding.join(''));
output.value = res;
proc.innerText=res;
}
}
function fromBinaryString(str) {
var res = '', j = 0;
for (var i = 0; i < str.length; i = j) {
j += 8;
var add = String.fromCharCode(parseInt(str.slice(i, j), 2));
res += add;
}
return res;
}
function snapshot() {
if (MediaStream) {
ctx.drawImage(video, 0, 0);
var data = ctx.getImageData(100, 100, 1, 1).data;
if ((data[0] > data[1]) && (data[0] > data[2])/* && (data[0] > 240)*/)
return 0;
if ((data[1] > data[0]) && (data[1] > data[2]) /*&& data[1] > 180*/)
return 1;
if ((data[2] > data[0]) && (data[2] > data[1]) /*&& data[2] > 180*/)
return 2;
else
return 2;
} else {
console.log("couldn't find MediaStream");
}
}
var resultArray = [];
var push = function (data) {
if (resultArray.slice(-1)[0] != data) {
resultArray.push(data);
}
};
var receiveToken = undefined;
document.addEventListener('click', function () {
if (receiveToken != undefined) {
console.log("off");
clearInterval(receiveToken);
receiveToken = undefined;
processResults(resultArray);
} else {
console.log("on");
receiveToken = setInterval(function () {
push(snapshot());
}, 100);
}
});
if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {};
}
// Some browsers partially implement mediaDevices. We can't just assign an object
// with getUserMedia as it would overwrite existing properties.
// Here, we will just add the getUserMedia property if it's missing.
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
// First get ahold of the legacy getUserMedia, if present
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// Some browsers just don't implement it - return a rejected promise with an error
// to keep a consistent interface
if (!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
}
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}
navigator.mediaDevices.getUserMedia({
video: true
}).then(function (stream) {
// Older browsers may not have srcObject
if ("srcObject" in video) {
video.srcObject = stream;
} else {
// Avoid using this in new browsers, as it is going away.
video.src = window.URL.createObjectURL(stream);
}
MediaStream = stream;
video.onloadedmetadata = function (e) {
video.play();
};
}).catch(function (err) {
console.log(err.name + ": " + err.message);
});
</script>