EAN-13 条码生成

来源:互联网 发布:新手淘宝客 编辑:程序博客网 时间:2024/05/01 06:26

EAN条码符号标准版
表示13位数字的EAN条码(EAN-13)称为标准版的EAN条码,其结构如图所示:

左侧空白区 起始符 左侧数据符 中间分隔符 右侧数据符 校验符 终止符 右测空白区
9个模块 3个模块 42个模块 5个模块 35个模块 7个模块 3个模块 9个模块

注意:在EAN码中一个模块的宽度为0.33mm。

EAN码的编码规则

数字符 左侧数据符 右侧数据符
A B C 


const char *g_EAN_Data[][3]={
{"0001101","0100111","1110010"},//0 0001101 0100111 1110010 
{"0011001","0110011","1100110"},//1 0011001 0110011 1100110 


{"0010011","0011011","1101100"},//2 0010011 0011011 1101100 
{"0111101","0100001","1000010"},//3 0111101 0100001 1000010 


{"0100011","0011101","1011100"},//4 0100011 0011101 1011100 
{"0110001","0111001","1001110"},//5 0110001 0111001 1001110 


{"0101111","0000101","1010000"},//6 0101111 0000101 1010000 
{"0111011","0010001","1000100"},//7 0111011 0010001 1000100 


{"0110111","0001001","1001000"},//8 0110111 0001001 1001000 
{"0001011","0010111","1110100"},//9 0001011 0010111 1110100
};

起始符:101

中间分隔符:01010

终止符:101。

A、B、C中的“0”和“1”分别表示具有一个模块宽度的“空”和“条”。

因为左侧数据编码方式有两种,要按照前置码选其中一种,如表:

前置字符 左侧数据符编码规则的选择
0 A A A A A A
1 A A B A B B
2 A A B B A B
3 A A B B B A
4 A B A A B B
5 A B B A A B
6(中国) A B B B A A
7 A B A B A B
8 A B A B B A
9 A B B A B A

EAN标准码的尺寸

宽:全部37.29mm 条码31.35mm

长:数据符条码22.85/23.18mm 起始符/分隔符/终止符24.50mm 全部26.26mm

放大倍数:0.8 ----- 2

2 . EAN条码符号缩短版

表示8位数字的EAN条码(EAN-8)称为缩短版EAN条码,其结构如表所示左侧空白区 起始符 左侧数据符 中间分隔符 右侧数据符 校验符 终止符 右侧空白区
7个模块 3个模块 28个模块 5个模块 21个模块 7个模块 3个模块 7个模块

EAN缩短码的尺寸

宽:条码22.11mm 全部26.73

长:数据符条码18.23/18.56mm 起始符/分隔符/终止符19.88mm 全部21.64mm

放大倍数:0.8 --- 2.0

总结:在EAN标准版中,前置符不用条码符表示。在缩短版中前置符包括在左侧数据符中,用条码符表

示并且左侧数据符均用A组编码规则,右侧数据符均用B组编码规则。

3 . EAN码校验位的计算方法

标准版和缩短版的校验码计算方法相同。

从代码位置序号2开始,所有偶数位的数字代码求和为a。
将上步中的a乘以3为a。
从代码位置序号3开始,所有奇数位的数字代码求和为b。
将a和b相加为c。
取c的个位数d。
用10减去d即为校验位数值。
例:234235654652的校验码的计算如下表:

数据码 校验码

代码位置序号 13 12 11 10 9 8 7 6 5 4 3 2 1
数字码 2 3 4 2 3 5 6 5 4 6 5 2 ?
偶数位   3 + 2 + 5 + 5 + 6 + 2  
奇数位 2 + 4 + 3 + 5 + 4 + 5    

步骤1:3+2+5+5+6+2=23

步骤2:23*3=69

步骤3:2+4+3+5+4+5=23

步骤4:69+23=92

步骤5:10-2=8

步骤6:校验码为 8 


C 代码如下:






const char *g_EAN_Data[][3]={
{"0001101","0100111","1110010"},//0 0001101 0100111 1110010 
{"0011001","0110011","1100110"},//1 0011001 0110011 1100110 


{"0010011","0011011","1101100"},//2 0010011 0011011 1101100 
{"0111101","0100001","1000010"},//3 0111101 0100001 1000010 


{"0100011","0011101","1011100"},//4 0100011 0011101 1011100 
{"0110001","0111001","1001110"},//5 0110001 0111001 1001110 


{"0101111","0000101","1010000"},//6 0101111 0000101 1010000 
{"0111011","0010001","1000100"},//7 0111011 0010001 1000100 


{"0110111","0001001","1001000"},//8 0110111 0001001 1001000 
{"0001011","0010111","1110100"},//9 0001011 0010111 1110100
};


const char g_EAN_Index[][6] =
{
{0,0,0,0,0,0,},//0 A A A A A A 
{0,0,1,0,1,1,},//1 A A B A B B 
{0,0,1,1,0,1,},//2 A A B B A B 
{0,0,1,1,1,0,},//3 A A B B B A 
{0,1,0,0,1,1,},//4 A B A A B B 
{0,1,1,0,0,1,},//5 A B B A A B 
{0,1,1,1,0,0,},//6(中国) A B B B A A 
{0,1,0,1,0,1,},//7 A B A B A B 
{0,1,0,1,1,0,},//8 A B A B B A 
{0,1,1,0,1,0,},//9 A B B A B A 
};


