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 / Barcode.pm < prev    next >
Encoding:
Perl POD Document  |  2004-04-27  |  5.0 KB  |  182 lines

  1. package GD::Barcode;
  2. require Exporter;
  3. use strict;
  4. use vars qw($VERSION @ISA $errStr);
  5. @ISA = qw(Exporter);
  6. $VERSION=1.15;
  7. my @aLoaded = ();
  8. #------------------------------------------------------------------------------
  9. # new (for GD::Barcode)
  10. #------------------------------------------------------------------------------
  11. sub new($$$;$) {
  12.         my($sClass, $sType, $sTxt, $rhPrm) = @_;
  13.         my $oThis = {};
  14.         unless(grep(/^$sType$/, @aLoaded)) {
  15.         eval "require 'GD/Barcode/$sType.pm';";
  16.         if($@) {
  17.                 $errStr = "Can't load $sType : $@";
  18.                 return undef;
  19.                 }
  20.                 push(@aLoaded, $sType);
  21.         }
  22.         bless $oThis, "GD::Barcode::$sType";
  23.         return undef if($errStr = $oThis->init($sTxt, $rhPrm));
  24.         return $oThis;
  25. }
  26. #------------------------------------------------------------------------------
  27. # barPtn (for GD::Barcode)
  28. #------------------------------------------------------------------------------
  29. sub barPtn {
  30.     my($bar, $table) = @_;
  31.     my($sWk, $sRes);
  32.  
  33.     $sRes = '';
  34.     foreach $sWk (split(//, $bar)) {
  35.                 $sRes .= $table->{$sWk};
  36.     }
  37.     return $sRes;
  38. }
  39. #------------------------------------------------------------------------------
  40. # dumpCode (for GD::Barcode) for Code39, NW7...
  41. #------------------------------------------------------------------------------
  42. sub dumpCode {
  43.     my( $sCode ) = @_;
  44.     my($sWk, $sRes, $sClr);
  45.  
  46. #Init
  47.     $sRes = '';
  48.     $sClr = '1';  # 1: Black, 0:White
  49.  
  50.     foreach $sWk (split(//, $sCode)) {
  51.                 $sRes .= ($sWk eq '1')? $sClr x 3 : $sClr;      #3 times or Normal
  52.                 $sClr = ($sClr eq '0')? '1': '0';
  53.     }
  54.     return $sRes;
  55. }
  56. #------------------------------------------------------------------------------
  57. # plot (for GD::Barcode)
  58. #------------------------------------------------------------------------------
  59. sub plot($$$$$) {
  60.   my($sBarcode, $iWidth, $iHeight, $fH, $iStart) = @_;
  61.   #Create Image
  62.   my ($gdNew, $cWhite, $cBlack);
  63.   eval {
  64.     $gdNew = GD::Image->new($iWidth, $iHeight);
  65.     $cWhite = $gdNew->colorAllocate(255, 255, 255);
  66.     $cBlack = $gdNew->colorAllocate(  0,   0,   0);
  67.  
  68.     my $iPos =$iStart;
  69.     foreach my $cWk (split(//,$sBarcode)) {
  70.         if($cWk eq '0') {
  71.             $gdNew->line($iPos, 0, $iPos, $iHeight - $fH, $cWhite);
  72.         }
  73.         elsif ($cWk eq 'G') {
  74.             $gdNew->line($iPos, 0, $iPos, $iHeight - 2*($fH/3), $cBlack);
  75.         }
  76.         else {                              #$cWk eq "1" etc.
  77.             $gdNew->line($iPos, 0, $iPos, $iHeight - $fH, $cBlack);
  78.         }
  79.         $iPos++;
  80.     }
  81.   };
  82.   return ($gdNew, $cBlack);
  83. }
  84. #------------------------------------------------------------------------------
  85. # Text (for GD::Barcode)
  86. #------------------------------------------------------------------------------
  87. sub Text($) {
  88.         my($oThis) = @_;
  89.         return $oThis->{text};
  90. }
  91. 1;
  92. __END__
  93.  
  94.  
  95. =head1 NAME
  96.  
  97. GD::Barcode - Create barcode image with GD
  98.  
  99. =head1 SYNOPSIS
  100.  
  101. I<ex. CGI>
  102.  
  103.   use GD::Barcode::UPCE;
  104.   binmode(STDOUT);
  105.   print "Content-Type: image/png\n\n";
  106.   print GD::Barcode->new('EAN13', '123456789012')->plot->png;
  107.  
  108. I<with Error Check>
  109.  
  110.   my $oGdBar = GD::Barcode->new('EAN13', '12345678901');
  111.   die $GD::Barcode::errStr unless($oGdBar);     #Invalid Length
  112.   $oGdBar->plot->png;
  113.  
  114. =head1 DESCRIPTION
  115.  
  116. GD::Barcode is a subclass of GD and allows you to create barcode image with GD.
  117. This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".
  118.  
  119. From 1.14, you can use this module even if no GD (except plot method).
  120.  
  121.  
  122. =head2 new
  123.  
  124. I<$oGdBar> = GD::Barcode::UPCE->new(I<$sType>, I<$sTxt>);
  125.  
  126. Constructor.
  127. Creates a GD::Barcode::I<$sType> object for I<$sTxt>.
  128.  
  129. =head2 plot()
  130.  
  131. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  132.  
  133. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  134. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  135.  
  136.  ex.
  137.   my $oGdB = GD::Barcode->new('EAN13', '123456789012');
  138.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  139.   # $sGD is a GD image with Height=>20 pixels, with no text.
  140.  
  141. =head2 barcode()
  142.  
  143. I<$sPtn> = $oGdBar->barcode();
  144.  
  145. returns a barcode pattern in string with '1' and '0'.
  146. '1' means black, '0' means white.
  147.  
  148.  ex.
  149.   my $oGdB = GD::Barcode->new('UPCE', '123456789012');
  150.   my $sPtn = $oGdB->barcode();
  151.   # $sPtn = '';
  152.  
  153. =head2 $errStr
  154.  
  155. $GD::Barcode::errStr
  156.  
  157. has error message.
  158.  
  159. =head2 $text
  160.  
  161. $oGdBar->{$text}
  162.  
  163. has barcode text based on I<$sTxt> specified in L<new> method.
  164.  
  165. =head1 AUTHOR
  166.  
  167. Kawai Takanori GCD00051@nifty.ne.jp
  168.  
  169. =head1 COPYRIGHT
  170.  
  171. The GD::Barocde module is Copyright (c) 2000 Kawai Takanori. Japan.
  172. All rights reserved.
  173.  
  174. You may distribute under the terms of either the GNU General Public
  175. License or the Artistic License, as specified in the Perl README file.
  176.  
  177. =head1 SEE ALSO
  178.  
  179. GD GD::Barcode subclasses
  180.  
  181. =cut
  182.