File: qrcode.pm

package info (click to toggle)
libpdf-api2-perl 2.048-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,228 kB
  • sloc: perl: 42,239; makefile: 11
file content (170 lines) | stat: -rw-r--r-- 3,782 bytes parent folder | download
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
62
63
64
65
66
67
68
69
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package PDF::API2::Resource::XObject::Form::BarCode::qrcode;

use base 'PDF::API2::Resource::XObject::Form::BarCode';

use strict;
use warnings;

our $VERSION = '2.048'; # VERSION

use Carp;

our @CARP_NOT = qw(PDF::API2);

sub new {
    my ($class, $pdf, %options) = @_;
    my $self = $class->SUPER::new($pdf, %options);

    eval {
        require Text::QRCode;
    };
    if ($@) {
        croak "Error loading Text::QRCode: $@";
    }

    my $mode = $options{'mode'} // '8-bit';
    $mode = 'numerical'       if $mode eq 'numeric';
    $mode = 'alpha-numerical' if $mode eq 'alphanumeric';
    $mode = 'alpha-numerical' if $mode eq 'alpha-numeric';

    my $generator = Text::QRCode->new(
        level         => ($options{'error_correction'} // 'L'),
        version       => ($options{'version'}          // 0),
        mode          => $mode,
        casesensitive => ($options{'case_sensitive'}   // 1),
    );

    my $lines_ref = $generator->plot($options{'code'});
    my @lines = reverse map { join('', @$_) } @$lines_ref;

    $self->render(@lines);
    return $self;
}

sub render {
    my ($self, @lines) = @_;

    $self->fillcolor($self->{' color'});

    my $y = $self->{' quzn'};
    my $w = $self->{' mils'} / 1000 * 72;
    my $h = $self->{' zone'};
    foreach my $line (@lines) {
        my $x = $self->{' quzn'};
        foreach my $char (split m//, $line) {
            if ($char eq '*') {
                $self->rectangle($x, $y, $x + $w, $y + $h);
                $self->fill();
            }
            $x += $w;
        }
        $y += $h;
    }

    $self->{' w'} = 2 * $self->{' quzn'} + length($lines[0]) * $w;
    $self->{' h'} = 2 * $self->{' quzn'} + length($lines[0]) * $h;
    $self->bbox(0, 0, $self->{' w'}, $self->{' h'});

    return $self;
}

1;

__END__

=head1 NAME

PDF::API2::Resource::XObject::Form::BarCode::qrcode - Generate QR codes

=head1 SYNOPSIS

    use PDF::API2;

    my $pdf = PDF::API2->new();
    my $page = $pdf->page();

    # Create a QR Code object
    my $barcode = $pdf->barcode('qr', 'This is a test');

    # Calculate the scale needed for a desired size (72pt / inch)
    my $desired_inches = 2;
    my $scale = $desired_inches * 72 / $barcode->width();

    # Place the QR Code one inch (72pt) from the bottom left corner
    $page->object($barcode, 72, 72, $scale);

    $pdf->save('qr_test.pdf');

=head1 REQUIREMENTS

L<Text::QRCode> is used to encode QR codes.

=head1 OPTIONS

    my $barcode = $pdf->barcode('qr', $value, %options);

The following standard barcode options are supported, as described in
L<PDF::API2/barcode>:

=over

=item * bar_width (default: 1; unit is points)

=item * bar_height (default: bar width)

=item * quiet_zone (default: 4x bar width)

=item * color (default: black)

=back

The following options are specific to QR codes:

=over

=item * error_correction (default: L)

The QR Code Error Correction Level.  Higher levels of error correction generate
larger bar codes but remain scannable when parts of the code are damaged or
obscured.

In increasing order of error correction, the possible values are:

=over

=item * L (7%)

=item * M (15%)

=item * Q (25%)

=item * H (30%)

=back

=item * version (default: 0)

If you need a specific version of QR Code, enter it here.  Otherwise, the
minimum version will be used that can support the encoded value.

=item * mode (default: 8-bit)

You can generate smaller QR codes if you're using a restricted character set:

=over

=item * 8-bit (typically the ISO-8859-1 character set)

=item * kanji

=item * alphanumeric (digits, uppercase letters, space, or C<$ % * + - . / :>)

=item * numeric (digits)

=back

=item * case_sensitive (default: 1)

Whether 8-bit characters should be treated in a case-sensitive manner.

=back