虽然二维码满天飞,但也不能忘了条形码,本篇介绍可以在C#中使用的1D/2D编码解码器。条形码的应用已经非常普遍,几乎所有超市里面的商品上面都印有条形码;
条形码的标准:
条形码的标准有ENA条形码、UPC条形码、二五条形码、交叉二五条形码、库德巴条形码、三九条形码和128条形码等,而商品上最常使用的就是EAN商品条形码。EAN商品条形码亦称通用商品条形码,由国际物品编码协会制定,通用于世界各地,是目前国际上使用最广泛的一种商品条形码。我国目前在国内推行使用的也是这种商品条形码。EAN商品条形码分为EAN-13(标准版)和EAN-8(缩短版)两种。以下是C#生成条形码的类库:
public class BarCodeHelper
{
public class Code128
{
private readonly DataTable _mCode128 = new DataTable();
private uint _mHeight = ; /// <summary>
/// 高度
/// </summary>
public uint Height { get { return _mHeight; } set { _mHeight = value; } }
private Font _mValueFont; /// <summary>
/// 是否显示可见号码 如果为NULL不显示号码
/// </summary>
public Font ValueFont { get { return _mValueFont; } set { _mValueFont = value; } }
private byte _mMagnify; /// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get { return _mMagnify; } set { _mMagnify = value; } } /// <summary>
/// 条码类别
/// </summary>
public enum Encode
{
Code128A,
Code128B,
Code128C,
Ean128
} public Code128()
{
_mCode128.Columns.Add("ID");
_mCode128.Columns.Add("Code128A");
_mCode128.Columns.Add("Code128B");
_mCode128.Columns.Add("Code128C");
_mCode128.Columns.Add("BandCode");
_mCode128.CaseSensitive = true;
#region 数据表
_mCode128.Rows.Add("", " ", " ", "", "");
_mCode128.Rows.Add("", "!", "!", "", "");
_mCode128.Rows.Add("", "\"", "\"", "", "");
_mCode128.Rows.Add("", "#", "#", "", "");
_mCode128.Rows.Add("", "$", "$", "", "");
_mCode128.Rows.Add("", "%", "%", "", "");
_mCode128.Rows.Add("", "&", "&", "", "");
_mCode128.Rows.Add("", "'", "'", "", "");
_mCode128.Rows.Add("", "(", "(", "", "");
_mCode128.Rows.Add("", ")", ")", "", "");
_mCode128.Rows.Add("", "*", "*", "", "");
_mCode128.Rows.Add("", "+", "+", "", "");
_mCode128.Rows.Add("", ",", ",", "", "");
_mCode128.Rows.Add("", "-", "-", "", "");
_mCode128.Rows.Add("", ".", ".", "", "");
_mCode128.Rows.Add("", "/", "/", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", "", "", "", "");
_mCode128.Rows.Add("", ":", ":", "", "");
_mCode128.Rows.Add("", ";", ";", "", "");
_mCode128.Rows.Add("", "<", "<", "", "");
_mCode128.Rows.Add("", "=", "=", "", "");
_mCode128.Rows.Add("", ">", ">", "", "");
_mCode128.Rows.Add("", "?", "?", "", "");
_mCode128.Rows.Add("", "@", "@", "", "");
_mCode128.Rows.Add("", "A", "A", "", "");
_mCode128.Rows.Add("", "B", "B", "", "");
_mCode128.Rows.Add("", "C", "C", "", "");
_mCode128.Rows.Add("", "D", "D", "", "");
_mCode128.Rows.Add("", "E", "E", "", "");
_mCode128.Rows.Add("", "F", "F", "", "");
_mCode128.Rows.Add("", "G", "G", "", "");
_mCode128.Rows.Add("", "H", "H", "", "");
_mCode128.Rows.Add("", "I", "I", "", "");
_mCode128.Rows.Add("", "J", "J", "", "");
_mCode128.Rows.Add("", "K", "K", "", "");
_mCode128.Rows.Add("", "L", "L", "", "");
_mCode128.Rows.Add("", "M", "M", "", "");
_mCode128.Rows.Add("", "N", "N", "", "");
_mCode128.Rows.Add("", "O", "O", "", "");
_mCode128.Rows.Add("", "P", "P", "", "");
_mCode128.Rows.Add("", "Q", "Q", "", "");
_mCode128.Rows.Add("", "R", "R", "", "");
_mCode128.Rows.Add("", "S", "S", "", "");
_mCode128.Rows.Add("", "T", "T", "", "");
_mCode128.Rows.Add("", "U", "U", "", "");
_mCode128.Rows.Add("", "V", "V", "", "");
_mCode128.Rows.Add("", "W", "W", "", "");
_mCode128.Rows.Add("", "X", "X", "", "");
_mCode128.Rows.Add("", "Y", "Y", "", "");
_mCode128.Rows.Add("", "Z", "Z", "", "");
_mCode128.Rows.Add("", "[", "[", "", "");
_mCode128.Rows.Add("", "\\", "\\", "", "");
_mCode128.Rows.Add("", "]", "]", "", "");
_mCode128.Rows.Add("", "^", "^", "", "");
_mCode128.Rows.Add("", "_", "_", "", "");
_mCode128.Rows.Add("", "NUL", "`", "", "");
_mCode128.Rows.Add("", "SOH", "a", "", "");
_mCode128.Rows.Add("", "STX", "b", "", "");
_mCode128.Rows.Add("", "ETX", "c", "", "");
_mCode128.Rows.Add("", "EOT", "d", "", "");
_mCode128.Rows.Add("", "ENQ", "e", "", "");
_mCode128.Rows.Add("", "ACK", "f", "", "");
_mCode128.Rows.Add("", "BEL", "g", "", "");
_mCode128.Rows.Add("", "BS", "h", "", "");
_mCode128.Rows.Add("", "HT", "i", "", "");
_mCode128.Rows.Add("", "LF", "j", "", "");
_mCode128.Rows.Add("", "VT", "k", "", "");
_mCode128.Rows.Add("", "FF", "I", "", "");
_mCode128.Rows.Add("", "CR", "m", "", "");
_mCode128.Rows.Add("", "SO", "n", "", "");
_mCode128.Rows.Add("", "SI", "o", "", "");
_mCode128.Rows.Add("", "DLE", "p", "", "");
_mCode128.Rows.Add("", "DC1", "q", "", "");
_mCode128.Rows.Add("", "DC2", "r", "", "");
_mCode128.Rows.Add("", "DC3", "s", "", "");
_mCode128.Rows.Add("", "DC4", "t", "", "");
_mCode128.Rows.Add("", "NAK", "u", "", "");
_mCode128.Rows.Add("", "SYN", "v", "", "");
_mCode128.Rows.Add("", "ETB", "w", "", "");
_mCode128.Rows.Add("", "CAN", "x", "", "");
_mCode128.Rows.Add("", "EM", "y", "", "");
_mCode128.Rows.Add("", "SUB", "z", "", "");
_mCode128.Rows.Add("", "ESC", "{", "", "");
_mCode128.Rows.Add("", "FS", "|", "", "");
_mCode128.Rows.Add("", "GS", "}", "", "");
_mCode128.Rows.Add("", "RS", "~", "", "");
_mCode128.Rows.Add("", "US", "DEL", "", "");
_mCode128.Rows.Add("", "FNC3", "FNC3", "", "");
_mCode128.Rows.Add("", "FNC2", "FNC2", "", "");
_mCode128.Rows.Add("", "SHIFT", "SHIFT", "", "");
_mCode128.Rows.Add("", "CODEC", "CODEC", "", "");
_mCode128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
_mCode128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
_mCode128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
_mCode128.Rows.Add("", "StartA", "StartA", "StartA", "");
_mCode128.Rows.Add("", "StartB", "StartB", "StartB", "");
_mCode128.Rows.Add("", "StartC", "StartC", "StartC", "");
_mCode128.Rows.Add("", "Stop", "Stop", "Stop", "");
#endregion
} /// <summary>
/// 获取128图形
/// </summary>
/// <param name="pText">文字</param>
/// <param name="pCode">编码</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(string pText, Encode pCode)
{
string viewText = pText;
string text = "";
IList<int> textNumb = new List<int>();
int examine; //首位
switch (pCode)
{
case Encode.Code128C:
examine = ;
if ((pText.Length & ) != ) throw new Exception("128C长度必须是偶数");
while (pText.Length != )
{
var temp = ;
try
{
var codeNumb128 = Int32.Parse(pText.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
text += GetValue(pCode, pText.Substring(, ), ref temp);
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
case Encode.Ean128:
examine = ;
if ((pText.Length & ) != ) throw new Exception("EAN128长度必须是偶数");
textNumb.Add();
text += "";
while (pText.Length != )
{
var temp = ;
try
{
var codeNumb128 = Int32.Parse(pText.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
text += GetValue(Encode.Code128C, pText.Substring(, ), ref temp);
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
default:
examine = pCode == Encode.Code128A ? : ; while (pText.Length != )
{
var temp = ;
var valueCode = GetValue(pCode, pText.Substring(, ), ref temp);
if (valueCode.Length == ) throw new Exception("无效的字符集!" + pText.Substring(, ));
text += valueCode;
textNumb.Add(temp);
pText = pText.Remove(, );
}
break;
}
if (textNumb.Count == ) throw new Exception("错误的编码,无数据");
text = text.Insert(, GetValue(examine)); //获取开始位 for (int i = ; i != textNumb.Count; i++)
{
examine += textNumb[i] * (i + );
}
examine = examine % ; //获得严效位
text += GetValue(examine); //获取严效位
text += ""; //结束位
var codeImage = GetImage(text);
GetViewText(codeImage, viewText);
return codeImage;
} /// <summary>
/// 获取目标对应的数据
/// </summary>
/// <param name="pCode">编码</param>
/// <param name="pValue">数值 A b 30</param>
/// <param name="pSetId">返回编号</param>
/// <returns>编码</returns>
private string GetValue(Encode pCode, string pValue, ref int pSetId)
{
if (_mCode128 == null) return "";
var row = _mCode128.Select(pCode + "='" + pValue + "'");
if (row.Length != ) throw new Exception("错误的编码" + pValue);
pSetId = Int32.Parse(row[]["ID"].ToString());
return row[]["BandCode"].ToString();
} /// <summary>
/// 根据编号获得条纹
/// </summary>
/// <param name="pCodeId"></param>
/// <returns></returns>
private string GetValue(int pCodeId)
{
var row = _mCode128.Select("ID='" + pCodeId + "'");
if (row.Length != ) throw new Exception("验效位的编码错误" + pCodeId);
return row[]["BandCode"].ToString();
} /// <summary>
/// 获得条码图形
/// </summary>
/// <param name="pText">文字</param>
/// <returns>图形</returns>
private Bitmap GetImage(string pText)
{
var value = pText.ToCharArray();
var width = ;
for (int i = ; i != value.Length; i++)
{
width += Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + );
}
var codeImage = new Bitmap(width, (int)_mHeight);
var garphics = Graphics.FromImage(codeImage);
//Pen _Pen;
var lenEx = ;
for (int i = ; i != value.Length; i++)
{
int valueNumb = Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + ); //获取宽和放大系数
garphics.FillRectangle((i & ) != ? Brushes.White : Brushes.Black,
new Rectangle(lenEx, , valueNumb, (int)_mHeight));
//_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
lenEx += valueNumb;
}
garphics.Dispose();
return codeImage;
} /// <summary>
/// 显示可见条码文字 如果小于40 不显示文字
/// </summary>
/// <param name="pBitmap">图形</param>
/// <param name="pViewText"></param>
private void GetViewText(Bitmap pBitmap, string pViewText)
{
if (_mValueFont == null) return; Graphics graphics = Graphics.FromImage(pBitmap);
SizeF drawSize = graphics.MeasureString(pViewText, _mValueFont);
if (drawSize.Height > pBitmap.Height - || drawSize.Width > pBitmap.Width)
{
graphics.Dispose();
return;
} int starY = pBitmap.Height - (int)drawSize.Height;
graphics.FillRectangle(Brushes.White, new Rectangle(, starY, pBitmap.Width, (int)drawSize.Height));
graphics.DrawString(pViewText, _mValueFont, Brushes.Black, , starY);
} //12345678
//(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
//结果为starc +12 +34 +56 +78 +47 +end internal Image GetCodeImage(string p)
{
throw new NotImplementedException();
}
}
}