line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Term::QRCode::Compact; |
2
|
1
|
|
|
1
|
|
587
|
use 5.020; |
|
1
|
|
|
|
|
4
|
|
3
|
1
|
|
|
1
|
|
7
|
use feature 'signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
102
|
|
4
|
1
|
|
|
1
|
|
6
|
no warnings 'experimental::signatures'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
5
|
1
|
|
|
1
|
|
503
|
use utf8; |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
5
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
30
|
use Exporter 'import'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
8
|
1
|
|
|
1
|
|
401
|
use Imager::QRCode; |
|
1
|
|
|
|
|
49078
|
|
|
1
|
|
|
|
|
630
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @EXPORT_OK = ('qr_code_as_text'); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=encoding utf8 |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Term::QRCode::Compact - create QR codes for display in the terminal |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Term::QRCode::Compact 'qr_code_as_text'; |
23
|
|
|
|
|
|
|
print qr_code_as_text(text => 'https://metacpan.org/module/Term::QRCode::Compact'); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Output: |
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
|
|
|
|
|
|
|
use Term::QRCode::Compact 'qr_code_as_text'; |
64
|
|
|
|
|
|
|
print qr_code_as_text( |
65
|
|
|
|
|
|
|
charset => 'ascii_1x1', |
66
|
|
|
|
|
|
|
text => 'Hello' |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# Output |
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
|
|
|
|
|
|
|
=cut |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
our %charset = ( |
117
|
|
|
|
|
|
|
ascii => { |
118
|
|
|
|
|
|
|
# ascii_1x1 => { |
119
|
|
|
|
|
|
|
# xfactor => 1, |
120
|
|
|
|
|
|
|
# yfactor => 1, |
121
|
|
|
|
|
|
|
# charset => [ ' ', '#' ], |
122
|
|
|
|
|
|
|
# }, |
123
|
|
|
|
|
|
|
'2x1' => { |
124
|
|
|
|
|
|
|
xfactor => 1, |
125
|
|
|
|
|
|
|
yfactor => 1, |
126
|
|
|
|
|
|
|
charset => [ ' ', '##' ], |
127
|
|
|
|
|
|
|
}, |
128
|
|
|
|
|
|
|
}, |
129
|
|
|
|
|
|
|
utf8 => { |
130
|
|
|
|
|
|
|
'1x2' => { |
131
|
|
|
|
|
|
|
xfactor => 1, |
132
|
|
|
|
|
|
|
yfactor => 2, |
133
|
|
|
|
|
|
|
charset => [ ' ', '▀' , |
134
|
|
|
|
|
|
|
'▄', '█' ], |
135
|
|
|
|
|
|
|
}, |
136
|
|
|
|
|
|
|
}, |
137
|
|
|
|
|
|
|
); |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
0
|
0
|
|
sub compress_lines( $lines, $xfactor, $yfactor, $charset ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $res; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
|
my $yofs = 0; |
143
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
while( $yofs < @$lines ) { |
145
|
0
|
|
|
|
|
|
my $xofs = 0; |
146
|
0
|
|
|
|
|
|
my $cols = @{$lines->[$yofs]}; |
|
0
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
while ($xofs < $cols) { |
148
|
0
|
|
|
|
|
|
my $bits = 0; |
149
|
0
|
|
|
|
|
|
for my $l (0..$yfactor-1) { |
150
|
0
|
|
|
|
|
|
for my $c (0..$xfactor-1) { |
151
|
0
|
|
|
|
|
|
my $bitpos = $l*$xfactor + $c; |
152
|
|
|
|
|
|
|
#say sprintf '%02d x %02d %04b %d %04b', $xofs+$c, $yofs+$l, $bitpos, $lines->[$yofs+$l]->[$xofs+$c], $bits; |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
$bits += $lines->[$yofs+$l]->[$xofs+$c] << $bitpos; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
0
|
|
|
|
|
|
$res .= $charset->[ $bits ]; |
158
|
0
|
|
|
|
|
|
$xofs += $xfactor |
159
|
|
|
|
|
|
|
}; |
160
|
0
|
|
|
|
|
|
$yofs += $yfactor; |
161
|
0
|
|
|
|
|
|
$res .= "\n"; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
0
|
|
|
|
|
|
return $res |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 FUNCTIONS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 C |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
say qr_code_as_text( text => 'hello' ); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Returns a string with newlines that represents |
174
|
|
|
|
|
|
|
the QR-Code. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Options |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=over 4 |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item B |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
The text to turn into a QR-Code |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item B |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
charset => 'utf8', |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
The charset to use when rendering the QR-Code, |
189
|
|
|
|
|
|
|
default is C. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item B |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Optional |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
dimensions => '1x2', |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
The number of pixels per returned character. |
198
|
|
|
|
|
|
|
Currently for ASCII the dimensions the dimensions |
199
|
|
|
|
|
|
|
are C<2x1> for ascii and C<2x1> for C. |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=item B |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Optional |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
level => 'L', |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
The level of error correction, 'H', 'M' or 'L'. |
208
|
|
|
|
|
|
|
Default is 'M'. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=back |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=cut |
213
|
|
|
|
|
|
|
|
214
|
0
|
|
|
0
|
1
|
|
sub qr_code_as_text( %options ) { |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
215
|
0
|
|
0
|
|
|
|
$options{charset} //= 'utf8'; |
216
|
0
|
|
0
|
|
|
|
$options{ level } //= 'H'; |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
my $qrcode = Imager::QRCode->new( |
219
|
|
|
|
|
|
|
size => 2, |
220
|
|
|
|
|
|
|
margin => 2, |
221
|
|
|
|
|
|
|
version => 1, |
222
|
|
|
|
|
|
|
level => $options{ level }, |
223
|
0
|
|
|
|
|
|
casesensitive => 1, |
224
|
|
|
|
|
|
|
lightcolor => Imager::Color->new(255, 255, 255), |
225
|
|
|
|
|
|
|
darkcolor => Imager::Color->new(0, 0, 0), |
226
|
|
|
|
|
|
|
); |
227
|
|
|
|
|
|
|
|
228
|
0
|
|
|
|
|
|
my $charset = $charset{ $options{ charset }}; |
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
my $dimensions = $options{ dimensions }; |
231
|
0
|
0
|
|
|
|
|
if( ! $dimensions ) { |
232
|
0
|
|
|
|
|
|
($dimensions) = keys (%$charset); |
233
|
|
|
|
|
|
|
} |
234
|
0
|
|
|
|
|
|
$charset = $charset->{ $dimensions }; |
235
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
my $img = $qrcode->plot($options{text}); |
237
|
0
|
|
|
|
|
|
my $rows = $img->getheight; |
238
|
0
|
|
|
|
|
|
my $cols = $img->getwidth; |
239
|
0
|
|
|
|
|
|
my $res; |
240
|
|
|
|
|
|
|
my @lines; |
241
|
0
|
|
|
|
|
|
for my $row (0..$rows-1) { |
242
|
0
|
|
|
|
|
|
my $line = []; |
243
|
0
|
|
|
|
|
|
for my $col (0..$cols-1) { |
244
|
0
|
|
|
|
|
|
my $val = $img->getpixel( 'x' => $col, 'y' => $row ); |
245
|
0
|
0
|
|
|
|
|
my $is_black = [$val->rgba]->[0] == 0 ? 1 : 0; |
246
|
0
|
|
|
|
|
|
push @$line, $is_black; |
247
|
|
|
|
|
|
|
} |
248
|
0
|
|
|
|
|
|
push @lines, $line; |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
return compress_lines( \@lines, |
252
|
|
|
|
|
|
|
$charset->{xfactor}, |
253
|
|
|
|
|
|
|
$charset->{yfactor}, |
254
|
|
|
|
|
|
|
$charset->{charset}, |
255
|
0
|
|
|
|
|
|
); |
256
|
|
|
|
|
|
|
} |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
1; |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head1 SEE ALSO |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
L - needs an update to support C<.> in C<@INC> |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
L - needs L |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=cut |