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 / ITF.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  5.3 KB  |  200 lines

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