`
hulianwang2014
  • 浏览: 690236 次
文章分类
社区版块
存档分类
最新评论
  • bcworld: 排版成这样,一点看的欲望都没有了
    jfinal

.NET 绘制 EAN13 (商品条码)

 
阅读更多

<script type="text/javascript"><!-- document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData(&quot;text&quot;); if (text &amp;&amp; text.length&gt;300) { text = text + &quot;/r/n/n本文来自CSDN博客,转载请标明出处:&quot; + location.href; clipboardData.setData(&quot;text&quot;, text); } }, 100); } } // --></script><script class="blogstory"><!-- function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();} // --></script>

EAN商品条码的类

这个使用的比较多

其实 前面的编码是根据前面的6来判断的。是类型A还是类型B

效果

使用方法

  1. //获取验证位
  2. char _ISBN=MyImage.BandCode.EAN13.EAN13ISBN( "690102803695" );
  3. MessageBox.Show(_ISBN.ToString());
  4. MyImage.BandCode.EAN13_EAN13Code= new MyImage.BandCode.EAN13();
  5. _EAN13Code.Magnify=1;
  6. _EAN13Code.Heigth=100;
  7. _EAN13Code.FontSize=16;
  8. pictureBox1.Image=_EAN13Code.GetCodeImage( "6901028036955" );
  9. pictureBox1.Image.Save(@ "C:/1.bmp" );

具体绘制类

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. namespace MyImage.BandCode
  7. {
  8. public class EAN13
  9. {
  10. private DataTablem_EAN13= new DataTable();
  11. public EAN13()
  12. {
  13. m_EAN13.Columns.Add( "ID" );
  14. m_EAN13.Columns.Add( "Type" );
  15. m_EAN13.Columns.Add( "A" );
  16. m_EAN13.Columns.Add( "B" );
  17. m_EAN13.Columns.Add( "C" );
  18. m_EAN13.Rows.Add( "0" , "AAAAAA" , "0001101" , "0100111" , "1110010" );
  19. m_EAN13.Rows.Add( "1" , "AABABB" , "0011001" , "0110011" , "1100110" );
  20. m_EAN13.Rows.Add( "2" , "AABBAB" , "0010011" , "0011011" , "1101100" );
  21. m_EAN13.Rows.Add( "3" , "AABBBA" , "0111101" , "0100001" , "1000010" );
  22. m_EAN13.Rows.Add( "4" , "ABAABB" , "0100011" , "0011101" , "1011100" );
  23. m_EAN13.Rows.Add( "5" , "ABBAAB" , "0110001" , "0111001" , "1001110" );
  24. m_EAN13.Rows.Add( "6" , "ABBBAA" , "0101111" , "0000101" , "1010000" );
  25. m_EAN13.Rows.Add( "7" , "ABABAB" , "0111011" , "0010001" , "1000100" );
  26. m_EAN13.Rows.Add( "8" , "ABABBA" , "0110111" , "0001001" , "1001000" );
  27. m_EAN13.Rows.Add( "9" , "ABBABA" , "0001011" , "0010111" , "1110100" );
  28. }
  29. private uint m_Height=40;
  30. ///<summary>
  31. ///绘制高
  32. ///</summary>
  33. public uint Heigth{ get { return m_Height;} set {m_Height=value;}}
  34. private byte m_FontSize=0;
  35. ///<summary>
  36. ///字体大小(宋体)
  37. ///</summary>
  38. public byte FontSize{ get { return m_FontSize;} set {m_FontSize=value;}}
  39. private byte m_Magnify=0;
  40. ///<summary>
  41. ///放大系数
  42. ///</summary>
  43. public byte Magnify{ get { return m_Magnify;} set {m_Magnify=value;}}
  44. public BitmapGetCodeImage( string p_Text)
  45. {
  46. if (p_Text.Length!=13) throw new Exception( "数字不是13位!" );
  47. string _CodeText=p_Text.Remove(0,1);
  48. string _CodeIndex= "101" ;
  49. char []_LeftType=GetValue(p_Text.Substring(0,1), "Type" ).ToCharArray();
  50. for ( int i=0;i!=6;i++)
  51. {
  52. _CodeIndex+=GetValue(_CodeText.Substring(0,1),_LeftType[i].ToString());
  53. _CodeText=_CodeText.Remove(0,1);
  54. }
  55. _CodeIndex+= "01010" ;
  56. for ( int i=0;i!=6;i++)
  57. {
  58. _CodeIndex+=GetValue(_CodeText.Substring(0,1), "C" );
  59. _CodeText=_CodeText.Remove(0,1);
  60. }
  61. _CodeIndex+= "101" ;
  62. return GetImage(_CodeIndex,p_Text);
  63. }
  64. ///<summary>
  65. ///获取目标对应的数据
  66. ///</summary>
  67. ///<paramname="p_Code">编码</param>
  68. ///<paramname="p_Value">类型</param>
  69. ///<returns>编码</returns>
  70. private string GetValue( string p_Value, string p_Type)
  71. {
  72. if (m_EAN13== null ) return "" ;
  73. DataRow[]_Row=m_EAN13.Select( "ID='" +p_Value+ "'" );
  74. if (_Row.Length!=1) throw new Exception( "错误的编码" +p_Value.ToString());
  75. return _Row[0][p_Type].ToString();
  76. }
  77. ///<summary>
  78. ///绘制编码图形
  79. ///</summary>
  80. ///<paramname="p_Text">编码</param>
  81. ///<returns>图形</returns>
  82. private BitmapGetImage( string p_Text, string p_ViewText)
  83. {
  84. char []_Value=p_Text.ToCharArray();
  85. int _FontWidth=0;
  86. Font_MyFont= null ;
  87. if (m_FontSize!=0)
  88. {
  89. #region获取符合大小的字体
  90. _MyFont= new Font( "宋体" ,m_FontSize);
  91. Bitmap_MyFontBmp= new Bitmap(m_FontSize,m_FontSize);
  92. Graphics_FontGraphics=Graphics.FromImage(_MyFontBmp);
  93. for ( byte i=m_FontSize;i!=0;i--)
  94. {
  95. SizeF_DrawSize=_FontGraphics.MeasureString(p_ViewText.Substring(0,1),_MyFont);
  96. if (_DrawSize.Height>m_FontSize)
  97. {
  98. _MyFont= new Font( "宋体" ,i);
  99. }
  100. else
  101. {
  102. _FontWidth=( int )_DrawSize.Width;
  103. break ;
  104. }
  105. }
  106. #endregion
  107. }
  108. if (ScanDrawText(_MyFont,p_Text,_FontWidth)== false )
  109. {
  110. _FontWidth=0;
  111. m_FontSize=0;
  112. }
  113. //宽==需要绘制的数量*放大倍数+两个字的宽
  114. Bitmap_CodeImage= new Bitmap(_Value.Length*(( int )m_Magnify+1)+(_FontWidth*2),( int )m_Height);
  115. Graphics_Garphics=Graphics.FromImage(_CodeImage);
  116. _Garphics.FillRectangle(Brushes.White, new Rectangle(0,0,_CodeImage.Width,_CodeImage.Height));
  117. int _Height=0;
  118. int _LenEx=_FontWidth;
  119. for ( int i=0;i!=_Value.Length;i++)
  120. {
  121. int _DrawWidth=m_Magnify+1;
  122. if (i==0||i==2||i==46||i==48||i==92||i==94)
  123. {
  124. _Height=( int )m_Height;
  125. }
  126. else
  127. {
  128. _Height=( int )m_Height-m_FontSize;
  129. }
  130. if (_Value[i]== '1' )
  131. {
  132. _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx,0,_DrawWidth,_Height));
  133. }
  134. else
  135. {
  136. _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx,0,_DrawWidth,_Height));
  137. }
  138. _LenEx+=_DrawWidth;
  139. }
  140. //绘制文字
  141. if (_FontWidth!=0&&m_FontSize!=0)
  142. {
  143. int _StarX=0;
  144. int _StarY=( int )m_Height-_MyFont.Height;
  145. _Garphics.DrawString(p_ViewText.Substring(0,1),_MyFont,Brushes.Black,0,_StarY);
  146. _StarX=_FontWidth+(3*(m_Magnify+1));
  147. _Garphics.DrawString(p_ViewText.Substring(1,6),_MyFont,Brushes.Black,_StarX,_StarY);
  148. _StarX=_FontWidth+(50*(m_Magnify+1));
  149. _Garphics.DrawString(p_ViewText.Substring(7,6),_MyFont,Brushes.Black,_StarX,_StarY);
  150. }
  151. _Garphics.Dispose();
  152. return _CodeImage;
  153. }
  154. ///<summary>
  155. ///判断字体是否大与绘制图形
  156. ///</summary>
  157. ///<paramname="_MyFont">字体</param>
  158. ///<paramname="p_Text">文字</param>
  159. ///<paramname="p_Width">字体的宽</param>
  160. ///<returns>true可以绘制False不可以绘制</returns>
  161. private bool ScanDrawText(Font_MyFont, string p_Text, int p_Width)
  162. {
  163. if (_MyFont== null ) return false ;
  164. int _Width=(p_Text.Length-6-5)*(( int )m_Magnify+1);
  165. if ((p_Width*12)>_Width) return false ;
  166. return true ;
  167. }
  168. ///<summary>
  169. ///获得条码的最后一位(验证位)
  170. ///</summary>
  171. ///<paramname="ANumbers">条码</param>
  172. ///<returns></returns>
  173. public static char EAN13ISBN( string _Numb)
  174. {
  175. int _Sum=0;
  176. int _i=1; //权值
  177. foreach ( char _Char in _Numb)
  178. {
  179. if ( "0123456789" .IndexOf(_Char)<0) continue ; //非数字
  180. _Sum+=(_Char- '0' )*_i;
  181. _i=_i==1?3:1;
  182. }
  183. return "01234567890" [10-_Sum%10];
  184. }
  185. }
  186. }

剩下CODE39了 这个过几天再做把。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics