资源简介
交叉25是一种高密度的,可以嵌入任意数量数字的,长度可变化的数字条形码。
该代码使用C语言编写,编码的数据个数为奇数时,自动添加校验。
代码片段和文件信息
/*
* ITF25_Barcode.c
*
* Created on: 2012-10-25
* Author: guximiao
*/
#include
#include “ITF25_Barcode.h“
#define NARROW 0
#define WIDE 1
const static unsigned char encoding_table[10][5] =
{
NARROWNARROWWIDEWIDENARROW // 0
WIDENARROWNARROWNARROWWIDE // 1
NARROWWIDENARROWNARROWWIDE // 2
WIDEWIDENARROWNARROWNARROW // 3
NARROWNARROWWIDENARROWWIDE // 4
WIDENARROWWIDENARROWNARROW // 5
NARROWWIDEWIDENARROWNARROW // 6
NARROWNARROWNARROWWIDEWIDE // 7
WIDENARROWNARROWWIDENARROW // 8
NARROWWIDENARROWWIDENARROW // 9
};
static unsigned int CalcCheckSum(const unsigned char buf[] unsigned int len);
static unsigned int EncodingSymbol(unsigned char barcode[] unsigned char odd unsigned char even);
int ITF25_Encoding(unsigned char barcode[] const char *str)
{
unsigned char checksum odd even;
unsigned int len;
int bar_len = 0;
len = strlen(str);
if((len&0x01) == 1)
{checksum = CalcCheckSum((unsigned char *)str len);}
// 起始符 1010
barcode[bar_len++] = 0xff;
barcode[bar_len++] = 0;
barcode[bar_len++] = 0xff;
barcode[bar_len++] = 0;
// 编码
while(len>1)
{
odd = *str++ - ‘0‘;
even = *str++ - ‘0‘;
bar_len += EncodingSymbol(&barcode[bar_len] odd even);
len -= 2;
}
if(len == 1)
{bar_len += EncodingSymbol(&barcode[bar_len] *str - ‘0‘ checksum);}
// 停止符 1101
barcode[bar_len++] = 0xff;
barcode[bar_len++] = 0xff;
barcode[bar_len++] = 0;
barcode[bar_len++] = 0xff;
return bar_len;
}
static unsigned int EncodingSymbol(unsigned char barcode[] unsigned char odd unsigned char even)
{
unsigned int ilen;
for(i=0len=0; i<5; i++)
{
if(encoding_table[odd][i] == NARROW)
{barcode[len++] = 0xff;}
else
{
barcode[len++] = 0xff;
barcode[len++] = 0xff;
}
if(encoding_table[even][i] == NARROW)
{barcode[len++] = 0;}
else
{
barcode[len++] = 0;
barcode[len++] = 0;
}
}
return len;
}
static unsigned int CalcCheckSum(const unsigned char buf[] unsigned int len)
{
unsigned int checkSum;
unsigned int ipoddeven;
p = 0;
for(i=lenodd=0even=0; i>0;)
{
if(p)
{even += buf[--i] - ‘0‘;}
else
{odd += buf[--i] - ‘0‘;}
p = !p;
}
odd = odd * (unsigned int)3;
checkSum = odd + even;
checkSum %= (unsigned int)10;
if(checkSum)
{checkSum = 10 - checkSum;}
return checkSum;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2474 2013-03-02 17:14 ITF25_Barcode\ITF25_Barcode.c
文件 225 2013-03-02 17:13 ITF25_Barcode\ITF25_Barcode.h
目录 0 2013-03-02 17:14 ITF25_Barcode
----------- --------- ---------- ----- ----
2699 3
评论
共有 条评论