-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-to-qr.html
61 lines (61 loc) · 1.71 KB
/
binary-to-qr.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
<html>
<head>
<style>
body {
background-color: #FFF;
}
table {
border-spacing: 0px;
}
td {
line-height: 4px;
width: 4px;
}
.zero {
background-color: #FFF;
}
.one {
background-color: #000;
}
</style>
</head>
<body>
<pre>
Supply the number of columns and the binary string as url parameters.
Example: binary-to-qr.html?cols=5&b=01010000001000101110
</pre>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
var getUrlParameter = function(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
var printQR = function(b, cols) {
var size = 10;
var rows = Math.ceil(b.length / cols);
var canvas = $('<canvas width="' + (cols * size) + 'px" height="' + (rows * size) + 'px">');
var ctx = canvas[0].getContext('2d');
for (var row = 0; row < rows; ++row) {
for (var col = 0; col < cols; ++col) {
ctx.fillStyle = b.charAt(row * cols + col) === '1' ? '#000' : '#FFF';
ctx.fillRect(col * size, row * size, size, size);
}
}
var center = $('<center>');
center.append(canvas);
$(document.body).append(center);
};
var cols = getUrlParameter('cols');
var b = getUrlParameter('b');
printQR(b, cols);
</script>
</body>
</html>