| 1 | package GD::Barcode::ITF; | ||
| 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=0.01; | ||
| 9 | #------------------------------------------------------------------------------ | ||
| 10 | # new (for GD::Barcode::ITF) | ||
| 11 | #------------------------------------------------------------------------------ | ||
| 12 | sub new($$) { | ||
| 13 | my($sClass, $sTxt) = @_; | ||
| 14 | $errStr =''; | ||
| 15 | my $oThis = {}; | ||
| 16 | bless $oThis; | ||
| 17 | return undef if($errStr = $oThis->init($sTxt)); | ||
| 18 | return $oThis; | ||
| 19 | } | ||
| 20 | #------------------------------------------------------------------------------ | ||
| 21 | # init (for GD::Barcode::ITF) | ||
| 22 | #------------------------------------------------------------------------------ | ||
| 23 | sub init($$){ | ||
| 24 | my($oThis, $sTxt) =@_; | ||
| 25 | #Check | ||
| 26 | return 'Invalid Characters' if($sTxt =~ /[^0-9]/); | ||
| 27 | |||
| 28 | #Not Set Chec | ||
| 29 | if( length($sTxt) %2 ) { | ||
| 30 | $sTxt .= calcITFCD($sTxt) | ||
| 31 | } | ||
| 32 | $oThis->{text} = $sTxt; | ||
| 33 | return ''; | ||
| 34 | } | ||
| 35 | #------------------------------------------------------------------------------ | ||
| 36 | # calcITFCD (for GD::Barcode::ITF) | ||
| 37 | #------------------------------------------------------------------------------ | ||
| 38 | sub calcITFCD($) { | ||
| 39 | my( $sTxt ) = @_; | ||
| 40 | my( $i, $iSum); | ||
| 41 | |||
| 42 | $iSum = 0; | ||
| 43 | for( $i = 0; $i < length($sTxt); $i++ ){ | ||
| 44 | $iSum += substr($sTxt, $i, 1) * (($i%2)? 1: 3); | ||
| 45 | } | ||
| 46 | $iSum %= 10; | ||
| 47 | $iSum = ($iSum == 0)? 0: (10 - $iSum); | ||
| 48 | return "$iSum"; | ||
| 49 | } | ||
| 50 | #------------------------------------------------------------------------------ | ||
| 51 | # new (for GD::Barcode::ITF) | ||
| 52 | #------------------------------------------------------------------------------ | ||
| 53 | sub barcode($) { | ||
| 54 | my ($oThis) = @_; | ||
| 55 | my($i, $sRes); | ||
| 56 | my $sTxt = $oThis->{text}; | ||
| 57 | my $rhPtn ={ | ||
| 58 | '0' => '00110', | ||
| 59 | '1' => '10001', | ||
| 60 | '2' => '01001', | ||
| 61 | '3' => '11000', | ||
| 62 | '4' => '00101', | ||
| 63 | '5' => '10100', | ||
| 64 | '6' => '01100', | ||
| 65 | '7' => '00011', | ||
| 66 | '8' => '10010', | ||
| 67 | '9' => '01010' | ||
| 68 | }; | ||
| 69 | |||
| 70 | $sRes = ''; | ||
| 71 | $sRes .= '1010'; #START | ||
| 72 | for( $i = 0; $i < length($sTxt); $i+=2 ){ | ||
| 73 | my $sBlack = $rhPtn->{substr($sTxt, $i, 1)}; | ||
| 74 | my $sWhite = $rhPtn->{substr($sTxt, $i+1, 1)}; | ||
| 75 | for(my $j = 0; $j < length($sBlack); $j++) { | ||
| 76 | $sRes .= (substr($sBlack, $j, 1) eq '1')? '1' x 3: '1'; | ||
| 77 | $sRes .= (substr($sWhite, $j, 1) eq '1')? '0' x 3: '0'; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | $sRes .= '1' x 3 . '01'; #STOP | ||
| 81 | return $sRes; | ||
| 82 | } | ||
| 83 | #------------------------------------------------------------------------------ | ||
| 84 | # plot (for GD::Barcode::ITF) | ||
| 85 | #------------------------------------------------------------------------------ | ||
| 86 | sub plot($;%) { | ||
| 87 | my($oThis, %hParam) =@_; | ||
| 88 | |||
| 89 | my $sTxtWk = $oThis->{text}; | ||
| 90 | my $sPtn = $oThis->barcode(); | ||
| 91 | |||
| 92 | #Create Image | ||
| 93 | my $iHeight = ($hParam{Height})? $hParam{Height} : 50; | ||
| 94 | my($oGd, $cBlack); | ||
| 95 | if($hParam{NoText}) { | ||
| 96 | ($oGd, $cBlack) = GD::Barcode::plot($sPtn, length($sPtn), $iHeight, 0, 0); | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | my($fW,$fH) = (GD::Font->Small->width, GD::Font->Small->height); | ||
| 100 | my $iWidth = length($sPtn); | ||
| 101 | #Bar Image | ||
| 102 | ($oGd, $cBlack) = GD::Barcode::plot($sPtn, $iWidth, $iHeight, $fH, 0); | ||
| 103 | |||
| 104 | #String | ||
| 105 | $oGd->string(GD::Font->Small, (length($sPtn)-$fW*(length($sTxtWk)))/2, $iHeight - $fH, | ||
| 106 | $sTxtWk, $cBlack); | ||
| 107 | } | ||
| 108 | return $oGd; | ||
| 109 | } | ||
| 110 | 1; | ||
| 111 | __END__ |