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

  1. package GD::Barcode::Code39;
  2. use strict;
  3. #use GD;
  4. BEGIN { eval{require 'GD.pm';}; };
  5. use GD::Barcode;
  6. require Exporter;
  7. use vars qw($VERSION @ISA $errStr);
  8. @ISA = qw(GD::Barcode Exporter);
  9. $VERSION=1.10;
  10. my $code39Bar = {
  11.  '0' => '000110100',
  12.  '1' => '100100001',
  13.  '2' => '001100001',
  14.  '3' => '101100000',
  15.  '4' => '000110001',
  16.  '5' => '100110000',
  17.  '6' => '001110000',
  18.  '7' => '000100101',
  19.  '8' => '100100100',
  20.  '9' => '001100100',
  21.  'A' => '100001001',
  22.  'B' => '001001001',
  23.  'C' => '101001000',
  24.  'D' => '000011001',
  25.  'E' => '100011000',
  26.  'F' => '001011000',
  27.  'G' => '000001101',
  28.  'H' => '100001100',
  29.  'I' => '001001100',
  30.  'J' => '000011100',
  31.  'K' => '100000011',
  32.  'L' => '001000011',
  33.  'M' => '101000010',
  34.  'N' => '000010011',
  35.  'O' => '100010010',
  36.  'P' => '001010010',
  37.  'Q' => '000000111',
  38.  'R' => '100000110',
  39.  'S' => '001000110',
  40.  'T' => '000010110',
  41.  'U' => '110000001',
  42.  'V' => '011000001',
  43.  'W' => '111000000',
  44.  'X' => '010010001',
  45.  'Y' => '110010000',
  46.  'Z' => '011010000',
  47.  '-' => '010000101',
  48.  '*' => '010010100',
  49.  '+' => '010001010',
  50.  '$' => '010101000',
  51.  '%' => '000101010',
  52.  '/' => '010100010',
  53.  '.' => '110000100',
  54.  ' ' => '011000100'
  55. };
  56. #------------------------------------------------------------------------------
  57. # new (for GD::Barcode::Code39)
  58. #------------------------------------------------------------------------------
  59. sub new($$) {
  60.   my($sClass, $sTxt) = @_;
  61.   $errStr ='';
  62.   my $oThis = {};
  63.   bless $oThis;
  64.   return undef if($errStr = $oThis->init($sTxt));
  65.   return $oThis;
  66. }
  67. #------------------------------------------------------------------------------
  68. # init (for GD::Barcode::Code39)
  69. #------------------------------------------------------------------------------
  70. sub init($$){
  71.         my($oThis, $sTxt) =@_;
  72. #Check
  73.     return 'Invalid Characters' if($sTxt =~ /[^0-9A-Z\-*+\$%\/. ]/);
  74.         $oThis->{text} = $sTxt;
  75.         return '';
  76. }
  77. #------------------------------------------------------------------------------
  78. # barcode (for GD::Barcode::Code39)
  79. #------------------------------------------------------------------------------
  80. sub barcode($) {
  81.         my($oThis) = @_;
  82.         my($sTxt);
  83.     my($sWk, $sRes);
  84.  
  85. #       $sTxt = '*'. $oThis->{text} . '*';
  86.         $sTxt = $oThis->{text};
  87.     $sRes = '';
  88.     foreach $sWk (split(//, $sTxt)) {
  89.       $sRes .= GD::Barcode::dumpCode( $code39Bar->{$sWk} .'0' );
  90.     }
  91.     return $sRes;
  92. }
  93. #------------------------------------------------------------------------------
  94. # plot (for GD::Barcode::Code39)
  95. #------------------------------------------------------------------------------
  96. sub plot($;%) {
  97.   my($oThis, %hParam) = @_;
  98.  
  99. #Barcode Pattern
  100. #  my $sTxtWk = '*' . $oThis->{text} . '*';
  101.   my $sTxtWk = $oThis->{text};
  102.   my $sPtn = $oThis->barcode();
  103.  
  104. #Create Image
  105.   my $iHeight = ($hParam{Height})? $hParam{Height} : 50;
  106.   my ($oGd, $cBlack);
  107.   if($hParam{NoText}) {
  108.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0);
  109.   }
  110.   else {
  111.         my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height);
  112.         my $iWidth = length($sPtn);
  113.         #Bar Image
  114.         ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0);
  115.  
  116.         #String
  117.         $oGd->string(GD::Font->Small, (length($sPtn)-$fW*(length($sTxtWk)))/2, $iHeight - $fH, 
  118.                         $sTxtWk, $cBlack);
  119.   }
  120.   return $oGd;
  121. }
  122. 1;
  123. __END__
  124.  
  125.  
  126. =head1 NAME
  127.  
  128. GD::Barcode::Code39 - Create Code39 barcode image with GD
  129.  
  130. =head1 SYNOPSIS
  131.  
  132. I<ex. CGI>
  133.  
  134.   use GD::Barcode::Code39;
  135.   binmode(STDOUT);
  136.   print "Content-Type: image/png\n\n";
  137.   print GD::Barcode::Code39->new('*CODE39IMG*')->plot->png;
  138.  
  139. I<with Error Check>
  140.  
  141.   my $oGdBar = GD::Barcode::Code39->new('*123456789;*');
  142.   die $GD::Barcode::Code39::errStr unless($oGdBar);     #Invalid Characters
  143.  
  144.  
  145. =head1 DESCRIPTION
  146.  
  147. GD::Barcode::Code39 is a subclass of GD::Barcode and allows you to
  148. create CODE-39 barcode image with GD.
  149. This module based on "Generate Barcode Ver 1.02 By Shisei Hanai 97/08/22".
  150.  
  151. =head2 new
  152.  
  153. I<$oGdBar> = GD::Barcode::Code39->new(I<$sTxt>);
  154.  
  155. Constructor.
  156. Creates a GD::Barcode::Code39 object for I<$sTxt>.
  157.  
  158. =head2 plot()
  159.  
  160. I<$oGd> = $oGdBar->plot([Height => I<$iHeight>, NoText => I<0 | 1>]);
  161.  
  162. creates GD object with barcode image for the I<$sTxt> specified at L<new> method.
  163. I<$iHeight> is height of the image. If I<NoText> is 1, the image has no text image of I<$sTxt>.
  164.  
  165.  ex.
  166.   my $oGdB = GD::Barcode::Code39->new('*12345*');
  167.   my $oGD = $oGdB->plot(NoText=>1, Height => 20);
  168.   # $sGD is a GD image with Height=>20 pixels, with no text.
  169.  
  170. =head2 barcode()
  171.  
  172. I<$sPtn> = $oGdBar->barcode();
  173.  
  174. returns a barcode pattern in string with '1' and '0'.
  175. '1' means black, '0' means white.
  176.  
  177.  ex.
  178.   my $oGdB = GD::Barcode::Code39->new('*12345*');
  179.   my $sPtn = $oGdB->barcode();
  180.   # $sPtn = '';
  181.  
  182. =head2 $errStr
  183.  
  184. $GD::Barcode::Code39::errStr
  185.  
  186. has error message.
  187.  
  188. =head2 $text
  189.  
  190. $oGdBar->{text}
  191.  
  192. has barcode text based on I<$sTxt> specified in L<new> method.
  193.  
  194. =head1 AUTHOR
  195.  
  196. Kawai Takanori GCD00051@nifty.ne.jp
  197.  
  198. =head1 COPYRIGHT
  199.  
  200. The GD::Barocde::Code39 module is Copyright (c) 2000 Kawai Takanori. Japan.
  201. All rights reserved.
  202.  
  203. You may distribute under the terms of either the GNU General Public
  204. License or the Artistic License, as specified in the Perl README file.
  205.  
  206. =head1 SEE ALSO
  207.  
  208. GD::Barcode
  209.  
  210. =cut
  211.