File Coverage

blib/lib/QRCode/Any.pm
Criterion Covered Total %
statement 14 28 50.0
branch 0 8 0.0
condition 0 4 0.0
subroutine 5 6 83.3
pod 1 1 100.0
total 20 47 42.5


line stmt bran cond sub pod time code
1             package QRCode::Any;
2              
3 1     1   346146 use 5.010001;
  1         5  
4 1     1   6 use strict;
  1         3  
  1         33  
5 1     1   10 use warnings;
  1         3  
  1         75  
6 1     1   2342 use Log::ger;
  1         66  
  1         6  
7              
8 1     1   970 use Exporter::Rinci qw(import);
  1         676  
  1         7  
9              
10             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11             our $DATE = '2026-01-20'; # DATE
12             our $DIST = 'QRCode-Any'; # DIST
13             our $VERSION = '0.003'; # VERSION
14              
15             my $known_formats = [qw/png/]; # TODO: html, txt
16             my $sch_format = ['str', in=>$known_formats, default=>'png'];
17             our %argspecopt_format = (
18             format => {
19             summary => 'Format of QRCode to generate',
20             schema => $sch_format,
21             description => <<'MARKDOWN',
22              
23             The default, when left undef, is `png`.
24              
25             MARKDOWN
26             cmdline_aliases => {f=>{}},
27             },
28             );
29             our %argspecopt_format_args = (
30             format_args => {
31             schema => 'hash*',
32             description => <<'MARKDOWN',
33              
34             Format-specific arguments.
35              
36             MARKDOWN
37             },
38             );
39             our %argspecs_format = (
40             %argspecopt_format,
41             %argspecopt_format_args,
42             );
43              
44             our %SPEC;
45              
46             $SPEC{':package'} = {
47             v => 1.1,
48             summary => 'Common interface to QRCode functions',
49             description => <<'MARKDOWN',
50              
51              
52             MARKDOWN
53             };
54              
55             $SPEC{'encode_qrcode'} = {
56             v => 1.1,
57             summary => 'Encode a text into QR Code (in one of several supported formats)',
58             description => <<'MARKDOWN',
59              
60             MARKDOWN
61             args => {
62             %argspecs_format,
63             text => {
64             schema => 'str*',
65             req => 1,
66             },
67             filename => {
68             schema => 'filename*',
69             req => 1,
70             },
71             level => {
72             summary => 'Error correction level',
73             schema => ['str*', in=>[qw/L M Q H/]],
74             default => 'M',
75             },
76             },
77             };
78             sub encode_qrcode {
79 0     0 1   my %args = @_;
80 0   0       my $format = $args{format} // 'png';
81 0   0       my $level = $args{level} // 'M';
82              
83 0 0         if ($format eq 'png') {
84 0           require Imager;
85 0           require Imager::QRCode;
86 0           my $qrcode = Imager::QRCode->new(
87             size => 5,
88             margin => 2,
89             version => 1,
90             level => $level,
91             casesensitive => 1,
92             lightcolor => Imager::Color->new(255, 255, 255),
93             darkcolor => Imager::Color->new(0, 0, 0),
94             );
95              
96             # generates rub-through image
97 0           my $img = $qrcode->plot($args{text});
98              
99 0 0         my $conv_img = $img->to_rgb8
100             or die "converting with to_rgb8() failed: " . Imager->errstr;
101              
102 0           my $filename = $args{filename};
103 0 0         $filename .= ".png" unless $filename =~ /\.png\z/;
104 0 0         $conv_img->write(file => $filename)
105             or return [500, "Failed to write to file `$filename`: " . $conv_img->errstr];
106 0           [200, "OK", undef, {"func.filename"=>$filename}];
107             } else {
108 0           [501, "Unsupported format '$format'"];
109             }
110             }
111              
112             1;
113             # ABSTRACT: Common interface to QRCode functions
114              
115             __END__