void Barcode_StringAddBar(unsigned char *pData,const char *pString,int len,int *pPostion,int BarWide)
{
int loop,i;
for(loop=0;loop<len;loop++)
{
if(pString[loop] == '1')
{
for(i=0;i<BarWide;i++)
{
SetPointData(pData,*pPostion,1);
(*pPostion) ++ ; 
}
}
else
{
(*pPostion) += BarWide ;
}
}
}




int PrinterBarCode_EAN_13(char *Code,int BarWide,int PrintHigh)
{
int len,loop,i,j ;
unsigned char buf[100];
int nODD=0;
int nEVEN=0;
int nPostion=0;
int midPoint;
int endPoint;
int StartPoint;
int FontStart1;
int FontStart2;
u8 CheckNum;


// RETAILMSG(1,(TEXT("[PRT]  ++ PrinterBarCode_EAN_13 !!!\r\n")));
// RETAILMSG(1,(TEXT("[PRT]  code = %s\r\n"),Code));




memset(buf,0,sizeof(buf));
len = strlen(Code);
if(len > 13 || len < 12 )
{
return -4;
}
for(loop=0; loop<12;loop+=2)
{
if(Code[loop] >= '0' && Code[loop] <= '9')
{
nODD += (Code[loop] - '0');
}
else
{
return -5;
}
}


for(loop=1; loop<12;loop+=2)
{
if(Code[loop] >= '0' && Code[loop] <= '9')
{
nEVEN += (Code[loop] - '0');
}
else
{
return -5;
}
}

len = nODD + nEVEN * 3 ;
len %= 10 ;
len = 10 - len ;
len %= 10 ;
CheckNum = len + '0' ;

// RETAILMSG(1,(TEXT("[PRT]  Check = %d\r\n"),len));


nODD = Code[0] - '0' ;
//起始位
Barcode_StringAddBar(buf,"101",3,&nPostion,BarWide);
for(loop=1;loop<=6;loop++)
{
nEVEN = g_EAN_Index[nODD][loop-1];
i = Code[loop] - '0' ;
Barcode_StringAddBar(buf,g_EAN_Data[i][nEVEN],7,&nPostion,BarWide);
}
//分隔符
midPoint = nPostion ;
Barcode_StringAddBar(buf,"01010",5,&nPostion,BarWide);


for(loop=7;loop<=11;loop++)
{
//nEVEN = 2 ;
i = Code[loop] - '0' ;
Barcode_StringAddBar(buf,g_EAN_Data[i][2],7,&nPostion,BarWide);
}
//校验位
Barcode_StringAddBar(buf,g_EAN_Data[len][2],7,&nPostion,BarWide);
//结束位
endPoint = nPostion ;
Barcode_StringAddBar(buf,"101",3,&nPostion,BarWide);


StartPoint = 384 - nPostion ;
if(StartPoint < 0)
{
return -6 ;
}
else
{
StartPoint /= 8 ;
}
StartPoint /= 2 ;

memset(g_PrinterBuffer,0x00,48);
memcpy(g_PrinterBuffer+StartPoint,buf,48);
for(loop=1;loop<PrintHigh;loop++)
{
memcpy(g_PrinterBuffer + loop*48 , g_PrinterBuffer , 48);
}
g_SendPrintDataSize = 48*PrintHigh ;

p_LineData = (LineData *)(g_PrinterBuffer + g_SendPrintDataSize);
memset(p_LineData,0x00,48*24);



StartPoint *= 8 ;
midPoint += StartPoint ;
endPoint += StartPoint ;

for(loop=0;loop<12;loop++)
{
nPostion = StartPoint;
Barcode_StringAddBar(g_PrinterBuffer +g_SendPrintDataSize +  loop*48 , "101" ,3 ,&nPostion,BarWide);
FontStart1 = nPostion ;

nPostion = midPoint;
Barcode_StringAddBar(g_PrinterBuffer +g_SendPrintDataSize +  loop*48 , "01010" , 5,&nPostion,BarWide);
FontStart2 = nPostion ;

nPostion = endPoint;
Barcode_StringAddBar(g_PrinterBuffer +g_SendPrintDataSize +  loop*48 , "101" , 3,&nPostion,BarWide);
}


RETAILMSG(1,(TEXT("[PRT]  StartPoint = %d\r\n"),StartPoint));
//第一位
if(StartPoint >= 14 )
{
nPostion += AddDot_LineBuf(Code[0],StartPoint-14,0);
}
else
{
nPostion += AddDot_LineBuf(Code[0],0,0);
}
//前6字节
len = midPoint - FontStart1 ;
len /= 6 ;
if(len >= 12 )
{
for(loop=1;loop<= 6 ; loop++)
{
nPostion = FontStart1 + len*(loop - 1);
nPostion += (len-12)/2 ;
nPostion += AddDot_LineBuf(Code[loop],nPostion,0);
}
}



//后6字节
len = endPoint - FontStart2 ;
len /= 6 ;
if(len >= 12 )
{
for(loop=0;loop< 6 ; loop++)
{
nPostion = FontStart2 + len*(loop);
nPostion += (len-12)/2 ;
if(loop == 5)
{
nPostion += AddDot_LineBuf(CheckNum,nPostion,0);
}
else
{
nPostion += AddDot_LineBuf(Code[loop+7],nPostion,0);
}
}
}
g_SendPrintDataSize += (48*24) ;




return g_SendPrintDataSize ;


}


原创粉丝点击