/// <summary> /// Code128 的摘要说明。 /// </summary> public class Code128 { private string m_caption; private int m_orientation = 0; private string m_printthis; public bool m_showText = true; private Color BackColor= Color.White; protected Brush BarBrush = Brushes.Black; private Color ForeColor = Color.Black; private Font Font = new Font("宋体",10); public string strVale = ""; public int leftMargin = 10;//条码的起点x坐标 public int topMargin = 10; //条码的起点y坐标 public int Height; public int LineWidth;//条码宽度 public Code128() { } public virtual Bitmap DrawBarCode() { int iTemp = (this.strVale.Length*8)*LineWidth; Bitmap bitmap1 = new Bitmap(iTemp+50, this.Height); Graphics graphics1 = Graphics.FromImage(bitmap1); graphics1.FillRectangle(Brushes.White, 0, 0, bitmap1.Size.Width, this.Height); this.drawbarcode(graphics1); graphics1.Dispose(); graphics1 = null; return bitmap1; } private string mkbarcodeC128() { string text = "11010010000"; string text2 = "11010011100"; string text3 = "1100011101011";
long num2 = 0; bool flag = false; string text4 = ""; string caption = this.strVale; this.m_caption = this.strVale; text4 = text; long num = 0x68; if (caption.StartsWith("\u0107") || (((caption.Length > 1) && this.myisdigit(caption[0])) && this.myisdigit(caption[1]))) { text4 = text2; flag = true; num = 0x69;//c code } for (int i = 0; i < caption.Length; i++) { if (((caption.Length > (i + 1)) && this.myisdigit(caption[i])) && this.myisdigit(caption[i + 1])) { if (!flag) { flag = true; text4 = text4 + this.getC128char(0x63); num2++; num += num2 * 0x63;//A码 } text4 = text4 + this.getC128char(Convert.ToInt32(caption.Substring(i, 2))); num2++; num += num2 * Convert.ToInt32(caption.Substring(i, 2)); i++; } else if (caption.Substring(i, 1) == "\u0107") { text4 = text4 + this.getC128char(0x66); num2++; num += num2 * 0x66; } else { if (flag) { flag = false; text4 = text4 + this.getC128char(100); num2++; num += num2 * 100; } if ((caption[i] >= ' ') && (caption[i] <= '\x0086')) { text4 = text4 + this.getC128char(caption[i] - ' '); num2++; num += num2 * (caption[i] - ' '); } else if (caption[i] < ' ') { text4 = text4 + this.getC128char(0x62) + this.getC128char(caption[i] + '@'); num2++; num += num2 * 0x62; num2++; num += num2 * (caption[i] + '@'); } } } this.m_printthis = this.m_caption; long itemp = num/(long) 0x67; num -= (num / ((long) 0x67)) *0x67; this.m_printthis = this.m_printthis.Replace("\u0107", " "); return (text4 + this.getC128char((int) num) + text3); } private bool myisdigit(int c) { return ((c >= 0x30) && (c <= 0x39)); } public void drawbarcode(Graphics g) { string caption = this.strVale; Font font = this.Font; SolidBrush brush = new SolidBrush(this.BackColor); SolidBrush brush2 = new SolidBrush(this.ForeColor); Matrix matrix = new Matrix(); matrix.RotateAt((float) this.m_orientation, new PointF(0f, 0f)); Rectangle rect = new Rectangle(0,0,1,2); string barcodeBits = this.mkbarcodeC128(); rect.X = this.leftMargin; rect.Y = this.topMargin; rect.Width = barcodeBits.Length+20; rect.Height = this.Height; SizeF ef = new SizeF(0f, 0f); if (font != null) { ef = g.MeasureString(caption, font, 0); } g.FillRectangle(brush, rect);
caption = this.m_printthis; double num = 0;
if (barcodeBits.Length < 2) { g.DrawString("", font, brush2, (float) rect.Left, (float) rect.Top); } else { num = LineWidth; int num2 = 0; for (int i = 0; i < barcodeBits.Length; i++) { switch (barcodeBits[i]) { case '1': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f)); break; case '2': if (this.m_showText) { g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height)); num2++; } break; case '3': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f)); if (this.m_showText) { g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height)); num2++; } break; case '4': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? (ef.Height / 3f) : 0f)); break; case '5': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - ef.Height); break; case '6': if (this.m_showText) { g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height)); num2++; } break; case '7': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f)); break; case '8': g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) (rect.Top + ((rect.Bottom - rect.Top) / 2)), (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f)); break; } } if (this.m_showText && (num2 == 0)) { g.DrawString(caption, font, brush2, (float) ((rect.Left + (((float) (num * barcodeBits.Length)) / 2f)) - (ef.Width / 2f)), (float) (rect.Bottom - ef.Height)); } } } }
类太长private string getC128char(int c) { switch (c) { case 0: return "11011001100"; case 1: return "11001101100"; case 2: return "11001100110"; case 3: return "10010011000"; case 4: return "10010001100"; case 5: return "10001001100"; case 6: return "10011001000"; case 7: return "10011000100"; case 8: return "10001100100"; case 9: return "11001001000"; case 10: return "11001000100"; case 11: return "11000100100"; case 12: return "10110011100"; case 13: return "10011011100"; case 14: return "10011001110"; case 15: return "10111001100"; case 0x10: return "10011101100"; case 0x11: return "10011100110"; case 0x12: return "11001110010"; case 0x13: return "11001011100"; case 20: return "11001001110"; case 0x15: return "11011100100"; case 0x16: return "11001110100"; case 0x17: return "11101101110"; case 0x18: return "11101001100"; case 0x19: return "11100101100"; case 0x1a: return "11100100110"; case 0x1b: return "11101100100"; case 0x1c: return "11100110100"; case 0x1d: return "11100110010"; case 30: return "11011011000"; case 0x1f: return "11011000110"; case 0x20: return "11000110110"; case 0x21: return "10100011000"; case 0x22: return "10001011000"; case 0x23: return "10001000110"; case 0x24: return "10110001000"; case 0x25: return "10001101000"; case 0x26: return "10001100010"; case 0x27: return "11010001000"; case 40: return "11000101000"; case 0x29: return "11000100010"; case 0x2a: return "10110111000"; case 0x2b: return "10110001110"; case 0x2c: return "10001101110"; case 0x2d: return "10111011000"; case 0x2e: return "10111000110"; case 0x2f: return "10001110110"; case 0x30: return "11101110110"; case 0x31: return "11010001110"; case 50: return "11000101110"; case 0x33: return "11011101000"; case 0x34: return "11011100010"; case 0x35: return "11011101110"; case 0x36: return "11101011000"; case 0x37: return "11101000110"; case 0x38: return "11100010110"; case 0x39: return "11101101000"; case 0x3a: return "11101100010"; case 0x3b: return "11100011010"; case 60: return "11101111010"; case 0x3d: return "11001000010"; case 0x3e: return "11110001010"; case 0x3f: return "10100110000"; case 0x40: return "10100001100"; case 0x41: return "10010110000"; case 0x42: return "10010000110"; case 0x43: return "10000101100"; case 0x44: return "10000100110"; case 0x45: return "10110010000"; case 70: return "10110000100"; case 0x47: return "10011010000"; case 0x48: return "10011000010"; case 0x49: return "10000110100"; case 0x4a: return "10000110010"; case 0x4b: return "11000010010"; case 0x4c: return "11001010000"; case 0x4d: return "11110111010"; case 0x4e: return "11000010100"; case 0x4f: return "10001111010"; case 80: return "10100111100"; case 0x51: return "10010111100"; case 0x52: return "10010011110"; case 0x53: return "10111100100"; case 0x54: return "10011110100"; case 0x55: return "10011110010"; case 0x56: return "11110100100"; case 0x57: return "11110010100"; case 0x58: return "11110010010"; case 0x59: return "11011011110"; case 90: return "11011110110"; case 0x5b: return "11110110110"; case 0x5c: return "10101111000"; case 0x5d: return "10100011110"; case 0x5e: return "10001011110"; case 0x5f: return "10111101000"; case 0x60: return "10111100010"; case 0x61: return "11110101000"; case 0x62: return "11110100010"; case 0x63: return "10111011110"; case 100: return "10111101110"; case 0x65: return "11101011110"; case 0x66: return "11110101110"; } return "00000000000"; }
有一个dll可以直接生成。
ean-13的编码规则看这篇文章 我写过一个code93的 class Code93 { List<int> indexs = new List<int>(20); char[] chars = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J', 'K','L','M','N','O','P','Q','R','S','T', 'U','V','W','X','Y','Z','-','.',' ','$', '/','+','%'}; string[] codes = {"100010100","101001000","101000100","101000010","100101000", "100100100","100100010","101010000","100010010","100001010" , "110101000","110100100","110100010","110010100","110010010", "110001010","101101000","101100100","101100010","100110100", "100011010","101011000","101001100","101000110","100101100", "100010110","110110100","110110010","110101100","110100110" , "110010110","110011010","101101100","101100110","100110110" , "100111010","100101110","111010100","111010010","111001010" , "101101110","101101110","110101110","100100110","111011010" , "111010110","100110010","101011110","1010111101"}; public string EndodingCode93(string s) { string code = codes[47]; for (int i = 0; i < s.Length; i++) { if (s[i] == '+') return null; int y = -1; for (int x = 0; x < chars.Length; x++) if (chars[x] == s[i]) { y = x; break; } if (y == -1) return null; code += codes[y]; indexs.Add(y); } int m = 0; for (int i = indexs.Count - 1; i >= 0; i--) m += (((indexs.Count - 1 - i) % 20) + 1) * indexs[i]; indexs.Add(m % 47); int n = 0; for (int i = indexs.Count - 1; i >= 0; i--) n += (((indexs.Count - 1 - i) % 15) + 1) * indexs[i]; code += codes[m % 47]; code += codes[n % 47]; code += codes[48]; indexs.Clear(); return code; } }
http://blog.csdn.net/fazzamilan/archive/2009/03/20/4008364.aspx
试过了,可以的
参考
核心代码在:code.pas这个文件里,是用记事本打开就行了。
如果有熟悉delphi的人。让他给你编译个dll。直接调用就行了。
/// <summary>
/// Code128 的摘要说明。
/// </summary>
public class Code128
{
private string m_caption;
private int m_orientation = 0;
private string m_printthis;
public bool m_showText = true;
private Color BackColor= Color.White;
protected Brush BarBrush = Brushes.Black;
private Color ForeColor = Color.Black;
private Font Font = new Font("宋体",10);
public string strVale = "";
public int leftMargin = 10;//条码的起点x坐标
public int topMargin = 10; //条码的起点y坐标
public int Height;
public int LineWidth;//条码宽度
public Code128()
{
}
public virtual Bitmap DrawBarCode()
{
int iTemp = (this.strVale.Length*8)*LineWidth;
Bitmap bitmap1 = new Bitmap(iTemp+50, this.Height);
Graphics graphics1 = Graphics.FromImage(bitmap1);
graphics1.FillRectangle(Brushes.White, 0, 0, bitmap1.Size.Width, this.Height);
this.drawbarcode(graphics1);
graphics1.Dispose();
graphics1 = null;
return bitmap1;
}
private string mkbarcodeC128()
{
string text = "11010010000";
string text2 = "11010011100";
string text3 = "1100011101011";
long num2 = 0;
bool flag = false;
string text4 = "";
string caption = this.strVale;
this.m_caption = this.strVale;
text4 = text;
long num = 0x68;
if (caption.StartsWith("\u0107") || (((caption.Length > 1) && this.myisdigit(caption[0])) && this.myisdigit(caption[1])))
{
text4 = text2;
flag = true;
num = 0x69;//c code
}
for (int i = 0; i < caption.Length; i++)
{
if (((caption.Length > (i + 1)) && this.myisdigit(caption[i])) && this.myisdigit(caption[i + 1]))
{
if (!flag)
{
flag = true;
text4 = text4 + this.getC128char(0x63);
num2++;
num += num2 * 0x63;//A码
}
text4 = text4 + this.getC128char(Convert.ToInt32(caption.Substring(i, 2)));
num2++;
num += num2 * Convert.ToInt32(caption.Substring(i, 2));
i++;
}
else if (caption.Substring(i, 1) == "\u0107")
{
text4 = text4 + this.getC128char(0x66);
num2++;
num += num2 * 0x66;
}
else
{
if (flag)
{
flag = false;
text4 = text4 + this.getC128char(100);
num2++;
num += num2 * 100;
}
if ((caption[i] >= ' ') && (caption[i] <= '\x0086'))
{
text4 = text4 + this.getC128char(caption[i] - ' ');
num2++;
num += num2 * (caption[i] - ' ');
}
else if (caption[i] < ' ')
{
text4 = text4 + this.getC128char(0x62) + this.getC128char(caption[i] + '@');
num2++;
num += num2 * 0x62;
num2++;
num += num2 * (caption[i] + '@');
}
}
}
this.m_printthis = this.m_caption;
long itemp = num/(long) 0x67;
num -= (num / ((long) 0x67)) *0x67;
this.m_printthis = this.m_printthis.Replace("\u0107", " ");
return (text4 + this.getC128char((int) num) + text3);
}
private bool myisdigit(int c)
{
return ((c >= 0x30) && (c <= 0x39));
}
public void drawbarcode(Graphics g)
{
string caption = this.strVale;
Font font = this.Font;
SolidBrush brush = new SolidBrush(this.BackColor);
SolidBrush brush2 = new SolidBrush(this.ForeColor);
Matrix matrix = new Matrix();
matrix.RotateAt((float) this.m_orientation, new PointF(0f, 0f));
Rectangle rect = new Rectangle(0,0,1,2); string barcodeBits = this.mkbarcodeC128();
rect.X = this.leftMargin;
rect.Y = this.topMargin;
rect.Width = barcodeBits.Length+20;
rect.Height = this.Height; SizeF ef = new SizeF(0f, 0f);
if (font != null)
{
ef = g.MeasureString(caption, font, 0);
}
g.FillRectangle(brush, rect);
caption = this.m_printthis;
double num = 0;
if (barcodeBits.Length < 2)
{
g.DrawString("", font, brush2, (float) rect.Left, (float) rect.Top);
}
else
{
num = LineWidth;
int num2 = 0;
for (int i = 0; i < barcodeBits.Length; i++)
{
switch (barcodeBits[i])
{
case '1':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f));
break; case '2':
if (this.m_showText)
{
g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height));
num2++;
}
break; case '3':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f));
if (this.m_showText)
{
g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height));
num2++;
}
break; case '4':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? (ef.Height / 3f) : 0f));
break; case '5':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - ef.Height);
break; case '6':
if (this.m_showText)
{
g.DrawString(Convert.ToString(this.m_printthis[num2]), this.Font, brush2, (float) (((float) (i * num)) + rect.Left), (float) (rect.Bottom - ef.Height));
num2++;
}
break; case '7':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) rect.Top, (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f));
break; case '8':
g.FillRectangle(brush2, (float) (((int) (i * num)) + rect.Left), (float) (rect.Top + ((rect.Bottom - rect.Top) / 2)), (float) (((int) ((i + 1) * num)) - ((int) (i * num))), rect.Height - (this.m_showText ? ef.Height : 0f));
break;
}
}
if (this.m_showText && (num2 == 0))
{
g.DrawString(caption, font, brush2, (float) ((rect.Left + (((float) (num * barcodeBits.Length)) / 2f)) - (ef.Width / 2f)), (float) (rect.Bottom - ef.Height));
}
}
}
}
{
switch (c)
{
case 0:
return "11011001100"; case 1:
return "11001101100"; case 2:
return "11001100110"; case 3:
return "10010011000"; case 4:
return "10010001100"; case 5:
return "10001001100"; case 6:
return "10011001000"; case 7:
return "10011000100"; case 8:
return "10001100100"; case 9:
return "11001001000"; case 10:
return "11001000100"; case 11:
return "11000100100"; case 12:
return "10110011100"; case 13:
return "10011011100"; case 14:
return "10011001110"; case 15:
return "10111001100"; case 0x10:
return "10011101100"; case 0x11:
return "10011100110"; case 0x12:
return "11001110010"; case 0x13:
return "11001011100"; case 20:
return "11001001110"; case 0x15:
return "11011100100"; case 0x16:
return "11001110100"; case 0x17:
return "11101101110"; case 0x18:
return "11101001100"; case 0x19:
return "11100101100"; case 0x1a:
return "11100100110"; case 0x1b:
return "11101100100"; case 0x1c:
return "11100110100"; case 0x1d:
return "11100110010"; case 30:
return "11011011000"; case 0x1f:
return "11011000110"; case 0x20:
return "11000110110"; case 0x21:
return "10100011000"; case 0x22:
return "10001011000"; case 0x23:
return "10001000110"; case 0x24:
return "10110001000"; case 0x25:
return "10001101000"; case 0x26:
return "10001100010"; case 0x27:
return "11010001000"; case 40:
return "11000101000"; case 0x29:
return "11000100010"; case 0x2a:
return "10110111000"; case 0x2b:
return "10110001110"; case 0x2c:
return "10001101110"; case 0x2d:
return "10111011000"; case 0x2e:
return "10111000110"; case 0x2f:
return "10001110110"; case 0x30:
return "11101110110"; case 0x31:
return "11010001110"; case 50:
return "11000101110"; case 0x33:
return "11011101000"; case 0x34:
return "11011100010"; case 0x35:
return "11011101110"; case 0x36:
return "11101011000"; case 0x37:
return "11101000110"; case 0x38:
return "11100010110"; case 0x39:
return "11101101000"; case 0x3a:
return "11101100010"; case 0x3b:
return "11100011010"; case 60:
return "11101111010"; case 0x3d:
return "11001000010"; case 0x3e:
return "11110001010"; case 0x3f:
return "10100110000"; case 0x40:
return "10100001100"; case 0x41:
return "10010110000"; case 0x42:
return "10010000110"; case 0x43:
return "10000101100"; case 0x44:
return "10000100110"; case 0x45:
return "10110010000"; case 70:
return "10110000100"; case 0x47:
return "10011010000"; case 0x48:
return "10011000010"; case 0x49:
return "10000110100"; case 0x4a:
return "10000110010"; case 0x4b:
return "11000010010"; case 0x4c:
return "11001010000"; case 0x4d:
return "11110111010"; case 0x4e:
return "11000010100"; case 0x4f:
return "10001111010"; case 80:
return "10100111100"; case 0x51:
return "10010111100"; case 0x52:
return "10010011110"; case 0x53:
return "10111100100"; case 0x54:
return "10011110100"; case 0x55:
return "10011110010"; case 0x56:
return "11110100100"; case 0x57:
return "11110010100"; case 0x58:
return "11110010010"; case 0x59:
return "11011011110"; case 90:
return "11011110110"; case 0x5b:
return "11110110110"; case 0x5c:
return "10101111000"; case 0x5d:
return "10100011110"; case 0x5e:
return "10001011110"; case 0x5f:
return "10111101000"; case 0x60:
return "10111100010"; case 0x61:
return "11110101000"; case 0x62:
return "11110100010"; case 0x63:
return "10111011110"; case 100:
return "10111101110"; case 0x65:
return "11101011110"; case 0x66:
return "11110101110";
}
return "00000000000";
}
有一个dll可以直接生成。
我写过一个code93的 class Code93
{
List<int> indexs = new List<int>(20);
char[] chars = {'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z','-','.',' ','$',
'/','+','%'};
string[] codes = {"100010100","101001000","101000100","101000010","100101000",
"100100100","100100010","101010000","100010010","100001010" ,
"110101000","110100100","110100010","110010100","110010010",
"110001010","101101000","101100100","101100010","100110100",
"100011010","101011000","101001100","101000110","100101100",
"100010110","110110100","110110010","110101100","110100110" ,
"110010110","110011010","101101100","101100110","100110110" ,
"100111010","100101110","111010100","111010010","111001010" ,
"101101110","101101110","110101110","100100110","111011010" ,
"111010110","100110010","101011110","1010111101"};
public string EndodingCode93(string s)
{
string code = codes[47];
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '+')
return null;
int y = -1;
for (int x = 0; x < chars.Length; x++)
if (chars[x] == s[i])
{
y = x;
break;
}
if (y == -1)
return null;
code += codes[y];
indexs.Add(y);
}
int m = 0;
for (int i = indexs.Count - 1; i >= 0; i--)
m += (((indexs.Count - 1 - i) % 20) + 1) * indexs[i];
indexs.Add(m % 47);
int n = 0;
for (int i = indexs.Count - 1; i >= 0; i--)
n += (((indexs.Count - 1 - i) % 15) + 1) * indexs[i];
code += codes[m % 47];
code += codes[n % 47];
code += codes[48];
indexs.Clear();
return code;
}
}