| 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__ |