| 1 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use File::Which; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | # ABSTRACT: Extract the Smart Health Card information from files | ||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | use PostScript::Convert; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | package Health::SHC::Extract; | ||||||||||
| 10 | |||||||||||
| 11 | 3 | our $VERSION = '0.006'; | |||||||||
| 12 | |||||||||||
| 13 | =head1 NAME | ||||||||||
| 14 | |||||||||||
| 15 | Health::SHC::Extract - Extract Smart Health Card QR codes from PDFs or png files. | ||||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | use Health::SHC::Extract; | ||||||||||
| 20 | |||||||||||
| 21 | my $shc = Health::SHC::Extract->new(); | ||||||||||
| 22 | |||||||||||
| 23 | my @qrcodes = $shc->extract_qr_from_pdf('t/sample-qr-code.pdf'); | ||||||||||
| 24 | |||||||||||
| 25 | my @qrcodes = $shc->extract_qr_from_png('t/sample-qr-code.png'); | ||||||||||
| 26 | |||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||
| 28 | |||||||||||
| 29 | This perl module can extract a Smart Health Card's data from QR codes in | ||||||||||
| 30 | PDFs or image files. | ||||||||||
| 31 | |||||||||||
| 32 | The extract_qr_from_pdf function converts a pdf to a png and then calls | ||||||||||
| 33 | extract_qr_from_png. | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | =head1 PREREQUISITES | ||||||||||
| 38 | |||||||||||
| 39 | =over | ||||||||||
| 40 | |||||||||||
| 41 | =item * L<PostScript::Convert> | ||||||||||
| 42 | |||||||||||
| 43 | =item * L<File::Temp> | ||||||||||
| 44 | |||||||||||
| 45 | =item * L<Image::Magick> | ||||||||||
| 46 | |||||||||||
| 47 | =item * L<Barcode::ZBar> | ||||||||||
| 48 | |||||||||||
| 49 | =back | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | =head1 COPYRIGHT | ||||||||||
| 54 | |||||||||||
| 55 | The following copyright notice applies to all the files provided in | ||||||||||
| 56 | this distribution, including binary files, unless explicitly noted | ||||||||||
| 57 | otherwise. | ||||||||||
| 58 | |||||||||||
| 59 | Copyright 2021 - 2024 Timothy Legge <timlegge@gmail.com> | ||||||||||
| 60 | |||||||||||
| 61 | =head1 LICENCE | ||||||||||
| 62 | |||||||||||
| 63 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 64 | it under the same terms as Perl itself. | ||||||||||
| 65 | |||||||||||
| 66 | =cut | ||||||||||
| 67 | |||||||||||
| 68 | =head2 METHODS | ||||||||||
| 69 | |||||||||||
| 70 | =head3 B<new(...)> | ||||||||||
| 71 | |||||||||||
| 72 | Constructor; see OPTIONS above. | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 77 | 2 | my $class = shift; | |||||||||
| 78 | |||||||||||
| 79 | 2 | my $self = {}; | |||||||||
| 80 | 2 | bless $self, $class; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | =head3 B<extract_qr_from_pdf($filename)> | ||||||||||
| 84 | |||||||||||
| 85 | Extracts any Smart Health Card URI data from the QR codes found | ||||||||||
| 86 | in the PDF file. It converts the PDF to a png file and extracts | ||||||||||
| 87 | the URIs from the image file by calling extract_qr_from_png. | ||||||||||
| 88 | |||||||||||
| 89 | Arguments: | ||||||||||
| 90 | $filename: string filename of a pdf file. | ||||||||||
| 91 | |||||||||||
| 92 | Returns: ARRAY shc:/ URI from QR code | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | 0 | ▶ | sub extract_qr_from_pdf { | ||||||||
|
Subroutine
extract_qr_from_pdf: not called CC=5 SCAR=30.9
Pod
documented
| |||||||||||
| 97 | 0 | my $self = shift; | |||||||||
| 98 | 0 | my $filename = shift; | |||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | if ( ! -e $filename ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 0 | return; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 3 | ▶ | use File::Temp qw/ tempfile tempdir /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 105 | 0 | my ($fh, $output_filename) = tempfile('shctempfileXXXXX', SUFFIX => '.png'); | |||||||||
| 106 | |||||||||||
| 107 | 0 | my $ret; | |||||||||
| 108 | 0 | eval { | |||||||||
| 109 | 0 | my $gs_exec = File::Which::which('gs'); | |||||||||
| 110 | 0 | ▶ | $ret = defined $gs_exec ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | ▶ | PostScript::Convert::psconvert($filename, filename => $output_filename, format => 'pnggray') if $ret; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | }; | ||||||||||
| 113 | 0 | ▶ | return () if (!$ret); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | |||||||||||
| 115 | 0 | my @qrcodes = $self->extract_qr_from_png($output_filename); | |||||||||
| 116 | |||||||||||
| 117 | 0 | unlink $output_filename; | |||||||||
| 118 | 0 | return @qrcodes; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | =head3 B<extract_qr_from_png($filename)> | ||||||||||
| 122 | |||||||||||
| 123 | Extracts any Smart Health Card URI data from the QR codes found | ||||||||||
| 124 | in a png file. | ||||||||||
| 125 | |||||||||||
| 126 | Returns an array of shc:/ URIs | ||||||||||
| 127 | |||||||||||
| 128 | Arguments: | ||||||||||
| 129 | $filename: string filename of a png file. | ||||||||||
| 130 | |||||||||||
| 131 | Returns: ARRAY shc:/ URI from QR code | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | 1 | ▶ | sub extract_qr_from_png { | ||||||||
|
Subroutine
extract_qr_from_png: called CC=4 SCAR=27.1
Pod
documented
| |||||||||||
| 135 | 1 | my $self = shift; | |||||||||
| 136 | 1 | my $filename = shift; | |||||||||
| 137 | |||||||||||
| 138 | 1 | require Image::Magick; | |||||||||
| 139 | 0 | require Barcode::ZBar; | |||||||||
| 140 | |||||||||||
| 141 | # obtain image data | ||||||||||
| 142 | 0 | my $magick = Image::Magick->new(); | |||||||||
| 143 | 0 | ▶ | $magick->Read($filename) && die; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | 0 | my $raw = $magick->ImageToBlob(magick => 'GRAY', depth => 8); | |||||||||
| 145 | |||||||||||
| 146 | # wrap image data | ||||||||||
| 147 | 0 | my $image = Barcode::ZBar::Image->new(); | |||||||||
| 148 | 0 | $image->set_format('Y800'); | |||||||||
| 149 | 0 | my ($col, $rows) = $magick->Get(qw(columns rows)); | |||||||||
| 150 | 0 | $image->set_size($col, $rows); | |||||||||
| 151 | 0 | $image->set_data($raw); | |||||||||
| 152 | |||||||||||
| 153 | # create a reader | ||||||||||
| 154 | 0 | my $scanner = Barcode::ZBar::ImageScanner->new(); | |||||||||
| 155 | |||||||||||
| 156 | # configure the reader | ||||||||||
| 157 | 0 | $scanner->parse_config("enable"); | |||||||||
| 158 | |||||||||||
| 159 | # scan the image for barcodes | ||||||||||
| 160 | 0 | my $n = $scanner->scan_image($image); | |||||||||
| 161 | |||||||||||
| 162 | # extract results | ||||||||||
| 163 | 0 | my @qrcodes; | |||||||||
| 164 | 0 | foreach my $symbol ($image->get_symbols()) { | |||||||||
| 165 | # do something useful with results | ||||||||||
| 166 | 0 | my $data = $symbol->get_data(); | |||||||||
| 167 | |||||||||||
| 168 | 0 | ▶ | if ($data =~ /^shc:\//) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 0 | push (@qrcodes, $data); | |||||||||
| 170 | } | ||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | # clean up | ||||||||||
| 174 | 0 | undef($image); | |||||||||
| 175 | 0 | return @qrcodes; | |||||||||
| 176 | } | ||||||||||
| 177 | 3 | 1; | |||||||||