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

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