CODE128(CPP)

#include "Code128.h"

String sIDList[107] = { //字符集ID表
212222, 222122, 222221, 121223, 121322, 131222, 122213, 122312,
132212, 221213, 221312, 231212, 112232, 122132, 122231, 113222,
123122, 123221, 223211, 221132, 221231, 213212, 223112, 312131,
311222, 321122, 321221, 312212, 322112, 322211, 212123, 212321,
232121, 111323, 131123, 131321, 112313, 132113, 132311, 211313,
231113, 231311, 112133, 112331, 132131, 113123, 113321, 133121,
313121, 211331, 231131, 213113, 213311, 213131, 311123, 311321,
331121, 312113, 312311, 332111, 314111, 221411, 431111, 111224,
111422, 121124, 121421, 141122, 141221, 112214, 112412, 122114,
122411, 142112, 142211, 241211, 221114, 413111, 241112, 134111,
111242, 121142, 121241, 114212, 124112, 124211, 411212, 421112,
421211, 212141, 214121, 412121, 111143, 111341, 131141, 114113,
114311, 411113, 411311, 113141, 114131, 311141, 411131, 211412,
211214, 211232, 2331112
};

void code128(String sContent, TImage* image, int height,
int iWidthLevel, bool bIfText)
{
int Pos[2] = {STARTPOS_X, STARTPOS_Y};
int iCodeID[MAX_CHA] = {0};
int i = 0;
int ImageWidth, ImageHeight;

CodeToID(sContent, iCodeID);
ImageWidth = GetBarcodeLen(iCodeID, iWidthLevel) + 20;
if(bIfText){
image->Height = height + 5;
image->Width = ImageWidth;
image->Picture->Bitmap->Height = height + 5;
image->Picture->Bitmap->Width = ImageWidth;
height -= 14; //14:单个字符的高度
Pos[1] = height;
AddText(&sContent, image, Pos, iCodeID, iWidthLevel);
}
else{
image->Height = height;
image->Picture->Bitmap->Height = height;
image->Width = ImageWidth;
image->Picture->Bitmap->Width = ImageWidth;
}
while(1){
if(iCodeID[0] != 0){
if(iCodeID[i] != 999){
DrawCode(iCodeID[i], image, Pos, height, iWidthLevel);
i++;
}
else{
break;
}
}
else break;
}
}

void WhiteLine(TImage* image, int* iPos, int height, int width)
{
while(width > 0){
image->Canvas->MoveTo(*iPos, *(iPos + 1));
image->Canvas->Pen->Color = clWhite;
image->Canvas->Pen->Width = 1;
image->Canvas->LineTo(*iPos, *(iPos + 1) + height);
width--;
(*iPos)++;
}
}

void BlackLine(TImage* image, int* iPos, int height, int width)
{
while(width > 0){
image->Canvas->MoveTo(*iPos, *(iPos + 1));
image->Canvas->Pen->Color = clBlack;

image->Canvas->Pen->Width = 1;
image->Canvas->LineTo(*iPos, *(iPos + 1) + height);
width--;
(*iPos)++;
}
}

void DrawCode(int iIDNO, TImage* image, int* iPos, int height, int iWidthLevel)
{
int iLineWidth;
String sLineCol = "black";
if(sIDList[iIDNO].Length() == 7){ //画7位停止位
BlackLine(image, iPos, height, 2 * iWidthLevel);
WhiteLine(image, iPos, height, 3 * iWidthLevel);
BlackLine(image, iPos, height, 3 * iWidthLevel);
WhiteLine(image, iPos, height, 1 * iWidthLevel);
BlackLine(image, iPos, height, 1 * iWidthLevel);
WhiteLine(image, iPos, height, 1 * iWidthLevel);
BlackLine(image, iPos, height, 2 * iWidthLevel);
}
else if(sIDList[iIDNO].Length() == 6){ //画6位去其他位
for(int i = 1; i <= 6; i++){
TryStrToInt(sIDList[iIDNO].SubString(i, 1), iLineWidth);
if(sLineCol == "black"){
BlackLine(image, iPos, height, iLineWidth * iWidthLevel);
sLineCol = "white";
}
else if(sLineCol == "white"){
WhiteLine(image, iPos, height, iLineWidth * iWidthLevel);
sLineCol = "black";
}
}
}
}

void CheckAndStopID(int* iCodeID)
{
int iCheckBitSUM = *iCodeID;
iCodeID++;
int i = 1;
while(1){
if(*iCodeID != 999){
iCheckBitSUM += *iCodeID * i;
i++;
iCodeID++;
}
else break;
}
*iCodeID = iCheckBitSUM % 103; //计算并写入校验位ID值
iCodeID++;
*iCodeID = STOP;
iCodeID++;
*iCodeID = 999; //编码结束
}

