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 / UPCE.pm < prev    next >
Encoding:
Perl POD Document  |  2003-06-02  |  7.2 KB  |  267 lines

  1. package GD::Barcode::UPCE;
  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 $oddEven4UPCE = {
  10.  0 => 'EEEOOO',
  11.  1 => 'EEOEOO',
  12.  2 => 'EEOOEO',
  13.  3 => 'EEOOOE',
  14.  4 => 'EOEEOO',
  15.  5 => 'EOOEEO',
  16.  6 => 'EOOOEE',
  17.  7 => 'EOEOEO',
  18.  8 => 'EOEOOE',
  19.  9 => 'EOOEOE'
  20. };
  21. my $leftOddBar ={
  22.  '0' => '0001101',
  23.  '1' => '0011001',
  24.  '2' => '0010011',
  25.  '3' => '0111101',
  26.  '4' => '0100011',
  27.  '5' => '0110001',
  28.  '6' => '0101111',
  29.  '7' => '0111011',
  30.  '8' => '0110111',
  31.  '9' => '0001011'
  32. };
  33. my $leftEvenBar = {
  34.  '0' => '0100111',
  35.  '1' => '0110011',
  36.  '2' => '0011011',
  37.  '3' => '0100001',
  38.  '4' => '0011101',
  39.  '5' => '0111001',
  40.  '6' => '0000101',
  41.  '7' => '0010001',
  42.  '8' => '0001001',
  43.  '9' => '0010111'
  44. };
  45. my $guardBar = 'G0G';
  46. my $UPCrightGuardBar = '0G0G0G';
  47. #------------------------------------------------------------------------------
  48. # new (for Spreadsheet::ParseExcel)
  49. #------------------------------------------------------------------------------
  50. sub new($$) {
  51.   my($sClass, $sTxt) = @_;
  52.   $errStr ='';
  53.   my $oThis = {};
  54.   bless $oThis;
  55.   return undef if($errStr = $oThis->init($sTxt));
  56.   return $oThis;
  57. }
  58. #------------------------------------------------------------------------------
  59. # init (for Spreadsheet::ParseExcel)
  60. #------------------------------------------------------------------------------
  61. sub init($$){
  62.         my($oThis, $sTxt) =@_;
  63.         return 'Invalid characters' if($sTxt =~ /[^0-9]/); 
  64.  
  65. #Check
  66.     my $iLen = length($sTxt);
  67.         if( $iLen == 6 ) {
  68.                 $sTxt = '0' . $sTxt;
  69.         $sTxt .= calcUPCECD( $sTxt );
  70.         }
  71.         elsif($iLen == 7) {
  72.         $sTxt .= calcUPCECD( $sTxt );
  73.         }
  74.         elsif($iLen == 8) {
  75.                 ;
  76.         }
  77.         else {
  78.                 return 'Invalid Length';
  79.         }
  80.         $oThis->{text} = $sTxt;
  81.         return '';
  82. }
  83. #------------------------------------------------------------------------------
  84. # calcUPCACD (for GD::Barcode::UPCE)
  85. #------------------------------------------------------------------------------
  86. sub calcUPCACD {
  87.   my( $sTxt ) = @_;
  88.   my( $i, $iSum, @aWeight);
  89.  
  90.   @aWeight = (3,1,3,1,3,1,3,1,3,1,3);
  91.   $iSum = 0;
  92.   for( $i = 0; $i < 11; $i++ ) {
  93.       $iSum += substr($sTxt, $i, 1)  * $aWeight[$i];
  94.   }
  95.   $iSum %= 10;
  96.   $iSum = ($iSum == 0)? 0: (10 - $iSum);
  97.   return "$iSum";
  98. }
  99. #------------------------------------------------------------------------------
  100. # calcUPCECD (for GD::Barcode::UPCE)
  101. #------------------------------------------------------------------------------
  102. sub calcUPCECD {
  103.   my( $sTxt ) =@_;
  104.   my( $upcA );
  105.  
  106.   my $cLast = substr($sTxt, 6, 1);
  107.   if ($cLast =~ /[0-2]/) {      #0,1,2
  108.         $upcA = substr($sTxt, 0, 3). $cLast . '0' x 4 . substr($sTxt, 3, 3);
  109.   }
  110.   elsif ($cLast eq '3') {
  111.         $upcA = substr($sTxt, 0, 4) . '0' x 5 . substr($sTxt, 4, 2);
  112.   }
  113.   elsif ($cLast eq '4') {
  114.         $upcA = substr($sTxt, 0, 5) . '0' x 5 . substr($sTxt, 5, 1);
  115.   }
  116.   else  { # $cLast =~ /5-9/
  117.         $upcA = substr($sTxt, 0, 6) . '0' x 4 . $cLast;
  118.   }
  119.   return &calcUPCACD( $upcA );
  120. }
  121. #------------------------------------------------------------------------------
  122. # barcode (for GD::Barcode::UPCE)
  123. #------------------------------------------------------------------------------
  124. sub barcode($) {
  125.   my ($oThis) = @_;
  126.   my ($topDigit, $oddEven, $c, $i);
  127.   my ($sRes);
  128.  
  129. #(1)Init
  130.   my $sTxt = $oThis->{text};
  131.   $sRes = $guardBar;            #GUARD
  132.   $oddEven = $oddEven4UPCE->{substr($sTxt, 7, 1)};
  133.  
  134. #(2)Left 6 (Skip 1 character)
  135.     for( $i = 1; $i < 7; $i++ ){
  136.         $c = substr($sTxt, $i, 1);
  137.                 $sRes .= GD::Barcode::barPtn($c, 
  138.                                 ( substr($oddEven, $i-1, 1) eq 'O' )?
  139.                                                 $leftOddBar : $leftEvenBar);
  140.     }
  141. #
  142.   $sRes .= $UPCrightGuardBar;
  143.   return $sRes;
  144.  
  145. }
  146. #------------------------------------------------------------------------------
  147. # plot (for Spreadsheet::ParseExcel)
  148. #------------------------------------------------------------------------------
  149. sub plot($%) {
  150.   my($oThis, %hParam) =@_;
  151.   my $sTxt = $oThis->{text};
  152.   my $sPtn = $oThis->barcode();
  153.  
  154. #Create Image
  155.   my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
  156.   my ($oGd, $cBlack);
  157.   if($hParam{NoText}) {
  158.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  159.   }
  160.   else {
  161.         my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
  162.         my $iWidth = length($sPtn)+ 2*($fW+1);
  163.         #Bar Image
  164.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, $fW+1);
  165.         #String
  166.         $oGd->string(GD::Font->Small,        0, $iHeight - $fH, substr($sTxt, 0, 1), $cBlack);
  167.         $oGd->string(GD::Font->Small, $fW +  8, $iHeight - $fH, substr($sTxt, 1, 6), $cBlack);
  168.         $oGd->string(GD::Font->Small, $fW + 54, $iHeight - $fH, substr($sTxt, 7, 1), $cBlack);
  169.   }
  170.   return $oGd;
  171.  
  172.   return $oGd;
  173. }
  174. 1;
  175. __END__
  176.  
  177.  
  178. =head1 NAME
  179.  
  180. GD::Barcode::UPCE - Create UPC-E barcode image with GD
  181.  
  182. =head1 SYNOPSIS
  183.  
  184. I<ex. CGI>
  185.  
  186.   use GD::Barcode::UPCE;
  187.   binmode(STDOUT);
  188.   print "Content-Type: image/png\n\n";
  189.   print GD::Barcode::UPCE->new('123456')->plot->png;
  190.  
  191. I<with Error Check>
  192.  
  193.   my $oGdBar = GD::Barcode::UPCE->new('123456789');
  194.   die $GD::Barcode::UPCE::errStr unless($oGdBar);       #Invalid Length
  195.  
  196. =head1 DESCRIPTION
  197.  
  198. GD::Barcode::UPCE is a subclass of GD::Barcode and allows you to
  199. create UPC-E barcode image with GD.
  200. This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".
  201.  
  202. =head2 new
  203.  
  204. I<$oGdBar> = GD::Barcode::UPCE->new(I<$sTxt>);
  205.  
  206. Constructor.
  207. Creates a GD::Barcode::UPCE object for I<$sTxt>.
  208. I<$sTxt> has 6 or 7 or 8 numeric characters([0-9]).
  209. If I<$sTxt> has 6 characters, this module adds '0' at the front of I<$sTxt>.
  210. and calculates CD for you.
  211. If I<$sTxt> has 7 characters, this module calaculates CD for you.
  212.  
  213. =head2 plot()
  214.  
  215. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  216.  
  217. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  218. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  219.  
  220.  ex.
  221.   my $oGdB = GD::Barcode::UPCE->new('123456');
  222.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  223.   # $sGD is a GD image with Height=>20 pixels, with no text.
  224.  
  225. =head2 barcode()
  226.  
  227. I<$sPtn> = $oGdBar->barcode();
  228.  
  229. returns a barcode pattern in string with '1', 'G' and '0'.
  230. '1' means black, 'G' also means black but little bit long,
  231. '0' means white.
  232.  
  233.  ex.
  234.   my $oGdB = GD::Barcode::UPCE->new('123456');
  235.   my $sPtn = $oGdB->barcode();
  236.   # $sPtn = '';
  237.  
  238. =head2 $errStr
  239.  
  240. $GD::Barcode::UPCE::errStr
  241.  
  242. has error message.
  243.  
  244. =head2 $text
  245.  
  246. $oGdBar->{$text}
  247.  
  248. has barcode text based on I<$sTxt> specified in L<new> method.
  249.  
  250. =head1 AUTHOR
  251.  
  252. Kawai Takanori GCD00051@nifty.ne.jp
  253.  
  254. =head1 COPYRIGHT
  255.  
  256. The GD::Barocde::UPCE module is Copyright (c) 2000 Kawai Takanori. Japan.
  257. All rights reserved.
  258.  
  259. You may distribute under the terms of either the GNU General Public
  260. License or the Artistic License, as specified in the Perl README file.
  261.  
  262. =head1 SEE ALSO
  263.  
  264. GD::Barcode
  265.  
  266. =cut
  267.