EAN.CS
2 /// EAN 的摘要说明
3 /// </summary>
4 public class EAN
5 {
6 #region 属性
7 private DataTable m_EAN = new DataTable();
8 private DataTable m_Pic = new DataTable();
9
10 public EAN()
11 {
12 // 编号
13 m_EAN.Columns.Add( " ID " );
14 // 左边数据类型
15 m_EAN.Columns.Add( " Type " );
16 // 左边数据奇数列
17 m_EAN.Columns.Add( " A " );
18 // 左边数据偶数列
19 m_EAN.Columns.Add( " B " );
20 // 右边数据偶数列
21 m_EAN.Columns.Add( " C " );
22
23
24 m_EAN.Rows.Add( " 0 " , " AAAAAA " , " 0001101 " , " 0100111 " , " 1110010 " );
25 m_EAN.Rows.Add( " 1 " , " AABABB " , " 0011001 " , " 0110011 " , " 1100110 " );
26 m_EAN.Rows.Add( " 2 " , " AABBAB " , " 0010011 " , " 0011011 " , " 1101100 " );
27 m_EAN.Rows.Add( " 3 " , " AABBBA " , " 0111101 " , " 0100001 " , " 1000010 " );
28 m_EAN.Rows.Add( " 4 " , " ABAABB " , " 0100011 " , " 0011101 " , " 1011100 " );
29 m_EAN.Rows.Add( " 5 " , " ABBAAB " , " 0110001 " , " 0111001 " , " 1001110 " );
30 m_EAN.Rows.Add( " 6 " , " ABBBAA " , " 0101111 " , " 0000101 " , " 1010000 " );
31 m_EAN.Rows.Add( " 7 " , " ABABAB " , " 0111011 " , " 0010001 " , " 1000100 " );
32 m_EAN.Rows.Add( " 8 " , " ABABBA " , " 0110111 " , " 0001001 " , " 1001000 " );
33 m_EAN.Rows.Add( " 9 " , " ABBABA " , " 0001011 " , " 0010111 " , " 1110100 " );
34
35 // 编号,暂时只支持1.0X与2.0X,建议使用1.0X,因2.0X模块宽度可能无法识别
36 m_Pic.Columns.Add( " ID " );
37 // 字号(像素)
38 m_Pic.Columns.Add( " FontSize " );
39 // 图片宽度(像素)
40 m_Pic.Columns.Add( " PicWidth " );
41 // 图片高度(像素)
42 m_Pic.Columns.Add( " PicHeight " );
43 // 短条纹高度(像素)
44 m_Pic.Columns.Add( " StreakHeight " );
45 // 模块宽度(像素)
46 m_Pic.Columns.Add( " StreakWidth " );
47
48 m_Pic.Rows.Add( " 0.8X " , " 10 " , " 85 " , " 59 " , " 52 " , " 1 " );
49 m_Pic.Rows.Add( " 0.85X " , " 10 " , " 90 " , " 62 " , " 55 " , " 1 " );
50 m_Pic.Rows.Add( " 0.9X " , " 10 " , " 95 " , " 66 " , " 58 " , " 1 " );
51 m_Pic.Rows.Add( " 1.0X " , " 11 " , " 113 " , " 74 " , " 65 " , " 1 " );
52 m_Pic.Rows.Add( " 1.1X " , " 20 " , " 116 " , " 81 " , " 71 " , " 1 " );
53 m_Pic.Rows.Add( " 1.2X " , " 20 " , " 127 " , " 88 " , " 78 " , " 1 " );
54 m_Pic.Rows.Add( " 1.4X " , " 20 " , " 148 " , " 103 " , " 91 " , " 1 " );
55 m_Pic.Rows.Add( " 1.5X " , " 20 " , " 159 " , " 110 " , " 97 " , " 1 " );
56 m_Pic.Rows.Add( " 1.6X " , " 20 " , " 169 " , " 118 " , " 104 " , " 1 " );
57 m_Pic.Rows.Add( " 1.7X " , " 20 " , " 180 " , " 125 " , " 110 " , " 1 " );
58 m_Pic.Rows.Add( " 2.0X " , " 22 " , " 226 " , " 148 " , " 130 " , " 2 " );
59
60 }
61 // 放大倍数
62 private string m_Magnify = " 1.0X " ;
63 public string Magnify
64 {
65 get { return m_Magnify; }
66 set { m_Magnify = value; }
67 }
68 // 图片高度
69 private int m_Height = 74 ;
70 public int Height {
71 get { return m_Height; }
72 set { m_Height = value; }
73 }
74 // 图片宽度
75 private int m_Width = 113 ;
76 public int Width {
77 get { return m_Width; }
78 set { m_Width = value; }
79 }
80 // 字体大小
81 private byte m_FontSize = 12 ;
82 public byte FontSize {
83 get { return m_FontSize; }
84 set { m_FontSize = value; }
85 }
86 // 短条纹高度
87 private int m_StreakHeight = 65 ;
88 public int StreakHeight {
89 get { return m_StreakHeight; }
90 set { m_StreakHeight = value; }
91 }
92 // 条纹宽度
93 private int m_StreakWidth = 1 ;
94 public int StreakWidth {
95 get { return m_StreakWidth; }
96 set { m_StreakWidth = value; }
97 }
98
99 #endregion
100
101 #region 方法
102 public Bitmap GetImageByCode( string Code)
103 {
104 char [] _int = Code.ToCharArray();
105 for ( int i = 0 ; i < _int.Length; i ++ ) {
106 try
107 {
108 int .Parse(_int[i].ToString());
109 }
110 catch {
111 throw new Exception( " 请输入12或13位数字 " );
112 }
113 }
114 // 校验数据的有效性
115 if (Code.Length == 12 )
116 {
117 Code = Code + GetCheckCode(Code);
118 }
119
120 if (Code.Length != 13 && Code.Length != 12 )
121 {
122 throw new Exception( " 请输入12或13位数据 " );
123 }
124
125 // 检测校验码是否正确
126 if (CheckCode(Code) == false ) {
127 throw new Exception( " 校验码错误,请重新输入。 " );
128 }
129
130 // 定入相关参数
131 string _Code = string .Empty;
132 string _HeadCode = string .Empty;
133 string _LeftCode = string .Empty;
134 string _RightCode = string .Empty;
135 string _EndCode = string .Empty;
136 StringBuilder Sb = new StringBuilder();
137
138
139 // 前置码
140 _HeadCode = Code.Substring( 0 , 1 );
141 _Code = Code.Remove( 0 , 1 );
142 // 左边空白符
143 Sb.Append( " 00000000000 " );
144 // 起始行
145 Sb.Append( " 101 " );
146 // 确定左边数据编码方式
147 char [] _LeftType = new char [] { };
148 _LeftType = GetEANValue(Code.Substring( 0 , 1 ), " Type " ).ToCharArray();
149 // 左边数据符号
150 _LeftCode = _Code.Substring( 0 , 6 );
151 _Code = _Code.Remove( 0 , 6 );
152 for ( int i = 0 ; i < _LeftCode.Length; i ++ ) {
153 Sb.Append(GetEANValue(_LeftCode.Substring( 0 , 1 ), _LeftType[i].ToString()));
154 _LeftCode.Remove( 0 , 1 );
155 }
156 // 中间分隔符
157 Sb.Append( " 01010 " );
158 // 右边数据符
159 _RightCode = _Code.Substring( 0 , 5 );
160 _Code = _Code.Remove( 0 , 5 );
161 for ( int i = 0 ; i < _RightCode.Length; i ++ )
162 {
163 Sb.Append(GetEANValue(_RightCode.Substring( 0 , 1 ), " C " ));
164 _RightCode.Remove( 0 , 1 );
165 }
166 // 校验符
167 _EndCode = _Code.Substring( 0 , 1 );
168 Sb.Append(GetEANValue(_EndCode, " C " ));
169 // 终止符
170 Sb.Append( " 101 " );
171 // 右边空白符
172 Sb.Append( " 0000000 " );
173 // 绘制图形
174 return GetImage(Sb.ToString(),Code);
175 }
176
177 private string GetEANValue( string p_Value, string p_Type)
178 {
179 if (m_EAN == null ) return "" ;
180 DataRow[] _Row = m_EAN.Select( " ID=' " + p_Value + " ' " );
181 if (_Row.Length != 1 ) throw new Exception( " 错误的编码 " + p_Value.ToString());
182 return _Row[ 0 ][p_Type].ToString();
183 }
184
185 private string GetPicValue( string p_Value, string p_Type)
186 {
187 if (m_Pic == null ) return "" ;
188 DataRow[] _Row = m_Pic.Select( " ID=' " + p_Value + " ' " );
189 if (_Row.Length != 1 ) throw new Exception( " 错误的编码 " + p_Value.ToString());
190 return _Row[ 0 ][p_Type].ToString();
191 }
192
193
194 // 绘制图形
195 private Bitmap GetImage( string p_Text, string p_ViewText)
196 {
197 char [] _Value = p_Text.ToCharArray();
198 int _FontWidth = 0 ;
199 Font _MyFont = null ;
200
201
202 // 根据倍数确定图片长宽、条纹高度、字体大小
203 if (m_Magnify != " 1.0X " ) {
204 m_Height = int .Parse(GetPicValue(m_Magnify, " PicHeight " ));
205 m_Width = int .Parse(GetPicValue(m_Magnify, " PicWidth " ));
206 m_FontSize = byte .Parse(GetPicValue(m_Magnify, " FontSize " ));
207 m_StreakHeight = int .Parse(GetPicValue(m_Magnify, " StreakHeight " ));
208 m_StreakWidth = int .Parse(GetPicValue(m_Magnify, " StreakWidth " ));
209 }
210
211
212
213 if (m_FontSize != 0 )
214 {
215 #region 获取符合大小的字体
216 _MyFont = new Font( " OCR-B " , m_FontSize, FontStyle.Bold);
217 Bitmap _MyFontBmp = new Bitmap(m_FontSize, m_FontSize);
218 Graphics _FontGraphics = Graphics.FromImage(_MyFontBmp);
219
220 for ( byte i = m_FontSize; i != 0 ; i -- )
221 {
222 SizeF _DrawSize = _FontGraphics.MeasureString(p_ViewText.Substring( 0 , 1 ), _MyFont);
223
224 if (_DrawSize.Height > m_FontSize)
225 {
226 _MyFont = new Font( " OCR-B " , i, FontStyle.Bold);
227 }
228 else
229 {
230 _FontWidth = ( int )_DrawSize.Width;
231 break ;
232 }
233 }
234 #endregion
235 }
236
237 if (ScanDrawText(_MyFont, p_Text, _FontWidth) == false )
238 {
239 _FontWidth = 0 ;
240 m_FontSize = 0 ;
241 }
242
243 // 宽 == 需要绘制的数量*放大倍数 + 两个字的宽
244 // Bitmap _CodeImage = new Bitmap(_Value.Length * ((int)m_Magnify + 1) + (_FontWidth * 2), m_Height);
245 // Graphics _Garphics = Graphics.FromImage(_CodeImage);
246
247 // _Garphics.FillRectangle(Brushes.White, new Rectangle(0, 0, _CodeImage.Width, _CodeImage.Height));
248
249 Bitmap _CodeImage = new Bitmap(m_Width, m_Height);
250 Graphics _Garphics = Graphics.FromImage(_CodeImage);
251 _Garphics.FillRectangle(Brushes.White, new Rectangle( 0 , 0 , _CodeImage.Width, _CodeImage.Height));
252
253 // 输出条形
254 int _Height = 0 ;
255 int _LenEx = 0 ;
256 for ( int i = 0 ; i != _Value.Length; i ++ )
257 {
258 if (i == 11 || i == 13 || i == 57 || i == 59 || i == 103 || i == 105 )
259 {
260 _Height = m_Height;
261 }
262 else
263 {
264 _Height = m_StreakHeight;
265 }
266
267 if (_Value[i] == ' 1 ' )
268 {
269 _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0 , m_StreakWidth, _Height));
270
271 }
272 else
273 {
274 _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0 , m_StreakWidth, _Height));
275 }
276 _LenEx += m_StreakWidth;
277 }
278
279 // 绘制文字
280 if (_FontWidth != 0 && m_FontSize != 0 )
281 {
282 int _StarX = 0 ;
283 int _StarY = m_Height - _MyFont.Height;
284 _Garphics.DrawString(p_ViewText.Substring( 0 , 1 ), _MyFont, Brushes.Black, 0 , _StarY);
285 for ( int i = 0 ; i < 6 ; i ++ )
286 {
287 _StarX = i * m_StreakWidth * 7 + m_StreakWidth * ( 11 + 3 );
288 _Garphics.DrawString(p_ViewText.Substring(i + 1 , 1 ), _MyFont, Brushes.Black, _StarX, _StarY);
289 }
290 for ( int i = 0 ; i < 6 ; i ++ )
291 {
292 _StarX = i * m_StreakWidth * 7 + m_StreakWidth * ( 11 + 3 + 42 + 5 );
293 _Garphics.DrawString(p_ViewText.Substring( 7 + i, 1 ), _MyFont, Brushes.Black, _StarX, _StarY);
294 }
295 // 输出结束符>
296 _StarX = m_StreakWidth * 105 ;
297 _Garphics.DrawString( " > " , _MyFont, Brushes.Black, _StarX, _StarY);
298 }
299
300
301 _Garphics.Dispose();
302 return _CodeImage;
303 }
304 /// <summary>
305 /// 判断字体是否大与绘制图形
306 /// </summary>
307 /// <param name="_MyFont"> 字体 </param>
308 /// <param name="p_Text"> 文字 </param>
309 /// <param name="p_Width"> 字体的宽 </param>
310 /// <returns> true可以绘制 False不可以绘制 </returns>
311 private bool ScanDrawText(Font _MyFont, string p_Text, int p_Width)
312 {
313 // if(_MyFont==null)return false;
314 // int _Width = (p_Text.Length - 6 - 5) * ((int)m_Magnify + 1);
315 // if ((p_Width*12) > _Width) return false;
316 return true ;
317
318 }
319
320 /// <summary>
321 /// 检测校验码是否正确
322 /// </summary>
323 /// <param name="Code"> 校验码 </param>
324 /// <returns> 正确与否 </returns>
325 private bool CheckCode( string Code) {
326 if (Code.Length == 13 )
327 {
328 string _Code = Code.Substring( 0 , 12 );
329 int _verifyCode = int .Parse(GetCheckCode(_Code));
330 if (_verifyCode == int .Parse(Code.Substring( 12 , 1 )))
331 {
332 return true ;
333 }
334 else
335 {
336 return false ;
337 }
338 }
339 else {
340 return false ;
341 }
342 }
343
344
345 /// <summary>
346 /// 获取EAN13Code校验码
347 /// </summary>
348 /// <param name="Code"> EAN13Code前12位码 </param>
349 /// <returns> EAN13Code校验码 </returns>
350 private string GetCheckCode( string Code) {
351 if (Code.Length != 12 )
352 {
353 return "" ;
354 }
355 else {
356 // 奇位数之和
357 int _oddNum = 0 ;
358 // 偶位数之和
359 int _evenNum = 0 ;
360 for ( int i = 0 ; i < Code.Length; i ++ )
361 {
362 if ((i + 1 ) % 2 == 0 )
363 {
364 _oddNum += int .Parse(Code.Substring(i, 1 ));
365 }
366 else
367 {
368 _evenNum += int .Parse(Code.Substring(i, 1 ));
369 }
370 }
371 int _verifyCode = 10 - ((_oddNum * 3 + _evenNum) > 100 ? ((_oddNum * 3 + _evenNum) % 100 ) % 10 : (_oddNum * 3 + _evenNum) % 10 );
372 return _verifyCode.ToString();
373 }
374
375
376 }
377 #endregion
378 }
前台
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>1.0X</asp:ListItem>
<asp:ListItem>2.0X</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="生成EAN13条形码" />
<br />
<br />
PS:输入12位或13位数字<br />
12位则自动生成校验码.13位会验证校验码是否正确<br />
事件
protected void Button1_Click(object sender, EventArgs e)
{
EAN _EANCode = new EAN();
_EANCode.Magnify = DropDownList1.SelectedValue;
Bitmap aa = _EANCode.GetImageByCode(TextBox1.Text.Trim());
Response.ContentType = "image/jpeg";
aa.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
aa.Dispose();
//aa.Save(@"C:\1.bmp");
}