void CodeToID(String sContent, int* iCodeID)
{
if(sContent == "") return;
int iDataLength, i;
int iChaASIC2 = 0;
char CurrentMode = '0';
int* iCodeIDOrig;
iCodeIDOrig = iCodeID;
iDataLength = sContent.Length();
i = 1;
while(i < iDataLength + 1){
if(CurrentMode == '0'){
int* iCheckMode = new int;
*iCheckMode = CheckMode(sContent, i, 4);
if(*iCheckMode == CODE_A){

}
else if(*iCheckMode == CODE_B){
*iCodeID = CODE_B;
iCodeID++;
iChaASIC2 = *sContent.SubString(i, 1).c_str();
*iCodeID = iChaASIC2 - 32;
iCodeID++;
i++;
CurrentMode = 'B';
}
else if(*iCheckMode == CODE_C){
*iCodeID = CODE_C;
iCodeID++;
TryStrToInt(sContent.SubString(i, 2), *iCodeID);
iCodeID++;
i += 2;
TryStrToInt(sContent.SubString(i, 2), *iCodeID);
iCodeID++;
i += 2;
CurrentMode = 'C';
}
delete iCheckMode;
}
else if(Curre

ntMode == 'B'){
int* iCheckMode = new int;
*iCheckMode = CheckMode(sContent, i, 4);
if(*iCheckMode == CODE_B){
iChaASIC2 = *sContent.SubString(i, 1).c_str();
*iCodeID = iChaASIC2 - 32;
iCodeID++;
i++;
CurrentMode = 'B';
}
else if(*iCheckMode == CODE_C){
*iCodeID = CODE_C;
iCodeID++;
TryStrToInt(sContent.SubString(i, 2), *iCodeID);
iCodeID++;
i += 2;
TryStrToInt(sContent.SubString(i, 2), *iCodeID);
iCodeID++;
i += 2;
CurrentMode = 'C';
}
else{

}
delete iCheckMode;
}
else if(CurrentMode == 'C'){
int* iCheckMode = new int;
*iCheckMode = CheckMode(sContent, i, 2);
if(*iCheckMode == CODE_B){
*iCodeID = CODE_B;
iCodeID++;
iChaASIC2 = *sContent.SubString(i, 1).c_str();
*iCodeID = iChaASIC2 - 32;
iCodeID++;
i++;
CurrentMode = 'B';
}
else if(*iCheckMode == CODE_C){
TryStrToInt(sContent.SubString(i, 2), *iCodeID);
iCodeID++;
i += 2;
CurrentMode = 'C';
}
delete iCheckMode;
}
}
*iCodeID = 999;
switch(*iCodeIDOrig){
case CODE_C : *iCodeIDOrig = START_C; break;
case CODE_B : *iCodeIDOrig = START_B; break;
case CODE_A : *iCodeIDOrig = START_A; break;
}
CheckAndStopID(iCodeIDOrig);
}

int CheckMode(String s, int begain, int count)
{
int iDataLength;
iDataLength = s.Length();
if((begain + count) <= (iDataLength + 1)){
for(int i = begain; i < count + begain; i++){
if(*s.SubString(i, 1).c_str() > '9' || *s.SubString(i, 1).c_str() < '0'){
if(*s.SubString(i, 1).c_str() < ' '){
return CODE_A;
}
return CODE_B;
}
}
return CODE_C;
}
else{
return CODE_B;
}
}

void AddText(String* sText, TImage* image, int* iPos, int* iCodeID, int iWidthLevel)
{
int iBarcodeLen, iCharLen, iContenLen;
image->Canvas->Font->Size = 14;
iBarcodeLen = GetBarcodeLen(iCodeID, iWidthLevel);
iCharLen = image->Canvas->TextWidth((*sText).SubString(1, 1));
iBarcodeLen -= iCharLen;
if((*sText).Length() == 1){
iContenLen = 0;
}
else{
iContenLen = iBarcodeLen / ((*sText).Length() - 1);
}
for(int i = 1; i <= (*sText).Length(); i++){
image->Canvas->TextOutA(*iPos, *(iPos + 1), (*sText).SubString(i, 1));
*iPos += iContenLen;
}
/将画笔起

始位置定位于(10,10)/
*iPos = STARTPOS_X;
*(iPos + 1) = STARTPOS_Y;
}

int GetBarcodeLen(int* iCodeID, int iWidthLevel)
{
if(*iCodeID != 0){
int iBarcodeLen = 0;
while(1){
if(*iCodeID != 999){
if(*iCodeID != 106){
iBarcodeLen += 11 * iWidthLevel;
}
else{
iBarcodeLen += 13 * iWidthLevel;
}
}
else break;
iCodeID++;
}
return iBarcodeLen;
}
else return 0;
}

相关文档
最新文档