public class Code128
{
private DataTable m_Code128 = new DataTable();
private uint m_Height = ;
/// <summary>
/// 高度
/// </summary>
public uint Height { get { return m_Height; } set { m_Height = value; } }
private Font m_ValueFont = null;
/// <summary>
/// 是否显示可见号码 如果为NULL不显示号码
/// </summary>
public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
private byte m_Magnify = ;
/// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
/// <summary>
/// 条码类别
/// </summary>
public enum Encode
{
Code128A,
Code128B,
Code128C,
EAN128
}
public Code128()
{
m_Code128.Columns.Add("ID");
m_Code128.Columns.Add("Code128A");
m_Code128.Columns.Add("Code128B");
m_Code128.Columns.Add("Code128C");
m_Code128.Columns.Add("BandCode");
m_Code128.CaseSensitive = true;
#region 数据表
m_Code128.Rows.Add("", " ", " ", "", "");
m_Code128.Rows.Add("", "!", "!", "", "");
m_Code128.Rows.Add("", "\"", "\"", "", "");
m_Code128.Rows.Add("", "#", "#", "", "");
m_Code128.Rows.Add("", "$", "$", "", "");
m_Code128.Rows.Add("", "%", "%", "", "");
m_Code128.Rows.Add("", "&", "&", "", "");
m_Code128.Rows.Add("", "'", "'", "", "");
m_Code128.Rows.Add("", "(", "(", "", "");
m_Code128.Rows.Add("", ")", ")", "", "");
m_Code128.Rows.Add("", "*", "*", "", "");
m_Code128.Rows.Add("", "+", "+", "", "");
m_Code128.Rows.Add("", ",", ",", "", "");
m_Code128.Rows.Add("", "-", "-", "", "");
m_Code128.Rows.Add("", ".", ".", "", "");
m_Code128.Rows.Add("", "/", "/", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", ":", ":", "", "");
m_Code128.Rows.Add("", ";", ";", "", "");
m_Code128.Rows.Add("", "<", "<", "", "");
m_Code128.Rows.Add("", "=", "=", "", "");
m_Code128.Rows.Add("", ">", ">", "", "");
m_Code128.Rows.Add("", "?", "?", "", "");
m_Code128.Rows.Add("", "@", "@", "", "");
m_Code128.Rows.Add("", "A", "A", "", "");
m_Code128.Rows.Add("", "B", "B", "", "");
m_Code128.Rows.Add("", "C", "C", "", "");
m_Code128.Rows.Add("", "D", "D", "", "");
m_Code128.Rows.Add("", "E", "E", "", "");
m_Code128.Rows.Add("", "F", "F", "", "");
m_Code128.Rows.Add("", "G", "G", "", "");
m_Code128.Rows.Add("", "H", "H", "", "");
m_Code128.Rows.Add("", "I", "I", "", "");
m_Code128.Rows.Add("", "J", "J", "", "");
m_Code128.Rows.Add("", "K", "K", "", "");
m_Code128.Rows.Add("", "L", "L", "", "");
m_Code128.Rows.Add("", "M", "M", "", "");
m_Code128.Rows.Add("", "N", "N", "", "");
m_Code128.Rows.Add("", "O", "O", "", "");
m_Code128.Rows.Add("", "P", "P", "", "");
m_Code128.Rows.Add("", "Q", "Q", "", "");
m_Code128.Rows.Add("", "R", "R", "", "");
m_Code128.Rows.Add("", "S", "S", "", "");
m_Code128.Rows.Add("", "T", "T", "", "");
m_Code128.Rows.Add("", "U", "U", "", "");
m_Code128.Rows.Add("", "V", "V", "", "");
m_Code128.Rows.Add("", "W", "W", "", "");
m_Code128.Rows.Add("", "X", "X", "", "");
m_Code128.Rows.Add("", "Y", "Y", "", "");
m_Code128.Rows.Add("", "Z", "Z", "", "");
m_Code128.Rows.Add("", "[", "[", "", "");
m_Code128.Rows.Add("", "\\", "\\", "", "");
m_Code128.Rows.Add("", "]", "]", "", "");
m_Code128.Rows.Add("", "^", "^", "", "");
m_Code128.Rows.Add("", "_", "_", "", "");
m_Code128.Rows.Add("", "NUL", "`", "", "");
m_Code128.Rows.Add("", "SOH", "a", "", "");
m_Code128.Rows.Add("", "STX", "b", "", "");
m_Code128.Rows.Add("", "ETX", "c", "", "");
m_Code128.Rows.Add("", "EOT", "d", "", "");
m_Code128.Rows.Add("", "ENQ", "e", "", "");
m_Code128.Rows.Add("", "ACK", "f", "", "");
m_Code128.Rows.Add("", "BEL", "g", "", "");
m_Code128.Rows.Add("", "BS", "h", "", "");
m_Code128.Rows.Add("", "HT", "i", "", "");
m_Code128.Rows.Add("", "LF", "j", "", "");
m_Code128.Rows.Add("", "VT", "k", "", "");
m_Code128.Rows.Add("", "FF", "I", "", "");
m_Code128.Rows.Add("", "CR", "m", "", "");
m_Code128.Rows.Add("", "SO", "n", "", "");
m_Code128.Rows.Add("", "SI", "o", "", "");
m_Code128.Rows.Add("", "DLE", "p", "", "");
m_Code128.Rows.Add("", "DC1", "q", "", "");
m_Code128.Rows.Add("", "DC2", "r", "", "");
m_Code128.Rows.Add("", "DC3", "s", "", "");
m_Code128.Rows.Add("", "DC4", "t", "", "");
m_Code128.Rows.Add("", "NAK", "u", "", "");
m_Code128.Rows.Add("", "SYN", "v", "", "");
m_Code128.Rows.Add("", "ETB", "w", "", "");
m_Code128.Rows.Add("", "CAN", "x", "", "");
m_Code128.Rows.Add("", "EM", "y", "", "");
m_Code128.Rows.Add("", "SUB", "z", "", "");
m_Code128.Rows.Add("", "ESC", "{", "", "");
m_Code128.Rows.Add("", "FS", "|", "", "");
m_Code128.Rows.Add("", "GS", "}", "", "");
m_Code128.Rows.Add("", "RS", "~", "", "");
m_Code128.Rows.Add("", "US", "DEL", "", "");
m_Code128.Rows.Add("", "FNC3", "FNC3", "", "");
m_Code128.Rows.Add("", "FNC2", "FNC2", "", "");
m_Code128.Rows.Add("", "SHIFT", "SHIFT", "", "");
m_Code128.Rows.Add("", "CODEC", "CODEC", "", "");
m_Code128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
m_Code128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
m_Code128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
m_Code128.Rows.Add("", "StartA", "StartA", "StartA", "");
m_Code128.Rows.Add("", "StartB", "StartB", "StartB", "");
m_Code128.Rows.Add("", "StartC", "StartC", "StartC", "");
m_Code128.Rows.Add("", "Stop", "Stop", "Stop", "");
#endregion
}
/// <summary>
/// 获取128图形
/// </summary>
/// <param name="p_Text">文字</param>
/// <param name="p_Code">编码</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(string p_Text, Encode p_Code)
{
string _ViewText = p_Text;
string _Text = "";
IList<int> _TextNumb = new List<int>();
int _Examine = ; //首位
switch (p_Code)
{
case Encode.Code128C:
_Examine = ;
if (!((p_Text.Length & ) == )) throw new Exception("128C长度必须是偶数");
while (p_Text.Length != )
{
int _Temp = ;
try
{
int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
_Text += GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
_TextNumb.Add(_Temp);
p_Text = p_Text.Remove(, );
}
break;
case Encode.EAN128:
_Examine = ;
if (!((p_Text.Length & ) == )) throw new Exception("EAN128长度必须是偶数");
_TextNumb.Add();
_Text += "";
while (p_Text.Length != )
{
int _Temp = ;
try
{
int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
_Text += GetValue(Encode.Code128C, p_Text.Substring(, ), ref _Temp);
_TextNumb.Add(_Temp);
p_Text = p_Text.Remove(, );
}
break;
default:
if (p_Code == Encode.Code128A)
{
_Examine = ;
}
else
{
_Examine = ;
} while (p_Text.Length != )
{
int _Temp = ;
string _ValueCode = GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
if (_ValueCode.Length == ) throw new Exception("无效的字符集!" + p_Text.Substring(, ).ToString());
_Text += _ValueCode;
_TextNumb.Add(_Temp);
p_Text = p_Text.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 += ""; //结束位
Bitmap _CodeImage = GetImage(_Text);
GetViewText(_CodeImage, _ViewText);
return _CodeImage;
}
/// <summary>
/// 获取目标对应的数据
/// </summary>
/// <param name="p_Code">编码</param>
/// <param name="p_Value">数值 A b 30</param>
/// <param name="p_SetID">返回编号</param>
/// <returns>编码</returns>
private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
{
if (m_Code128 == null) return "";
DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
if (_Row.Length != ) throw new Exception("错误的编码" + p_Value.ToString());
p_SetID = Int32.Parse(_Row[]["ID"].ToString());
return _Row[]["BandCode"].ToString();
}
/// <summary>
/// 根据编号获得条纹
/// </summary>
/// <param name="p_CodeId"></param>
/// <returns></returns>
private string GetValue(int p_CodeId)
{
DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
if (_Row.Length != ) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
return _Row[]["BandCode"].ToString();
}
/// <summary>
/// 获得条码图形
/// </summary>
/// <param name="p_Text">文字</param>
/// <returns>图形</returns>
private Bitmap GetImage(string p_Text)
{
char[] _Value = p_Text.ToCharArray();
int _Width = ;
for (int i = ; i != _Value.Length; i++)
{
_Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + );
}
Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
Graphics _Garphics = Graphics.FromImage(_CodeImage);
//Pen _Pen;
int _LenEx = ;
for (int i = ; i != _Value.Length; i++)
{
int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + ); //获取宽和放大系数
if (!((i & ) == ))
{
//_Pen = new Pen(Brushes.White, _ValueNumb);
_Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
}
else
{
//_Pen = new Pen(Brushes.Black, _ValueNumb);
_Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
}
//_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
_LenEx += _ValueNumb;
}
_Garphics.Dispose();
return _CodeImage;
}
/// <summary>
/// 显示可见条码文字 如果小于40 不显示文字
/// </summary>
/// <param name="p_Bitmap">图形</param>
private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
{
if (m_ValueFont == null) return; Graphics _Graphics = Graphics.FromImage(p_Bitmap);
SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
if (_DrawSize.Height > p_Bitmap.Height - || _DrawSize.Width > p_Bitmap.Width)
{
_Graphics.Dispose();
return;
} int _StarX = p_Bitmap.Width - (int)_DrawSize.Width;
int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
_Graphics.FillRectangle(Brushes.White, new Rectangle(, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
_Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, _StarX / , _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();
}
}
05-02 08:04