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 / EAN8.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  5.8 KB  |  226 lines

  1. package GD::Barcode::EAN8;
  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=1.10;
  9. my $leftOddBar ={
  10.  '0' => '0001101',
  11.  '1' => '0011001',
  12.  '2' => '0010011',
  13.  '3' => '0111101',
  14.  '4' => '0100011',
  15.  '5' => '0110001',
  16.  '6' => '0101111',
  17.  '7' => '0111011',
  18.  '8' => '0110111',
  19.  '9' => '0001011'
  20. };
  21. my $rightBar = {
  22.  '0' => '1110010',
  23.  '1' => '1100110',
  24.  '2' => '1101100',
  25.  '3' => '1000010',
  26.  '4' => '1011100',
  27.  '5' => '1001110',
  28.  '6' => '1010000',
  29.  '7' => '1000100',
  30.  '8' => '1001000',
  31.  '9' => '1110100'
  32. };
  33. my $guardBar = 'G0G';
  34. my $centerBar = '0G0G0';
  35. #------------------------------------------------------------------------------
  36. # new (for GD::Barcode::EAN8)
  37. #------------------------------------------------------------------------------
  38. sub new($$) {
  39.   my($sClass, $sTxt) = @_;
  40.   $errStr ='';
  41.   my $oThis = {};
  42.   bless $oThis;
  43.   return undef if($errStr = $oThis->init($sTxt));
  44.   return $oThis;
  45. }
  46. #------------------------------------------------------------------------------
  47. # init (for GD::Barcode::EAN8)
  48. #------------------------------------------------------------------------------
  49. sub init($$){
  50.         my($oThis, $sTxt) =@_;
  51. #Check
  52.     return 'Invalid Characters' if($sTxt =~ /[^0-9]/);
  53.  
  54. #CalcCd
  55.         if( length($sTxt) == 7 ) {
  56.                 $sTxt .= calcEAN8CD( $sTxt ) ;
  57.         }
  58.         elsif(length($sTxt) == 8) {
  59.                 ;
  60.         }
  61.         else {
  62.                 return 'Invalid Length';
  63.         }
  64.         $oThis->{text} = $sTxt;
  65.         return '';
  66. }
  67. #------------------------------------------------------------------------------
  68. # calcEAN8CD (for GD::Barcode::EAN8)
  69. #------------------------------------------------------------------------------
  70. sub calcEAN8CD($) {
  71.   my( $sTxt ) = @_;
  72.   my( $i, $iSum);
  73.  
  74.   my @aWeight = (3, 1, 3, 1, 3, 1, 3);
  75.   $iSum = 0;
  76.   for( $i = 0; $i < 7; $i++ ){
  77.       $iSum += substr($sTxt, $i, 1)  * $aWeight[$i];
  78.   }
  79.   $iSum %= 10;
  80.   $iSum = ($iSum == 0)? 0: (10 - $iSum);
  81.   return "$iSum";
  82. }
  83. #------------------------------------------------------------------------------
  84. # new (for GD::Barcode::EAN8)
  85. #------------------------------------------------------------------------------
  86. sub barcode($) {
  87.     my ($oThis) = @_;
  88.     my($i, $sRes);
  89. #(1) Init
  90.   my $sTxt = $oThis->{text};
  91.   $sRes = $guardBar;            #GUARD
  92.  
  93. #(2) Left 4
  94.   for( $i = 0; $i < 4; $i++ ) {
  95.       $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $leftOddBar );
  96.   }
  97.  
  98. #(3) Center
  99.   $sRes .= $centerBar;
  100.  
  101. #(4) Right 4 bytes
  102.   for( $i = 4; $i < 8; $i++ ) {
  103.     $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $rightBar );
  104.   }
  105. #(5)GUARD
  106.   $sRes .= $guardBar;
  107.   return $sRes;
  108. }
  109. #------------------------------------------------------------------------------
  110. # plot (for GD::Barcode::EAN8)
  111. #------------------------------------------------------------------------------
  112. sub plot($;%) {
  113.   my($oThis, %hParam) =@_;
  114.  
  115.   my $sPtn = $oThis->barcode();
  116.  
  117. #Create Image
  118.   my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
  119.   my($oGd, $cBlack);
  120.   if($hParam{NoText}) {
  121.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  122.   }
  123.   else {
  124.         my($fW,$fH) = (GD::Font->Small->width,GD::Font->Small->height);
  125.         my $iWidth = length($sPtn);
  126. #$      ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
  127.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0);
  128.         $oGd->string(GD::Font->Small, $fW +  1, $iHeight - $fH, substr($oThis->{text}, 0, 4), $cBlack);
  129.         $oGd->string(GD::Font->Small, $fW + 33, $iHeight - $fH, substr($oThis->{text}, 4, 4), $cBlack);
  130.   }
  131.   return $oGd;
  132. }
  133. 1;
  134. __END__
  135.  
  136.  
  137.  
  138. =head1 NAME
  139.  
  140. GD::Barcode::EAN8 - Create EAN8(JAN8) barcode image with GD
  141.  
  142. =head1 SYNOPSIS
  143.  
  144. I<ex. CGI>
  145.  
  146.   use GD::Barcode::EAN8;
  147.   binmode(STDOUT);
  148.   print "Content-Type: image/png\n\n";
  149.   print GD::Barcode::EAN8->new('1234567')->plot->png;
  150.  
  151. I<with Error Check>
  152.  
  153.   my $oGdBar = GD::Barcode::EAN8->new('123456789');
  154.   die $GD::Barcode::EAN8::errStr unless($oGdBar);       #Invalid Length
  155.  
  156.  
  157. =head1 DESCRIPTION
  158.  
  159. GD::Barcode::EAN8 is a subclass of GD::Barcode and allows you to
  160. create EAN8(JAN8) barcode image with GD.
  161. This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".
  162.  
  163. =head2 new
  164.  
  165. I<$oGdBar> = GD::Barcode::EAN8->new(I<$sTxt>);
  166.  
  167. Constructor.
  168. Creates a GD::Barcode::EAN8 object for I<$sTxt>.
  169. I<$sTxt> has 7 or 8 numeric characters([0-9]).
  170. If I<$sTxt> has 12 characters, this module calacurates CD for you.
  171.  
  172. =head2 plot()
  173.  
  174. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  175.  
  176. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  177. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  178.  
  179.  ex.
  180.   my $oGdB = GD::Barcode::EAN8->new('1234567');
  181.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  182.   # $sGD is a GD image with Height=>20 pixels, with no text.
  183.  
  184. =head2 barcode()
  185.  
  186. I<$sPtn> = $oGdBar->barcode();
  187.  
  188. returns a barcode pattern in string with '1', 'G' and '0'.
  189. '1' means black, 'G' also means black but little bit long,
  190. '0' means white.
  191.  
  192.  ex.
  193.   my $oGdB = GD::Barcode::EAN8->new('1234567');
  194.   my $sPtn = $oGdB->barcode();
  195.   # $sPtn = '';
  196.  
  197. =head2 $errStr
  198.  
  199. $GD::Barcode::EAN8::errStr
  200.  
  201. has error message.
  202.  
  203. =head2 $text
  204.  
  205. $oGdBar->{$text}
  206.  
  207. has barcode text based on I<$sTxt> specified in L<new> method.
  208.  
  209. =head1 AUTHOR
  210.  
  211. Kawai Takanori GCD00051@nifty.ne.jp
  212.  
  213. =head1 COPYRIGHT
  214.  
  215. The GD::Barocde::EAN8 module is Copyright (c) 2000 Kawai Takanori. Japan.
  216. All rights reserved.
  217.  
  218. You may distribute under the terms of either the GNU General Public
  219. License or the Artistic License, as specified in the Perl README file.
  220.  
  221. =head1 SEE ALSO
  222.  
  223. GD::Barcode
  224.  
  225. =cut
  226.