使用允许我手动指定子集的 FPDF 创建 code128 条形码
Posted
技术标签:
【中文标题】使用允许我手动指定子集的 FPDF 创建 code128 条形码【英文标题】:Creating a code128 barcode using FPDF that allows me to manually specify the subsets 【发布时间】:2017-09-24 03:41:21 【问题描述】:我正在使用这个类在 FPDF 中生成 code128 条形码。它运作良好,但我遇到了这样一种情况,即班级使用的默认 A/B/C 子集没有被扫描条形码的人接受。
条形码字符串长度为 28 个字符。他们告诉我,他们以ACCCCCCCCCCCCCCCCCCCCCCCCCCA
的形式接收它,他们希望子集在哪里
BBCCCCCCCCCCCCCCCCCCCCCCCCCC
.
我在网上找到了这段代码,而 cmets 是法语的。语言障碍加上理解障碍使我无法弄清楚这一点。
看起来 [Bstart]
和 [Cstart]
是相关的,但我不确定在哪里使用它。
如果有帮助,我的条形码文本是%000121015501999000025136056
class PDF_Code128 extends FPDF
protected $T128; // Tableau des codes 128
protected $ABCset = ""; // jeu des caractères éligibles au C128
protected $Aset = ""; // Set A du jeu des caractères éligibles
protected $Bset = ""; // Set B du jeu des caractères éligibles
protected $Cset = ""; // Set C du jeu des caractères éligibles
protected $SetFrom; // Convertisseur source des jeux vers le tableau
protected $SetTo; // Convertisseur destination des jeux vers le tableau
protected $JStart = array("A"=>103, "B"=>104, "C"=>105); // Caractères de sélection de jeu au début du C128
protected $JSwap = array("A"=>101, "B"=>100, "C"=>99); // Caractères de changement de jeu
//____________________________ Extension du constructeur _______________________
function __construct($orientation='P', $unit='mm', $format='A4')
parent::__construct($orientation,$unit,$format);
$this->T128[] = array(2, 1, 2, 2, 2, 2); //0 : [ ] // composition des caractères
$this->T128[] = array(2, 2, 2, 1, 2, 2); //1 : [!]
$this->T128[] = array(2, 2, 2, 2, 2, 1); //2 : ["]
$this->T128[] = array(1, 2, 1, 2, 2, 3); //3 : [#]
$this->T128[] = array(1, 2, 1, 3, 2, 2); //4 : [$]
$this->T128[] = array(1, 3, 1, 2, 2, 2); //5 : [%]
$this->T128[] = array(1, 2, 2, 2, 1, 3); //6 : [&]
$this->T128[] = array(1, 2, 2, 3, 1, 2); //7 : [']
$this->T128[] = array(1, 3, 2, 2, 1, 2); //8 : [(]
$this->T128[] = array(2, 2, 1, 2, 1, 3); //9 : [)]
$this->T128[] = array(2, 2, 1, 3, 1, 2); //10 : [*]
$this->T128[] = array(2, 3, 1, 2, 1, 2); //11 : [+]
$this->T128[] = array(1, 1, 2, 2, 3, 2); //12 : [,]
$this->T128[] = array(1, 2, 2, 1, 3, 2); //13 : [-]
$this->T128[] = array(1, 2, 2, 2, 3, 1); //14 : [.]
$this->T128[] = array(1, 1, 3, 2, 2, 2); //15 : [/]
$this->T128[] = array(1, 2, 3, 1, 2, 2); //16 : [0]
$this->T128[] = array(1, 2, 3, 2, 2, 1); //17 : [1]
$this->T128[] = array(2, 2, 3, 2, 1, 1); //18 : [2]
$this->T128[] = array(2, 2, 1, 1, 3, 2); //19 : [3]
$this->T128[] = array(2, 2, 1, 2, 3, 1); //20 : [4]
$this->T128[] = array(2, 1, 3, 2, 1, 2); //21 : [5]
$this->T128[] = array(2, 2, 3, 1, 1, 2); //22 : [6]
$this->T128[] = array(3, 1, 2, 1, 3, 1); //23 : [7]
$this->T128[] = array(3, 1, 1, 2, 2, 2); //24 : [8]
$this->T128[] = array(3, 2, 1, 1, 2, 2); //25 : [9]
$this->T128[] = array(3, 2, 1, 2, 2, 1); //26 : [:]
$this->T128[] = array(3, 1, 2, 2, 1, 2); //27 : [;]
$this->T128[] = array(3, 2, 2, 1, 1, 2); //28 : [<]
$this->T128[] = array(3, 2, 2, 2, 1, 1); //29 : [=]
$this->T128[] = array(2, 1, 2, 1, 2, 3); //30 : [>]
$this->T128[] = array(2, 1, 2, 3, 2, 1); //31 : [?]
$this->T128[] = array(2, 3, 2, 1, 2, 1); //32 : [@]
$this->T128[] = array(1, 1, 1, 3, 2, 3); //33 : [A]
$this->T128[] = array(1, 3, 1, 1, 2, 3); //34 : [B]
$this->T128[] = array(1, 3, 1, 3, 2, 1); //35 : [C]
$this->T128[] = array(1, 1, 2, 3, 1, 3); //36 : [D]
$this->T128[] = array(1, 3, 2, 1, 1, 3); //37 : [E]
$this->T128[] = array(1, 3, 2, 3, 1, 1); //38 : [F]
$this->T128[] = array(2, 1, 1, 3, 1, 3); //39 : [G]
$this->T128[] = array(2, 3, 1, 1, 1, 3); //40 : [H]
$this->T128[] = array(2, 3, 1, 3, 1, 1); //41 : [I]
$this->T128[] = array(1, 1, 2, 1, 3, 3); //42 : [J]
$this->T128[] = array(1, 1, 2, 3, 3, 1); //43 : [K]
$this->T128[] = array(1, 3, 2, 1, 3, 1); //44 : [L]
$this->T128[] = array(1, 1, 3, 1, 2, 3); //45 : [M]
$this->T128[] = array(1, 1, 3, 3, 2, 1); //46 : [N]
$this->T128[] = array(1, 3, 3, 1, 2, 1); //47 : [O]
$this->T128[] = array(3, 1, 3, 1, 2, 1); //48 : [P]
$this->T128[] = array(2, 1, 1, 3, 3, 1); //49 : [Q]
$this->T128[] = array(2, 3, 1, 1, 3, 1); //50 : [R]
$this->T128[] = array(2, 1, 3, 1, 1, 3); //51 : [S]
$this->T128[] = array(2, 1, 3, 3, 1, 1); //52 : [T]
$this->T128[] = array(2, 1, 3, 1, 3, 1); //53 : [U]
$this->T128[] = array(3, 1, 1, 1, 2, 3); //54 : [V]
$this->T128[] = array(3, 1, 1, 3, 2, 1); //55 : [W]
$this->T128[] = array(3, 3, 1, 1, 2, 1); //56 : [X]
$this->T128[] = array(3, 1, 2, 1, 1, 3); //57 : [Y]
$this->T128[] = array(3, 1, 2, 3, 1, 1); //58 : [Z]
$this->T128[] = array(3, 3, 2, 1, 1, 1); //59 : [[]
$this->T128[] = array(3, 1, 4, 1, 1, 1); //60 : [\]
$this->T128[] = array(2, 2, 1, 4, 1, 1); //61 : []]
$this->T128[] = array(4, 3, 1, 1, 1, 1); //62 : [^]
$this->T128[] = array(1, 1, 1, 2, 2, 4); //63 : [_]
$this->T128[] = array(1, 1, 1, 4, 2, 2); //64 : [`]
$this->T128[] = array(1, 2, 1, 1, 2, 4); //65 : [a]
$this->T128[] = array(1, 2, 1, 4, 2, 1); //66 : [b]
$this->T128[] = array(1, 4, 1, 1, 2, 2); //67 : [c]
$this->T128[] = array(1, 4, 1, 2, 2, 1); //68 : [d]
$this->T128[] = array(1, 1, 2, 2, 1, 4); //69 : [e]
$this->T128[] = array(1, 1, 2, 4, 1, 2); //70 : [f]
$this->T128[] = array(1, 2, 2, 1, 1, 4); //71 : [g]
$this->T128[] = array(1, 2, 2, 4, 1, 1); //72 : [h]
$this->T128[] = array(1, 4, 2, 1, 1, 2); //73 : [i]
$this->T128[] = array(1, 4, 2, 2, 1, 1); //74 : [j]
$this->T128[] = array(2, 4, 1, 2, 1, 1); //75 : [k]
$this->T128[] = array(2, 2, 1, 1, 1, 4); //76 : [l]
$this->T128[] = array(4, 1, 3, 1, 1, 1); //77 : [m]
$this->T128[] = array(2, 4, 1, 1, 1, 2); //78 : [n]
$this->T128[] = array(1, 3, 4, 1, 1, 1); //79 : [o]
$this->T128[] = array(1, 1, 1, 2, 4, 2); //80 : [p]
$this->T128[] = array(1, 2, 1, 1, 4, 2); //81 : [q]
$this->T128[] = array(1, 2, 1, 2, 4, 1); //82 : [r]
$this->T128[] = array(1, 1, 4, 2, 1, 2); //83 : [s]
$this->T128[] = array(1, 2, 4, 1, 1, 2); //84 : [t]
$this->T128[] = array(1, 2, 4, 2, 1, 1); //85 : [u]
$this->T128[] = array(4, 1, 1, 2, 1, 2); //86 : [v]
$this->T128[] = array(4, 2, 1, 1, 1, 2); //87 : [w]
$this->T128[] = array(4, 2, 1, 2, 1, 1); //88 : [x]
$this->T128[] = array(2, 1, 2, 1, 4, 1); //89 : [y]
$this->T128[] = array(2, 1, 4, 1, 2, 1); //90 : [z]
$this->T128[] = array(4, 1, 2, 1, 2, 1); //91 : []
$this->T128[] = array(1, 1, 1, 1, 4, 3); //92 : [|]
$this->T128[] = array(1, 1, 1, 3, 4, 1); //93 : []
$this->T128[] = array(1, 3, 1, 1, 4, 1); //94 : [~]
$this->T128[] = array(1, 1, 4, 1, 1, 3); //95 : [DEL]
$this->T128[] = array(1, 1, 4, 3, 1, 1); //96 : [FNC3]
$this->T128[] = array(4, 1, 1, 1, 1, 3); //97 : [FNC2]
$this->T128[] = array(4, 1, 1, 3, 1, 1); //98 : [SHIFT]
$this->T128[] = array(1, 1, 3, 1, 4, 1); //99 : [Cswap]
$this->T128[] = array(1, 1, 4, 1, 3, 1); //100 : [Bswap]
$this->T128[] = array(3, 1, 1, 1, 4, 1); //101 : [Aswap]
$this->T128[] = array(4, 1, 1, 1, 3, 1); //102 : [FNC1]
$this->T128[] = array(2, 1, 1, 4, 1, 2); //103 : [Astart]
$this->T128[] = array(2, 1, 1, 2, 1, 4); //104 : [Bstart]
$this->T128[] = array(2, 1, 1, 2, 3, 2); //105 : [Cstart]
$this->T128[] = array(2, 3, 3, 1, 1, 1); //106 : [STOP]
$this->T128[] = array(2, 1); //107 : [END BAR]
for ($i = 32; $i <= 95; $i++) // jeux de caractères
$this->ABCset .= chr($i);
$this->Aset = $this->ABCset;
$this->Bset = $this->ABCset;
for ($i = 0; $i <= 31; $i++)
$this->ABCset .= chr($i);
$this->Aset .= chr($i);
for ($i = 96; $i <= 127; $i++)
$this->ABCset .= chr($i);
$this->Bset .= chr($i);
for ($i = 200; $i <= 210; $i++) // controle 128
$this->ABCset .= chr($i);
$this->Aset .= chr($i);
$this->Bset .= chr($i);
$this->Cset="0123456789".chr(206);
for ($i=0; $i<96; $i++) // convertisseurs des jeux A & B
@$this->SetFrom["A"] .= chr($i);
@$this->SetFrom["B"] .= chr($i + 32);
@$this->SetTo["A"] .= chr(($i < 32) ? $i+64 : $i-32);
@$this->SetTo["B"] .= chr($i);
for ($i=96; $i<107; $i++) // contrôle des jeux A & B
@$this->SetFrom["A"] .= chr($i + 104);
@$this->SetFrom["B"] .= chr($i + 104);
@$this->SetTo["A"] .= chr($i);
@$this->SetTo["B"] .= chr($i);
//________________ Fonction encodage et dessin du code 128 _____________________
function Code128($x, $y, $code, $w, $h)
$Aguid = ""; // Création des guides de choix ABC
$Bguid = "";
$Cguid = "";
for ($i=0; $i < strlen($code); $i++)
$needle = substr($code,$i,1);
$Aguid .= ((strpos($this->Aset,$needle)===false) ? "N" : "O");
$Bguid .= ((strpos($this->Bset,$needle)===false) ? "N" : "O");
$Cguid .= ((strpos($this->Cset,$needle)===false) ? "N" : "O");
$SminiC = "OOOO";
$IminiC = 4;
$crypt = "";
while ($code > "")
// BOUCLE PRINCIPALE DE CODAGE
$i = strpos($Cguid,$SminiC); // forçage du jeu C, si possible
if ($i!==false)
$Aguid [$i] = "N";
$Bguid [$i] = "N";
if (substr($Cguid,0,$IminiC) == $SminiC) // jeu C
$crypt .= chr(($crypt > "") ? $this->JSwap["C"] : $this->JStart["C"]); // début Cstart, sinon Cswap
$made = strpos($Cguid,"N"); // étendu du set C
if ($made === false)
$made = strlen($Cguid);
if (fmod($made,2)==1)
$made--; // seulement un nombre pair
for ($i=0; $i < $made; $i += 2)
$crypt .= chr(strval(substr($code,$i,2))); // conversion 2 par 2
$jeu = "C";
else
$madeA = strpos($Aguid,"N"); // étendu du set A
if ($madeA === false)
$madeA = strlen($Aguid);
$madeB = strpos($Bguid,"N"); // étendu du set B
if ($madeB === false)
$madeB = strlen($Bguid);
$made = (($madeA < $madeB) ? $madeB : $madeA ); // étendu traitée
$jeu = (($madeA < $madeB) ? "B" : "A" ); // Jeu en cours
$crypt .= chr(($crypt > "") ? $this->JSwap[$jeu] : $this->JStart[$jeu]); // début start, sinon swap
$crypt .= strtr(substr($code, 0,$made), $this->SetFrom[$jeu], $this->SetTo[$jeu]); // conversion selon jeu
$code = substr($code,$made); // raccourcir légende et guides de la zone traitée
$Aguid = substr($Aguid,$made);
$Bguid = substr($Bguid,$made);
$Cguid = substr($Cguid,$made);
$check = ord($crypt[0]); // calcul de la somme de contrôle
for ($i=0; $i<strlen($crypt); $i++)
$check += (ord($crypt[$i]) * $i);
$check %= 103;
$crypt .= chr($check) . chr(106) . chr(107); // Chaine cryptée complète
$i = (strlen($crypt) * 11) - 8; // calcul de la largeur du module
$modul = $w/$i;
for ($i=0; $i<strlen($crypt); $i++) // BOUCLE D'IMPRESSION
$c = $this->T128[ord($crypt[$i])];
for ($j=0; $j<count($c); $j++)
$this->Rect($x,$y,$c[$j]*$modul,$h,"F");
$x += ($c[$j++]+$c[$j])*$modul;
function Rotate($angle,$x=-1,$y=-1)
if($x==-1)
$x=$this->x;
if($y==-1)
$y=$this->y;
if($this->angle!=0)
$this->_out('Q');
$this->angle=$angle;
if($angle!=0)
$angle*=M_PI/180;
$c=cos($angle);
$s=sin($angle);
$cx=$x*$this->k;
$cy=($this->h-$y)*$this->k;
$this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
【问题讨论】:
【参考方案1】:如果这对将来的某人有帮助,这就是我解决问题的方法。
据我所知,条形码总是 28 个字符长,然后我硬编码创建 $crypt
$crypt = '';
$crypt .= chr($this->JStart["B"]);
$crypt .= strtr('%0', $this->SetFrom[$jeu], $this->SetTo[$jeu]);
$crypt .= chr($this->JSwap["C"]);
$crypt .= chr( substr($inputCode,2,2) );
$crypt .= chr( substr($inputCode,4,2) );
$crypt .= chr( substr($inputCode,6,2) );
$crypt .= chr( substr($inputCode,8,2) );
$crypt .= chr( substr($inputCode,10,2) );
$crypt .= chr( substr($inputCode,12,2) );
$crypt .= chr( substr($inputCode,14,2) );
$crypt .= chr( substr($inputCode,16,2) );
$crypt .= chr( substr($inputCode,18,2) );
$crypt .= chr( substr($inputCode,20,2) );
$crypt .= chr( substr($inputCode,22,2) );
$crypt .= chr( substr($inputCode,24,2) );
$crypt .= chr( substr($inputCode,26,2) );
这给了我使用与该类标准相同的代码创建条形码所需的确切信息:
$check = ord($crypt[0]); // calcul de la somme de contrôle
for ($i=0; $i<strlen($crypt); $i++)
$check += (ord($crypt[$i]) * $i);
$check %= 103;
$crypt .= chr($check) . chr(106) . chr(107); // Chaine cryptée complète
$i = (strlen($crypt) * 11) - 8; // calcul de la largeur du module
$modul = $w/$i;
for ($i=0; $i<strlen($crypt); $i++) // BOUCLE D'IMPRESSION
$c = $this->T128[ord($crypt[$i])];
for ($j=0; $j<count($c); $j++)
$this->Rect($x,$y,$c[$j]*$modul,$h,"F");
$x += ($c[$j++]+$c[$j])*$modul;
【讨论】:
以上是关于使用允许我手动指定子集的 FPDF 创建 code128 条形码的主要内容,如果未能解决你的问题,请参考以下文章