| 1 | package Term::QRCode::Compact; | ||||||||||||
| 2 | 1 | ▶ | use 5.020; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 1 | ▶ | use feature 'signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Exporter 'import'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use Imager::QRCode; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | our @EXPORT_OK = ('qr_code_as_text'); | |||||||||||
| 11 | |||||||||||||
| 12 | 1 | 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 | 1 | 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 | ▶ | sub compress_lines( $lines, $xfactor, $yfactor, $charset ) { | ||||||||||
|
Subroutine
compress_lines: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 140 | 0 | my $res; | |||||||||||
| 141 | |||||||||||||
| 142 | 0 | my $yofs = 0; | |||||||||||
| 143 | |||||||||||||
| 144 | 0 | ▶ | while( $yofs < @$lines ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | 0 | my $xofs = 0; | |||||||||||
| 146 | 0 | my $cols = @{$lines->[$yofs]}; | |||||||||||
| 147 | 0 | ▶ | while ($xofs < $cols) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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<qr_code_as_text> | ||||||||||||
| 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<text> | ||||||||||||
| 181 | |||||||||||||
| 182 | The text to turn into a QR-Code | ||||||||||||
| 183 | |||||||||||||
| 184 | =item B<charset> | ||||||||||||
| 185 | |||||||||||||
| 186 | charset => 'utf8', | ||||||||||||
| 187 | |||||||||||||
| 188 | The charset to use when rendering the QR-Code, | ||||||||||||
| 189 | default is C<utf8>. | ||||||||||||
| 190 | |||||||||||||
| 191 | =item B<dimensions> | ||||||||||||
| 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<utf8>. | ||||||||||||
| 200 | |||||||||||||
| 201 | =item B<level> | ||||||||||||
| 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 | ▶ | sub qr_code_as_text( %options ) { | ||||||||||
|
Subroutine
qr_code_as_text: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||
| 215 | 0 | ▶ | $options{charset} //= 'utf8'; | ||||||||||
|
Condition0%0/2
$options{"charset"} //= "utf8"
MC/DC0%0/1
$options{"charset"} //= "utf8"
$options{"charset"}
Truth table0%0/2
A
A= $options{"charset"}
| |||||||||||||
| 216 | 0 | ▶ | $options{ level } //= 'H'; | ||||||||||
|
Condition0%0/2
$options{"level"} //= "H"
MC/DC0%0/1
$options{"level"} //= "H"
$options{"level"}
Truth table0%0/2
A
A= $options{"level"}
| |||||||||||||
| 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 | ▶ | if( ! $dimensions ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | ▶ | my $is_black = [$val->rgba]->[0] == 0 ? 1 : 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 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 | 1; | |||||||||||
| 259 | |||||||||||||
| 260 | =head1 SEE ALSO | ||||||||||||
| 261 | |||||||||||||
| 262 | L<Text::QRCode> - needs an update to support C<.> in C<@INC> | ||||||||||||
| 263 | |||||||||||||
| 264 | L<Term::QRCode> - needs L<Text::QRCode> | ||||||||||||
| 265 | |||||||||||||
| 266 | =cut | ||||||||||||