| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | App::Basis::ConvertText::Plugin::Barcode | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | my $content = "12345678" ; | ||
| 9 | my $params = { | ||
| 10 | type => "EAN8" | ||
| 11 | } ; | ||
| 12 | my $obj = App::Basis::ConvertText2::Plugin::Barcode->new() ; | ||
| 13 | my $out = $obj->process( 'barcode', $content, $params) ; | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | convert a text string into a QRcode PNG, requires qrencode program | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | # ---------------------------------------------------------------------------- | ||
| 22 | |||
| 23 | package App::Basis::ConvertText2::Plugin::Barcode; | ||
| 24 | $App::Basis::ConvertText2::Plugin::Barcode::VERSION = '0.4'; | ||
| 25 | 1 | ▶ | use 5.10.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 28 | 1 | ▶ | use Path::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 1 | ▶ | use Capture::Tiny ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | 0 | ▶ | use Image::Resize; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 31 | use GD::Barcode; | ||
| 32 | use Moo; | ||
| 33 | use App::Basis; | ||
| 34 | use App::Basis::ConvertText2::Support; | ||
| 35 | use namespace::autoclean; | ||
| 36 | |||
| 37 | has handles => ( | ||
| 38 | is => 'ro', | ||
| 39 | init_arg => undef, | ||
| 40 | default => sub { [qw{qrcode barcode}] } | ||
| 41 | ); | ||
| 42 | |||
| 43 | use constant QRCODE => 'qrencode'; | ||
| 44 | |||
| 45 | my @_barcodes = ( | ||
| 46 | qw(Code39 EAN13 EAN8 COOP2of5 IATA2of5 Industrial2of5 | ||
| 47 | ITF Matrix2of5 NW7 QRcode UPCA UPCE) | ||
| 48 | ); | ||
| 49 | my %valid_barcodes = map { lc($_) => $_ } @_barcodes; | ||
| 50 | |||
| 51 | # ---------------------------------------------------------------------------- | ||
| 52 | |||
| 53 | =item barcode | ||
| 54 | |||
| 55 | create a qrcode image, just use default options for now | ||
| 56 | |||
| 57 | |||
| 58 | parameters | ||
| 59 | filename - filename to save the created image as | ||
| 60 | |||
| 61 | hashref params of | ||
| 62 | size - size of image, widthxheight - optional | ||
| 63 | version - version of the qrcode to create, defaults to 2 | ||
| 64 | pixels - number of pixels that make a bit, defaults to 2 | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub process { | ||
| 69 | my $self = shift; | ||
| 70 | my ( $tag, $content, $params, $cachedir ) = @_; | ||
| 71 | my $qrcode ; | ||
| 72 | |||
| 73 | # we have a special tag handler for qrcodes | ||
| 74 | if ( $tag eq 'qrcode' ) { | ||
| 75 | $params->{type} = 'QRcode'; | ||
| 76 | $qrcode->{Version} = $params->{version} || 2 ; | ||
| 77 | $qrcode->{ModuleSize} = $params->{pixels} || 2 ; | ||
| 78 | } | ||
| 79 | |||
| 80 | # strip any ending linefeed | ||
| 81 | chomp $content; | ||
| 82 | return "" if ( !$content ); | ||
| 83 | |||
| 84 | # get the type as BG::Barcode understands it | ||
| 85 | my $type = $valid_barcodes{ lc($params->{type}) } ; | ||
| 86 | # check if we can process this barcode | ||
| 87 | if ( !$type ) { | ||
| 88 | |||
| 89 | warn "$params->{type} is not a valid barcode type"; | ||
| 90 | |||
| 91 | # let the caller put the block back together | ||
| 92 | return undef; | ||
| 93 | } | ||
| 94 | |||
| 95 | # we can use the cache or process everything ourselves | ||
| 96 | my $sig = create_sig( $content, $params ); | ||
| 97 | my $filename = cachefile( $cachedir, "$sig.png" ); | ||
| 98 | if ( !-f $filename ) { | ||
| 99 | my $gdb ; | ||
| 100 | # sometimes it throws out some warnings, lets hide them | ||
| 101 | my ($stdout, $stderr, @result) = capture { | ||
| 102 | $gdb = GD::Barcode->new( $type, $content, $qrcode ); | ||
| 103 | } ; | ||
| 104 | if ( !$gdb ) { | ||
| 105 | warn "warning $tag $params->{type}: " . $GD::Barcode::errStr; | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | my $gd = $gdb->plot( NoText => $params->{notext}, Height => $params->{height} ); | ||
| 109 | path($filename)->spew_raw( $gd->png ); | ||
| 110 | |||
| 111 | # my $cmd = QRCODE . " -o$filename '$content'"; | ||
| 112 | # my ( $exit, $stdout, $stderr ) = run_cmd($cmd); | ||
| 113 | |||
| 114 | # for some reason qrcodes may need scaling | ||
| 115 | if ( -f $filename && $type eq 'QRcode' && $params->{height}) { | ||
| 116 | my $image = Image::Resize->new($filename); | ||
| 117 | my $gd = $image->resize( $params->{height}, $params->{height} ); | ||
| 118 | |||
| 119 | # overwrite original file with resized version | ||
| 120 | if ($gd) { | ||
| 121 | path($filename)->spew_raw( $gd->png ); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | my $out; | ||
| 127 | if ( -f $filename ) { | ||
| 128 | |||
| 129 | # create something suitable for the HTML | ||
| 130 | $out = create_img_src( $filename, $params->{title} ) . "\n" ; | ||
| 131 | } | ||
| 132 | return $out; | ||
| 133 | } | ||
| 134 | |||
| 135 | # ---------------------------------------------------------------------------- | ||
| 136 | |||
| 137 | 1; | ||