Code128Auto就是完全遵循GBT 18347-2001 128条码 上面的最小长度原则 编写的条形码
///
/// Code128Auto条形码,遵循长度最小原则
///
public class Code128Auto : absCode128
{
public Code128Auto(string rawData)
: base(rawData)
{
}
protected override bool RawDataCheck()
{
//Code128对应的ASCII码范围是0~127
foreach (char c in this._rawData)
{
if ((byte)c > 127)
{
return false;
}
}
return true;
}
protected override string GetEncodedData()
{
StringBuilder tempBuilder = new StringBuilder();
CharacterSet nowCharacterSet = Code128.GetCharacterSet(this._rawData, 0);
int checkNum;//校验字符
switch (nowCharacterSet)
{
case CharacterSet.A:
tempBuilder.Append(Code128.BSList[Code128.StartA]);//加上起始符StartA
checkNum = Code128.StartA;
break;
case CharacterSet.B:
tempBuilder.Append(Code128.BSList[Code128.StartB]);//加上起始符StartB
checkNum = Code128.StartB;
break;
default:
tempBuilder.Append(Code128.BSList[Code128.StartC]);//加上起始符StartC
checkNum = Code128.StartC;
break;
}
int nowWeight = 1, nowIndex = 0;
this.GetEncodedData(tempBuilder, nowCharacterSet, ref nowIndex, ref nowWeight, ref checkNum);
checkNum %= 103;
tempBuilder.Append(Code128.BSList[checkNum]);//加上校验符
tempBuilder.Append(Code128.BSList[Code128.Stop]);//加上结束符
return tempBuilder.ToString();
}
///
/// 通用方法
///
///
///
///
///
private void EncodingCommon(StringBuilder tempBuilder, byte sIndex, ref int nowWeight, ref int checkNum)
{
tempBuilder.Append(Code128.BSList[sIndex]);
checkNum += nowWeight * sIndex;
nowWeight++;
}
///
/// 获取编码后的数据
///
/// 编码数据容器
/// 当前字符集
/// 字符串索引
/// 当前权值
/// 当前检验值总和
private void GetEncodedData(StringBuilder tempBuilder, CharacterSet nowCharacterSet,ref int i, ref int nowWeight, ref int checkNum)
{//因为可能存在字符集C,所以i与nowWeight可能存在不一致关系,所以要分别定义
byte sIndex;
switch (nowCharacterSet)
{
case CharacterSet.A:
case CharacterSet.B:
for (; i < this._rawData.Length; i++)
{
if (char.IsDigit(this._rawData[i]))
{
//数字
int digitLength = Code128.GetDigitLength(this._rawData, i);
if (digitLength >= 4)
{
//转入CodeC
if (digitLength % 2 != 0)
{//奇数位数字,在第一个数字之后插入CodeC字符
sIndex = Code128.GetSIndex(nowCharacterSet, (this._rawData[i]));
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
i++;
}
nowCharacterSet = CharacterSet.C;
sIndex = Code128.GetCodeXIndex(nowCharacterSet);//插入CodeC切换字符
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
return;
}
else
{
//如果小于4位数字,则直接内部循环结束
for (int j = 0; j < digitLength; j++)
{
sIndex = Code128.GetSIndex(nowCharacterSet, (this._rawData[i]));
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
i++;
}
i--;//因为上面循环结束后继续外部循环会导致i多加了1,所以要减去1
continue;
}
}
else if (Code128.CharNotBelongsTo(nowCharacterSet, this._rawData[i]))
{//当前字符不属于目前的字符集
byte tempByte = Code128.GetProprietaryChar(this._rawData, i + 1);//获取当前字符后第一个属于A,或B的字符集
CharacterSet tempCharacterSet = Code128.GetShiftCharacterSet(nowCharacterSet);
if (tempByte != byte.MaxValue && Code128.CharOnlyBelongsTo(nowCharacterSet, (char)tempByte))
{
//加入转换符
sIndex = Code128.SHIFT_AB;
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
sIndex = Code128.GetSIndex(tempCharacterSet, this._rawData[i]);
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
continue;
}
else
{
//加入切换符
nowCharacterSet = tempCharacterSet;
sIndex = Code128.GetCodeXIndex(nowCharacterSet);
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
return;
}
}
else
{
sIndex = Code128.GetSIndex(nowCharacterSet, this._rawData[i]);
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
}
}
break;
default:
for (; i < this._rawData.Length; i += 2)
{
if (i != this._rawData.Length - 1 && char.IsDigit(this._rawData, i) && char.IsDigit(this._rawData, i + 1))
{
sIndex = byte.Parse(this._rawData.Substring(i, 2));
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
}
else
{
nowCharacterSet = Code128.GetCharacterSet(this._rawData, i);
//插入转换字符
sIndex = Code128.GetCodeXIndex(nowCharacterSet);
this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
this.GetEncodedData(tempBuilder, nowCharacterSet, ref i, ref nowWeight, ref checkNum);
return;
}
}
break;
}
}
#region 注销
//private void GetEncodedData(StringBuilder tempBuilder, string data, CharacterSet nowCharacterSet, ref int nowWeight, ref int checkNum)
//{
// byte sIndex, nowChar;
// switch (nowCharacterSet)
// {
// case CharacterSet.A:
// for (int i = 0; i < data.Length; i++)
// {
// nowChar = (byte)data[i];
// if (nowChar > 95)
// {
// //字符集B
// byte tempByte = Code128.GetProprietaryChar(data, i + 1);
// if (tempByte < 32)
// {
// //加入转换符
// sIndex = Code128.SHIFT_AB;
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// sIndex = Code128.GetSIndexFromB(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// continue;
// }
// else
// {
// //加入切换符
// sIndex = Code128.CODEB_AC;
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.B, ref nowWeight, ref checkNum);
// return;
// }
// }
// else if (char.IsDigit(data[i]))
// {
// //数字
// int digitLength = Code128.GetDigitLength(data, i);
// if (digitLength >= 4)
// {
// //转入CodeC
// if (digitLength % 2 != 0)
// {//奇数位数字,在第一个数字之后插入CodeC字符
// sIndex = Code128.GetSIndexFromA(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// i++;
// }
// sIndex = Code128.CODEC_AB;//插入CodeC切换字符
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.C, ref nowWeight, ref checkNum);
// return;
// }
// else
// {
// for (int j = 0; j < digitLength; j++)
// {
// sIndex = Code128.GetSIndexFromA(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);i++;
// }i--;
// continue;
// }
// }
// sIndex = Code128.GetSIndexFromA(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// }
// break;
// case CharacterSet.B:
// for (int i = 0; i < data.Length; i++)
// {
// nowChar = (byte)data[i];
// if (nowChar < 32)
// {
// //字符集A
// byte tempByte = Code128.GetProprietaryChar(data, i + 1);
// if (tempByte > 95)
// {
// //加入转换符
// sIndex = Code128.SHIFT_AB;
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// sIndex = Code128.GetSIndexFromA(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// continue;
// }
// else
// {
// //加入切换符
// sIndex = Code128.CODEA_BC;
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.B, ref nowWeight, ref checkNum);
// return;
// }
// }
// else if (char.IsDigit(data[i]))
// {
// //数字
// int digitLength = Code128.GetDigitLength(data, i);
// if (digitLength >= 4)
// {
// //转入CodeC
// if (digitLength % 2 != 0)
// {//奇数位数字,在第一个数字之后插入CodeC字符
// sIndex = Code128.GetSIndexFromB(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// i++;
// }
// sIndex = Code128.CODEC_AB;//插入CodeC切换字符
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// this.GetEncodedData(tempBuilder, data.Substring(i), CharacterSet.C, ref nowWeight, ref checkNum);
// return;
// }
// else
// {
// for (int j = 0; j < digitLength; j++)
// {
// sIndex = Code128.GetSIndexFromB(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);i++;
// }i--;
// continue;
// }
// }
// sIndex = Code128.GetSIndexFromB(data[i]);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// }
// break;
// default:
// for (int i = 0; i < data.Length; i += 2)
// {
// if (i != data.Length - 1 && char.IsDigit(data, i) && char.IsDigit(data, i + 1))
// {
// sIndex = byte.Parse(data.Substring(i, 2));
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// }
// else
// {
// nowCharacterSet = Code128.GetCharacterSet(data, i);
// //插入转换字符
// sIndex = Code128.GetCodeXIndex(nowCharacterSet);
// this.EncodingCommon(tempBuilder, sIndex, ref nowWeight, ref checkNum);
// this.GetEncodedData(tempBuilder, data.Substring(i), nowCharacterSet, ref nowWeight, ref checkNum);
// return;
// }
// }
// break;
// }
//}
#endregion
}
注销部分是一开始的获取编码的写法