Skip to content

Commit 87fe4aa

Browse files
committedFeb 28, 2022
smaller square QR code
1 parent 646560f commit 87fe4aa

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed
 

‎bin/servez

+28-4
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,41 @@ const log = {
2020
};
2121

2222
function genQRCode(s) {
23+
const blockChars = [
24+
' ', // 0
25+
'▘', // 1
26+
'▝', // 2
27+
'▀', // 3
28+
'▖', // 4
29+
'▌', // 5
30+
'▞', // 6
31+
'▛', // 7
32+
'▗', // 8
33+
'▚', // 9
34+
'▐', // 10
35+
'▜', // 11
36+
'▄', // 12
37+
'▙', // 13
38+
'▟', // 14
39+
'█', // 15
40+
];
41+
2342
const qr = QrCode.encodeText(s, Ecc.MEDIUM);
43+
const size = ((qr.size + 1) / 2 | 0) * 2 + 2;
2444

2545
const lines = [];
26-
for (let y = 0; y < qr.size; y++) {
46+
for (let y = -2; y < size; y += 2) {
2747
const line = [];
28-
for (let x = 0; x < qr.size; x++) {
29-
line.push(c[qr.getModule(x, y) ? 'bgBlack' : 'bgWhite'](' '));
48+
for (let x = -2; x < size; x += 1) {
49+
const code = (qr.getModule(x + 0, y + 0) ? 1 : 0) |
50+
(qr.getModule(x + 0, y + 0) ? 2 : 0) |
51+
(qr.getModule(x + 0, y + 1) ? 4 : 0) |
52+
(qr.getModule(x + 0, y + 1) ? 8 : 0) ;
53+
line.push(blockChars[code]);
3054
}
3155
lines.push(line.join(''));
3256
}
33-
return lines.join('\n');
57+
return c.bgWhite(c.black(lines.join('\n')));
3458
}
3559

3660
const optionSpec = {

0 commit comments

Comments
 (0)
Please sign in to comment.