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 / UPCA.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  6.3 KB  |  238 lines

  1. package GD::Barcode::UPCA;
  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::UPCA)
  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::UPCA)
  48. #------------------------------------------------------------------------------
  49. sub init($$){
  50.         my($oThis, $sTxt) =@_;
  51.         return 'Invalid characters' if($sTxt =~ /[^0-9]/); 
  52.  
  53. #Check
  54.     my $iLen = length($sTxt);
  55.         if($iLen == 11) {
  56.         $sTxt .= calcUPCACD( $sTxt );
  57.         }
  58.         elsif($iLen == 12) {
  59.                 ;
  60.         }
  61.         else {
  62.                 return 'Invalid Length';
  63.         }
  64.         $oThis->{text} = $sTxt;
  65.         return '';
  66. }
  67. #------------------------------------------------------------------------------
  68. # calcUPCACD (for GD::Barcode::UPCA)
  69. #------------------------------------------------------------------------------
  70. sub calcUPCACD {
  71.   my( $sTxt ) = @_;
  72.   my( $i, $iSum, @aWeight);
  73.  
  74.   @aWeight = (3,1,3,1,3,1,3,1,3,1,3);
  75.   $iSum = 0;
  76.   for( $i = 0; $i < 11; $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::UPCA)
  85. #------------------------------------------------------------------------------
  86. sub barcode($) {
  87.   my ($oThis) = @_;
  88.   my ($topDigit, $oddEven, $c, $i);
  89.   my ($sRes);
  90.  
  91. #(1)Init
  92.   my $sTxt = $oThis->{text};
  93.   $sRes = $guardBar;            #GUARD
  94. #(2)Left 6 letters
  95.   my $s1st = GD::Barcode::barPtn( substr($sTxt, 0, 1), $leftOddBar );
  96.   $s1st =~ tr/1/G/;
  97.   $sRes .= $s1st;
  98.   for( $i = 1; $i < 6; $i++ ) {
  99.       $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $leftOddBar );
  100.   }
  101.  
  102. #(4)Center
  103.   $sRes .= $centerBar;
  104.  
  105. #(5)Right
  106.   for( $i = 6; $i < 11; $i++ ) {
  107.       $sRes .= GD::Barcode::barPtn( substr($sTxt, $i, 1), $rightBar );
  108.   }
  109.   my $sLast = GD::Barcode::barPtn( substr($sTxt, 11, 1), $rightBar );
  110.   $sLast =~ tr/1/G/;
  111.   $sRes .= $sLast;
  112.  
  113. #(6)GUARD
  114.   $sRes .= $guardBar;
  115.   return $sRes;
  116. }
  117. #------------------------------------------------------------------------------
  118. # plot (for GD::Barcode::UPCA)
  119. #------------------------------------------------------------------------------
  120. sub plot($%) {
  121.   my($oThis, %hParam) =@_;
  122.  
  123.   my $sTxt = $oThis->{text};
  124.   my $sPtn = $oThis->barcode();
  125.  
  126. #Create Image
  127.   my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
  128.   my ($oGd, $cBlack);
  129.   if($hParam{NoText}) {
  130.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  131.   }
  132.   else {
  133.         my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
  134.         my $iWidth = length($sPtn)+ 2*($fW+1);
  135.         #Bar Image
  136.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
  137.         #String
  138.         $oGd->string(GD::Font->Small,        0, $iHeight - $fH, substr($sTxt, 0, 1), $cBlack);
  139.         $oGd->string(GD::Font->Small, $fW + 14, $iHeight - $fH, substr($sTxt, 1, 5), $cBlack);
  140.         $oGd->string(GD::Font->Small, $fW + 53, $iHeight - $fH, substr($sTxt, 6, 5), $cBlack);
  141.         $oGd->string(GD::Font->Small, $fW + 98, $iHeight - $fH, substr($sTxt,11, 1), $cBlack);
  142.   }
  143.   return $oGd;
  144. }
  145. 1;
  146. __END__
  147.  
  148.  
  149. =head1 NAME
  150.  
  151. GD::Barcode::UPCA - Create UPC-A barcode image with GD
  152.  
  153. =head1 SYNOPSIS
  154.  
  155. I<ex. CGI>
  156.  
  157.   use GD::Barcode::UPCA;
  158.   binmode(STDOUT);
  159.   print "Content-Type: image/png\n\n";
  160.   print GD::Barcode::UPCA->new('12345678901')->plot->png;
  161.  
  162. I<with Error Check>
  163.  
  164.   my $oGdBar = GD::Barcode::UPCA->new('123456789');
  165.   die $GD::Barcode::UPCA::errStr unless($oGdBar);       #Invalid Length
  166.  
  167.  
  168. =head1 DESCRIPTION
  169.  
  170. GD::Barcode::UPCA is a subclass of GD::Barcode and allows you to
  171. create UPC-A barcode image with GD.
  172. This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".
  173.  
  174. =head2 new
  175.  
  176. I<$oGdBar> = GD::Barcode::UPCA->new(I<$sTxt>);
  177.  
  178. Constructor.
  179. Creates a GD::Barcode::UPCA object for I<$sTxt>.
  180. I<$sTxt> has 11 or 12 numeric characters([0-9]).
  181. If I<$sTxt> has 11 characters, this module calacurates CD for you.
  182.  
  183. =head2 plot()
  184.  
  185. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  186.  
  187. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  188. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  189.  
  190.  ex.
  191.   my $oGdB = GD::Barcode::UPCA->new('12345678901');
  192.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  193.   # $sGD is a GD image with Height=>20 pixels, with no text.
  194.  
  195. =head2 barcode()
  196.  
  197. I<$sPtn> = $oGdBar->barcode();
  198.  
  199. returns a barcode pattern in string with '1', 'G' and '0'.
  200. '1' means black, 'G' also means black but little bit long,
  201. '0' means white.
  202.  
  203.  ex.
  204.   my $oGdB = GD::Barcode::UPCA->new('12345678901');
  205.   my $sPtn = $oGdB->barcode();
  206.   # $sPtn = '';
  207.  
  208. =head2 $errStr
  209.  
  210. $GD::Barcode::UPCA::errStr
  211.  
  212. has error message.
  213.  
  214. =head2 $text
  215.  
  216. $oGdBar->{$text}
  217.  
  218. has barcode text based on I<$sTxt> specified in L<new> method.
  219.  
  220.  
  221. =head1 AUTHOR
  222.  
  223. Kawai Takanori GCD00051@nifty.ne.jp
  224.  
  225. =head1 COPYRIGHT
  226.  
  227. The GD::Barocde::UPCA module is Copyright (c) 2000 Kawai Takanori. Japan.
  228. All rights reserved.
  229.  
  230. You may distribute under the terms of either the GNU General Public
  231. License or the Artistic License, as specified in the Perl README file.
  232.  
  233. =head1 SEE ALSO
  234.  
  235. GD::Barcode
  236.  
  237. =cut
  238.