home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / IATA2of5.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  5.0 KB  |  189 lines

  1. package GD::Barcode::IATA2of5;
  2. use strict;
  3. BEGIN { eval{require 'GD.pm';}; };
  4. use GD::Barcode;
  5. require Exporter;
  6. use vars qw($VERSION @ISA $errStr);
  7. @ISA = qw(GD::Barcode Exporter);
  8. $VERSION=0.01;
  9. #------------------------------------------------------------------------------
  10. # new (for GD::Barcode::IATA2of5)
  11. #------------------------------------------------------------------------------
  12. sub new($$) {
  13.   my($sClass, $sTxt) = @_;
  14.   $errStr ='';
  15.   my $oThis = {};
  16.   bless $oThis;
  17.   return undef if($errStr = $oThis->init($sTxt));
  18.   return $oThis;
  19. }
  20. #------------------------------------------------------------------------------
  21. # init (for GD::Barcode::IATA2of5)
  22. #------------------------------------------------------------------------------
  23. sub init($$){
  24.         my($oThis, $sTxt) =@_;
  25. #Check
  26.     return 'Invalid Characters' if($sTxt =~ /[^0-9]/);
  27. #    return 'Invalid Length' if(length($sTxt) != 17);
  28.  
  29.         $oThis->{text} = $sTxt;
  30.         return '';
  31. }
  32. #------------------------------------------------------------------------------
  33. # new (for GD::Barcode::IATA2of5)
  34. #------------------------------------------------------------------------------
  35. sub barcodeWk($) {
  36.     my($sPtn) =@_;
  37.     my $sRes = '';
  38.     my $sClr = '1';
  39.     for(my $i=0; $i< length($sPtn); $i++) {
  40.       $sRes .= (substr($sPtn, $i, 1) eq '1')? $sClr x 3 : $sClr;
  41.       $sRes .= '0'
  42.     }
  43.         return $sRes;
  44. }
  45. sub barcode($) {
  46.     my ($oThis) = @_;
  47.     my $i;
  48.     my $sRes;
  49.     my $sTxt = $oThis->{text};
  50.     my $rhPtn ={
  51.         'START' => '00',
  52.         '0'     => '00110',
  53.         '1'     => '10001',
  54.         '2'     => '01001',
  55.         '3'     => '11000',
  56.         '4'     => '00101',
  57.         '5'     => '10100',
  58.         '6'     => '01100',
  59.         '7'     => '00011',
  60.         '8'     => '10010',
  61.         '9'     => '01010',
  62.         'STOP'  => '10',
  63.     };
  64.   $sRes = barcodeWk($rhPtn->{START});
  65.   for( $i = 0; $i < length($sTxt); $i++ ){
  66.                 $sRes .= barcodeWk($rhPtn->{substr($sTxt, $i, 1)});
  67.   }
  68.   $sRes .= barcodeWk($rhPtn->{STOP});
  69.   return substr($sRes, 0, length($sRes)- 1);
  70. }
  71.  
  72. #------------------------------------------------------------------------------
  73. # plot (for GD::Barcode::IATA2of5)
  74. #------------------------------------------------------------------------------
  75. sub plot($;%) {
  76.   my($oThis, %hParam) =@_;
  77.  
  78.   my $sTxtWk = $oThis->{text};
  79.   my $sPtn = $oThis->barcode();
  80.  
  81. #Create Image
  82.   my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
  83.   my($oGd, $cBlack);
  84.   if($hParam{NoText}) {
  85.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  86.   }
  87.   else {
  88.         my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
  89.         my $iWidth = length($sPtn);
  90.         #Bar Image
  91.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0);
  92.  
  93.         #String
  94.         $oGd->string(GD::Font->Small, (length($sPtn)-$fW*(length($sTxtWk)))/2, $iHeight - $fH, 
  95.                         $sTxtWk, $cBlack);
  96.   }
  97.   return $oGd;
  98. }
  99. 1;
  100. __END__
  101.  
  102.  
  103.  
  104. =head1 NAME
  105.  
  106. GD::Barcode::IATA2of5 - Create IATA2of5 barcode image with GD
  107.  
  108. =head1 SYNOPSIS
  109.  
  110. I<ex. CGI>
  111.  
  112.   use GD::Barcode::IATA2of5;
  113.   binmode(STDOUT);
  114.   print "Content-Type: image/png\n\n";
  115.   print GD::Barcode::IATA2of5->new('1234567890')->plot->png;
  116.  
  117. I<with Error Check>
  118.  
  119.   my $oGdBar = GD::Barcode::IATA2of5->new('A12345678');
  120.   die $GD::Barcode::IATA2of5::errStr unless($oGdBar);   #Invalid Characters
  121.   $oGdBar->plot->png;
  122.  
  123.  
  124. =head1 DESCRIPTION
  125.  
  126. GD::Barcode::IATA2of5 is a subclass of GD::Barcode and allows you to
  127. create IATA2of5 barcode image with GD.
  128.  
  129. =head2 new
  130.  
  131. I<$oGdBar> = GD::Barcode::IATA2of5->new(I<$sTxt>);
  132.  
  133. Constructor.
  134. Creates a GD::Barcode::IATA2of5 object for I<$sTxt>.
  135. I<$sTxt> has numeric characters([0-9]).
  136.  
  137. =head2 plot()
  138.  
  139. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  140.  
  141. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  142. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  143.  
  144.  ex.
  145.   my $oGdB = GD::Barcode::IATA2of5->new('12345678');
  146.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  147.   # $sGD is a GD image with Height=>20 pixels, with no text.
  148.  
  149. =head2 barcode()
  150.  
  151. I<$sPtn> = $oGdBar->barcode();
  152.  
  153. returns a barcode pattern in string with '1' and '0'.
  154. '1' means black, '0' means white.
  155.  
  156.  ex.
  157.   my $oGdB = GD::Barcode::IATA2of5->new('12345678');
  158.   my $sPtn = $oGdB->barcode();
  159.  
  160. =head2 $errStr
  161.  
  162. $GD::Barcode::IATA2of5::errStr
  163.  
  164. has error message.
  165.  
  166. =head2 $text
  167.  
  168. $oGdBar->{$text}
  169.  
  170. has barcode text based on I<$sTxt> specified in L<new> method.
  171.  
  172. =head1 AUTHOR
  173.  
  174. Kawai Takanori GCD00051@nifty.ne.jp
  175.  
  176. =head1 COPYRIGHT
  177.  
  178. The GD::Barocde::IATA2of5 module is Copyright (c) 2000 Kawai Takanori. Japan.
  179. All rights reserved.
  180.  
  181. You may distribute under the terms of either the GNU General Public
  182. License or the Artistic License, as specified in the Perl README file.
  183.  
  184. =head1 SEE ALSO
  185.  
  186. GD::Barcode
  187.  
  188. =cut
  189.