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 / Industrial2of5.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  5.0 KB  |  188 lines

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