//该段代码为 生成条形码代码
//生成效果
请教该条形码是否正确!
主要是生成条形码 二进制的地方!
本人Email:paabo@live.cn 恭候指教
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Paabo.WordProcessing.Common
{
public class BarCodeProvider
{
#region 单例
private static BarCodeProvider _Intance;
public static BarCodeProvider Intance
{
get
{
if (_Intance == null)
{
_Intance = new BarCodeProvider();
}
return _Intance;
}
}
#endregion
#region Size
/// <summary>
/// 图片宽度
/// </summary>
private int _Width = 200;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
/// <summary>
/// 图片高度
/// </summary>
private int _Height = 80;
public int Height
{
get { return _Height; }
set { _Height = value; }
}
/// <summary>
/// 明文高度
/// </summary>
private int _TextHeight = 25;
public int TextHeight
{
get { return _TextHeight; }
set { _TextHeight = value; }
}
#endregion
#region 边距
private int _Margin_Top = 5;
/// <summary>
/// 上边距
/// </summary>
public int Margin_Top
{
get { return _Margin_Top; }
set { _Margin_Top = value; }
}
private int _Margin_Left = 5;
/// <summary>
/// 左边距
/// </summary>
public int Margin_Left
{
get { return _Margin_Left; }
set { _Margin_Left = value; }
}
private int _Margin_Right = 5;
/// <summary>
/// 右边距
/// </summary>
public int Margin_Right
{
get { return _Margin_Right; }
set { _Margin_Right = value; }
}
private int _Margin_Bottom = 5;
/// <summary>
/// 下边距
/// </summary>
public int Margin_Bottom
{
get { return _Margin_Bottom; }
set { _Margin_Bottom = value; }
}
#endregion
private Font _TextFont = new Font("宋体", 12);
/// <summary>
/// 明文字体
/// </summary>
public Font TextFont
{
get { return _TextFont; }
set { _TextFont = value; }
}
private Pen _BlackPen = new Pen(Brushes.Black);
private Pen _WhitePen = new Pen(Brushes.White);
/// <summary>
/// 将明文装换为编码
/// </summary>
/// <param name="text">明文内容</param>
/// <returns>编码</returns>
private string ConvertToBarCode(string text)
{
string code = string.Empty;
foreach (char item in text)
{
int itemValue = item;
code += Convert.ToString(itemValue, 2) + ",";
}
return code;
}
/// <summary>
/// 将字符串生成条形图片
/// </summary>
/// <param name="text">明文内容</param>
/// <returns></returns>
public Bitmap CreateBarCodeImage(string text)
{
Bitmap map = new Bitmap(Width, Height);
Graphics g = Graphics.FromImage(map);
try
{
string code = ConvertToBarCode(text);
code = string.Format("101{0}101", code);
char[] array = code.ToCharArray();
char[] textArray = text.ToCharArray();
int lineWidth = (Width - Margin_Left-Margin_Right) / (array.Length - textArray.Length);
int lineHeight = Height - TextHeight - Margin_Bottom;
_BlackPen.Width = lineWidth;
_WhitePen.Width = lineWidth;
int x = 5;
int topY = 5;
int bottonY = Height - Margin_Bottom - TextHeight;
int index = 0;
char pItem = ' ';
Pen pen = null;
foreach (char item in array)
{
if (item == ',')
{
string t = textArray[index].ToString();
g.DrawString(t, TextFont, Brushes.Black, new PointF(x - (lineWidth * 5), bottonY + 3));
index++;
}
else
{
if (pItem == ' ')
{
pen = _BlackPen;
}
else
{
if (item != pItem)
{
pen = pen == _BlackPen ? _WhitePen : _BlackPen;
}
}
pItem = item;
g.DrawLine(pen, new Point(x, topY), new Point(x, bottonY));
}
x += lineWidth;
}
}
catch (Exception ex)
{
g.Clear(Color.White);
g.DrawString(ex.Message, TextFont, Brushes.Black, new PointF(0, 0));
}
g.Save();
return map;
}
}
}
打印出来,拿去扫描条形码的机器上面,一扫就知道是对是错了。
我发个给你看看,这个我是测试过的,肯定是可以用的。
1 using System;
2 using System.Collections.Generic;
3 using System.Web;
4 using System.Collections;
5 using System.Text.RegularExpressions;
6
7 namespace BarCode
8 {
9 public class BarCodeToHTML
10 {
11 public static string get39(string s, int width, int height)
12 {
13 Hashtable ht = new Hashtable();
14
15 #region 39码 12位
16 ht.Add('A', "110101001011");
17 ht.Add('B', "101101001011");
18 ht.Add('C', "110110100101");
19 ht.Add('D', "101011001011");
20 ht.Add('E', "110101100101");
21 ht.Add('F', "101101100101");
22 ht.Add('G', "101010011011");
23 ht.Add('H', "110101001101");
24 ht.Add('I', "101101001101");
25 ht.Add('J', "101011001101");
26 ht.Add('K', "110101010011");
27 ht.Add('L', "101101010011");
28 ht.Add('M', "110110101001");
29 ht.Add('N', "101011010011");
30 ht.Add('O', "110101101001");
31 ht.Add('P', "101101101001");
32 ht.Add('Q', "101010110011");
33 ht.Add('R', "110101011001");
34 ht.Add('S', "101101011001");
35 ht.Add('T', "101011011001");
36 ht.Add('U', "110010101011");
37 ht.Add('V', "100110101011");
38 ht.Add('W', "110011010101");
39 ht.Add('X', "100101101011");
40 ht.Add('Y', "110010110101");
41 ht.Add('Z', "100110110101");
42 ht.Add('0', "101001101101");
43 ht.Add('1', "110100101011");
44 ht.Add('2', "101100101011");
45 ht.Add('3', "110110010101");
46 ht.Add('4', "101001101011");
47 ht.Add('5', "110100110101");
48 ht.Add('6', "101100110101");
49 ht.Add('7', "101001011011");
50 ht.Add('8', "110100101101");
51 ht.Add('9', "101100101101");
52 ht.Add('+', "100101001001");
53 ht.Add('-', "100101011011");
54 ht.Add('*', "100101101101");
55 ht.Add('/', "100100101001");
56 ht.Add('%', "101001001001");
57 ht.Add('$', "100100100101");
58 ht.Add('.', "110010101101");
59 ht.Add(' ', "100110101101");
60 #endregion
61 #region 39码 9位
62 //ht.Add('0', "000110100");
63 //ht.Add('1', "100100001");
64 //ht.Add('2', "001100001");
65 //ht.Add('3', "101100000");
66 //ht.Add('4', "000110001");
67 //ht.Add('5', "100110000");
68 //ht.Add('6', "001110000");
69 //ht.Add('7', "000100101");
70 //ht.Add('8', "100100100");
71 //ht.Add('9', "001100100");
72 //ht.Add('A', "100001001");
73 //ht.Add('B', "001001001");
74 //ht.Add('C', "101001000");
75 //ht.Add('D', "000011001");
76 //ht.Add('E', "100011000");
77 //ht.Add('F', "001011000");
78 //ht.Add('G', "000001101");
79 //ht.Add('H', "100001100");
80 //ht.Add('I', "001001100");
81 //ht.Add('J', "000011100");
82 //ht.Add('K', "100000011");
83 //ht.Add('L', "001000011");
84 //ht.Add('M', "101000010");
85 //ht.Add('N', "000010011");
86 //ht.Add('O', "100010010");
87 //ht.Add('P', "001010010");
88 //ht.Add('Q', "000000111");
89 //ht.Add('R', "100000110");
90 //ht.Add('S', "001000110");
91 //ht.Add('T', "000010110");
92 //ht.Add('U', "110000001");
93 //ht.Add('V', "011000001");
94 //ht.Add('W', "111000000");
95 //ht.Add('X', "010010001");
96 //ht.Add('Y', "110010000");
97 //ht.Add('Z', "011010000");
98 //ht.Add('-', "010000101");
99 //ht.Add('.', "110000100");
100 //ht.Add(' ', "011000100");
101 //ht.Add('*', "010010100");
102 //ht.Add('$', "010101000");
103 //ht.Add('/', "010100010");
104 //ht.Add('+', "010001010");
105 //ht.Add('%', "000101010");
106 #endregion
107
108 s = "*" + s.ToUpper() + "*";
109
110 string result_bin = "";//二进制串
111
112 try
113 {
114 foreach (char ch in s)
115 {
116 result_bin += ht[ch].ToString();
117 result_bin += "0";//间隔,与一个单位的线条宽度相等
118 }
119 }
120 catch { return "存在不允许的字符!"; }
121
122 string result_html = "";//HTML代码
123 string color = "";//颜色
124 foreach (char c in result_bin)
125 {
126 color = c == '0' ? "#FFFFFF" : "#000000";
127 result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
128 }
129 result_html += "<div style=\"clear:both\"></div>";
130
131 int len = ht['*'].ToString().Length;
132 foreach (char c in s)
133 {
134 result_html += "<div style=\"width:" + (width * (len + 1)) + "px;float:left;color:#000000;text-align:center;\">" + c + "</div>";
135 }
136 result_html += "<div style=\"clear:both\"></div>";
137
138 return "<div style=\"background:#FFFFFF;padding:5px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + result_html + "</div>";
139 }
140 public static string getEAN13(string s, int width, int height)
141 {
142 int checkcode_input = -1;//输入的校验码
143 if (!Regex.IsMatch(s, @"^\d{12}$"))
144 {
145 if (!Regex.IsMatch(s, @"^\d{13}$"))
146 {
147 return "存在不允许的字符!";
148 }
149 else
150 {
151 checkcode_input = int.Parse(s[12].ToString());
152 s = s.Substring(0, 12);
153 }
154 }
155
156 int sum_even = 0;//偶数位之和
157 int sum_odd = 0;//奇数位之和
158
159 for (int i = 0; i < 12; i++)
160 {
161 if (i % 2 == 0)
162 {
163 sum_odd += int.Parse(s[i].ToString());
164 }
165 else
166 {
167 sum_even += int.Parse(s[i].ToString());
168 }
169 }
170
171 int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码
172
173 if (checkcode_input > 0 && checkcode_input != checkcode)
174 {
175 return "输入的校验码错误!";
176 }
177
178 s += checkcode;//变成13位
179
180 // 000000000101左侧42个01010右侧35个校验7个101000000000
181 // 6 101左侧6位 01010右侧5位 校验1位101000000000
182
183 string result_bin = "";//二进制串
184 result_bin += "000000000101";
185
186 string type = ean13type(s[0]);
187 for (int i = 1; i < 7; i++)
188 {
189 result_bin += ean13(s[i], type[i - 1]);
190 }
191 result_bin += "01010";
192 for (int i = 7; i < 13; i++)
193 {
194 result_bin += ean13(s[i], 'C');
195 }
196 result_bin += "101000000000";
197
198 string result_html = "";//HTML代码
199 string color = "";//颜色
200 int height_bottom = width * 5;
201 foreach (char c in result_bin)
202 {
203 color = c == '0' ? "#FFFFFF" : "#000000";
204 result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
205 }
206 result_html += "<div style=\"clear:both\"></div>";
207
208 result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\">" + s[0] + "</div>";
209 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
210 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
211 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
212 for (int i = 1; i < 7; i++)
213 {
214 result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
215 }
216 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
217 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
218 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
219 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
220 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
221 for (int i = 7; i < 13; i++)
222 {
223 result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
224 }
225 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
226 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
227 result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
228 result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"></div>";
229 result_html += "<div style=\"clear:both\"></div>";
230
231 return "<div style=\"background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:'楷体';\">" + result_html + "</div>";
232 }
233 private static string ean13(char c, char type)
234 {
235 switch (type)
236 {
237 case 'A':
238 {
239 switch (c)
240 {
241 case '0': return "0001101";
242 case '1': return "0011001";
243 case '2': return "0010011";
244 case '3': return "0111101";//011101
245 case '4': return "0100011";
246 case '5': return "0110001";
247 case '6': return "0101111";
248 case '7': return "0111011";
249 case '8': return "0110111";
250 case '9': return "0001011";
251 default: return "Error!";
252 }
253 }
254 case 'B':
255 {
256 switch (c)
257 {
258 case '0': return "0100111";
259 case '1': return "0110011";
260 case '2': return "0011011";
261 case '3': return "0100001";
262 case '4': return "0011101";
263 case '5': return "0111001";
264 case '6': return "0000101";//000101
265 case '7': return "0010001";
266 case '8': return "0001001";
267 case '9': return "0010111";
268 default: return "Error!";
269 }
270 }
271 case 'C':
272 {
273 switch (c)
274 {
275 case '0': return "1110010";
276 case '1': return "1100110";
277 case '2': return "1101100";
278 case '3': return "1000010";
279 case '4': return "1011100";
280 case '5': return "1001110";
281 case '6': return "1010000";
282 case '7': return "1000100";
283 case '8': return "1001000";
284 case '9': return "1110100";
285 default: return "Error!";
286 }
287 }
288 default: return "Error!";
289 }
290 }
291 private static string ean13type(char c)
292 {
293 switch (c)
294 {
295 case '0': return "AAAAAA";
296 case '1': return "AABABB";
297 case '2': return "AABBAB";
298 case '3': return "AABBBA";
299 case '4': return "ABAABB";
300 case '5': return "ABBAAB";
301 case '6': return "ABBBAA";//中国
302 case '7': return "ABABAB";
303 case '8': return "ABABBA";
304 case '9': return "ABBABA";
305 default: return "Error!";
306 }
307 }
308 }
309 }
310