script/barcodegen

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/GD/Barcode/Image.pm ↑ Summary
1 #!/usr/local/bin/perl -w
2 #
3 # barcodegen - a program for making barcodes in various image formats
4 #
5 # GD::Barcode::Image is the package that contains barcodegen
6 #
7 # uses GD::Barcode, supports following types of barcodes:
8 # Code39 COOP2of5 EAN13 EAN8 IATA2of5 Industrial2of5
9 # ITF Matrix2of5 NW7 QRcode UPCA UPCE
10 # Barcode info: http://www.makebarcode.com/specs/speclist.html
11 # For output images, uses Image::Magick - so output can be any format
12 # that Image::Magick supports. This requires the GD module also.
13 # -----------------------------------------------------------------------
14 # Created: May 2007
15 # Copyright (C) 2007 Avinash Chopde <avinash@aczoom.com> www.aczoom.com
16 # -----------------------------------------------------------------------
17 # License
18 # This program is free software; you can redistribute it and/or modify it
19 # under the same terms as Perl itself.
20 # See http://www.perl.com/perl/misc/Artistic.html.
21 # -----------------------------------------------------------------------
22  
23 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 10 ▶use Getopt::Long 2.33; # > 2.32 for --version flag
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 0 ▶use GD::Barcode::Image;
Subroutine BEGIN: called CC=1 SCAR=2.6
29 use Image::Magick;
30 use Carp;
31  
32 our $VERSION = 1.03;
33  
34 # ---------------------------------------
35 my $man = 0;
36 my $help = 0;
37 my $verbose = 0;
38 my $type = 'Code39'; # barcode type (Code39, EAM13, UPCA etc)
39 my $format = 'PNG'; # output type (PNG, JPEG, EPS, PDF, etc)
40 my $filename = ''; # output file name
41 my $notext = undef; # if set, create barcode only, with no text label
42 my $height = 36; # default height in pixels for non-QRCode
43 my $qrsize = undef; # Module Size for QRCode
44 my $qrecc = undef; # only for QRCode: Error Correction Capability
45 my $qrversion = undef; # only for QRCode: Version
46 my $border = '10x7'; # border SIZE, or WIDTHxHEIGHT, example: 20 or 20x3
47  
48 GetOptions(
49 'type=s' => \$type, # barcode type - code39, etc
50 'format=s' => \$format, # output format: png, jpeg, eps, pdf, etc
51 'notext' => \$notext, # create barcode only, with no text label
52 'height=i' => \$height, # image height in pixels
53 'qrsize=i' => \$qrsize, # QRcode - Module Size
54 'qrecc=s' => \$qrecc, # QRcode only - Error Correction Capability
55 'qrversion=i' => \$qrversion, # QRcode only - Version
56 'border=s' => \$border, # border SIZE or WIDTHxHEIGHT
57 'write=s' => \$filename, # output file name
58 'verbose|v' => \$verbose, # include info/warnings in output, to STDERR
59 'help|h|?' => \$help,
60 'man' => \$man, # perldoc man page output
61 ) or pod2usage(2);
62  
63 pod2usage(1)
64 if $help;
65 pod2usage( -verbose => 2 )
66 if $man;
67  
68 # ---------------------------------------
69 # check arguments - border must be two numbers
70 my ( $bwidth, $bheight ) = split( /x/, $border, 2 );
71 if ($bwidth) {
72 $bheight = $bwidth unless ($bheight);
73 pod2usage("** Error: --border arguments ($border) invalid")
74 if ( $bwidth !~ /^(([+]?\d+)|([+]?(\d+\.\d+|\d+\.|\.\d+)))$/
75 || $bheight !~ /^(([+]?\d+)|([+]?(\d+\.\d+|\d+\.|\.\d+)))$/ );
76 # accept numbers: 10 +11 12. .13 14.15
77  
78 $border = $bwidth . 'x' . $bheight;
79 }
80  
81 # barcode text must be provided
82 my $text = $ARGV[0]; # barcode text
83  
84 # if 0 or more than 1 arguments were given, is error.
85 pod2usage("** Error: No barcode text provided.")
86 if ( @ARGV == 0 || !$text );
87 pod2usage("** Error: More than one barcode text string provided.")
88 if ( @ARGV > 1 );
89  
90 # only QRcode uses these arguments
91 if ($type =~ /QRcode/) {
92 if (defined($notext)) {
93 carp "Warning: ignoring notext parameter (for QRcode),";
94 }
95 $notext = undef;
96 $height = undef;
97 } else {
98 if (defined($qrecc) || defined($qrversion)) {
99 carp "Warning: ignoring ecc or version parameters (not QRcode),";
100 $qrecc = undef;
101 $qrversion = undef;
102 }
103 $qrsize = undef;
104 }
105  
106 # ---------------------------------------
107 # informational messages
108 if ( $verbose > 0 ) {
109 print STDERR " Creating barcode for string \"$text\"\n";
110 print STDERR " Type: $type\n";
111 print STDERR " Height: $height pixels\n" if ($height);
112 print STDERR " Border: $border\n" if ($border);
113 print STDERR " No Text will be printed on image.\n" if ($notext);
114 print STDERR " QRcode Module Size: $qrsize\n" if ($qrsize);
115 print STDERR " QRcode Error Correction Capability: $qrecc\n" if ($qrecc);
116 print STDERR " QRcode Version: $qrversion\n" if ($qrversion);
117 print STDERR " Output image format: $format\n";
118 print STDERR " Output file name: \"$filename\"\n" if ($filename);
119 }
120  
121 # ---------------------------------------
122 # using GD::Barcode and GD, create PNG image of Barcode, with no border
123  
124 my %rhPrm = ( # Used for QRcode type only
125 Ecc => $qrecc,
126 Version => $qrversion,
127 ModuleSize => $qrsize,
128 );
129  
130 my $gdbcim = GD::Barcode::Image->new( $type, $text, \%rhPrm );
131 croak "** Error: Barcode $type failed for $text: ${GD::Barcode::errStr},"
132 unless ($gdbcim);
133  
134 my $image = $gdbcim->plot_imagick( NoText => $notext, Height => $height );
135 croak "** Error: Barcode imaging failed: ${GD::Barcode::errStr},"
136 unless ($image);
137  
138 # using Image::Magick, add optional border, and convert to required format
139 my $errStr;
140 $errStr = $image->Set( bordercolor => 'white' );
141 carp "Warning: \$image->Set bordercolor failed: $errStr,"
142 if ("$errStr");
143  
144 if ($border) {
145 $errStr = $image->Border( geometry => $border );
146 carp "Warning: \$image->Border failed: $errStr,"
147 if ("$errStr");
148 }
149  
150 # if required, redirect to output file
151 if ($filename) {
152 open( STDOUT, '>', $filename )
153 or croak "** Error: Can't redirect to output ($filename): $!,";
154 }
155 binmode(STDOUT);
156  
157 my $blob = $image->ImageToBlob( magick => $format );
158 croak "** Error: Could not write image in format ($format): $errStr"
159 if ("$errStr");
160 print $blob;
161 # $image->Write(file => \*STDOUT, magick => $format); # don't use!
162 # file => \*STDOUT ... leaves program hanging - reading from STDIN, does
163 # not exit cleanly. $format:- works correctly, on machine tested.
164 # $image->Write($format . ':-'); # works...
165 # $blob = $image->ImageToBlob(magick=>$format); print $blob; # another way...
166  
167 close(STDOUT);
168  
169 # -----------------------------------------------------------------------
170  
171 __END__
« lib/GD/Barcode/Image.pm ↑ Summary