File Coverage

blib/lib/GD/Barcode/Image.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package GD::Barcode::Image;
2 1     1   64572 use strict;
  1         3  
  1         72  
3 1     1   1727 use Image::Magick;
  0            
  0            
4             use GD::Barcode;
5             require Exporter;
6             use vars qw($VERSION @ISA $AUTOLOAD);
7             @ISA = qw(Exporter); # HAS-A GD::Barcode:, no IS-A
8             $VERSION = 1.03;
9              
10             #------------------------------------------------------------------------------
11             # GD::Barcode::Image extends GD::Barcode functionality.
12             # GD/Barcode.pm is a package with a single factory-like method named new()
13             # new() itself creates objects of a specific Symbology - Barcode Type,
14             # which are derived classes from GD::Barcode
15             # There are other functions in GD/Barcode.pm such as dumpCode, and even
16             # plot(), but these are not object methods, and since there is never any
17             # object created of class GD::Barcode, all are private functions.
18             #
19             # GD::Barcode::Image follows the implementation of GD::Barcode - which is
20             # easiest done by using a member object of GD::Barcode:: type,
21             # and implementing plot_imagick and plot_gd functions.
22             # It would be possible to use AUTOLOAD and proxy all other methods of
23             # GD::Barcode:: classes, but unless someone points out the need for
24             # that, leaving it out for now (see example after END in this file)
25             # And if absolutey needed: callers can look at the GD::Barcode::
26             # object in this object hash: $oThis->{gd_barcode}
27             #------------------------------------------------------------------------------
28             # If these functions need to be merged into GD::Barcode, all that is to
29             # needed from this file are the two functions: plot_imagick and plot_gd -
30             # they will work with very minor modifications. The command-line script
31             # barcodegen should also be carried into GD::Barcode, if this module
32             # becomes obsolete.
33             #------------------------------------------------------------------------------
34             # new() - create an object of this class
35             # [not a factory method - unlike GD::Barcode]
36             #------------------------------------------------------------------------------
37             sub new($$$;$) {
38             my $sClass = shift;
39             my ($sType) = @_;
40              
41             # special check: since Image.pm resides in GD/Barcode, there is
42             # the danger that the GD::Barcode->new() function will load/init
43             # this module as a Barcode Symbology. To prevent this, check for type.
44             if ( grep( /^$sType$/i, 'Image' ) ) {
45             $GD::Barcode::errStr = 'Error in new() - Invalid Barcode Type: "Image"';
46             return undef;
47             }
48              
49             my $oThis = {};
50             bless $oThis, $sClass;
51             my $gdbc = GD::Barcode->new(@_);
52             $oThis->{gd_barcode} = $gdbc;
53             return $gdbc ? $oThis : undef;
54             }
55              
56             #------------------------------------------------------------------------------
57             # init (for GD::Barcode::Image)
58             # special check: since Image.pm resides in GD/Barcode, there is
59             # the danger that the GD::Barcode->new() function will load/init this
60             # module as a Barcode Symbology, so check for type
61             #------------------------------------------------------------------------------
62             sub init() {
63             return 'Error in init() - Invalid Barcode Type: "Image"';
64             }
65              
66             #------------------------------------------------------------------------------
67             # plot_imagick: Convert to Image::Magick Object (for GD::Barcode)
68             # Requires both the GD and Image::Magick modules.
69             # signature is similar to GD::Barcode::QRcode->plot()
70             #------------------------------------------------------------------------------
71             sub plot_imagick($%) {
72             my ( $oThis, %hParam ) = @_;
73              
74             #Create Image
75             my $imNew = undef;
76             eval {
77             require Image::Magick;
78              
79             my ( $gdNew, $png ) = ( undef, undef );
80              
81             $gdNew = $oThis->{gd_barcode}->plot(%hParam);
82             if ($gdNew) {
83             $png = $gdNew->png();
84             $imNew = Image::Magick->new();
85              
86             $GD::Barcode::errStr = $imNew->BlobToImage($png);
87             $imNew = undef
88             if ($GD::Barcode::errStr); # on error, free imagick object
89             }
90             };
91             return $imNew;
92             }
93              
94             #------------------------------------------------------------------------------
95             # plot_gd: Convert to GD::Image Object (for GD::Barcode)
96             # plot_gd created to follow name pattern of "plot_imagick" - plot_
97             # Requires the GD module.
98             #------------------------------------------------------------------------------
99             sub plot_gd($%) {
100             my ( $oThis, %hParam ) = @_;
101             return $oThis->{gd_barcode}->plot(%hParam);
102             }
103              
104             #------------------------------------------------------------------------------
105              
106             1;
107              
108             __END__