.NET 绘制 EAN13 (商品条码)

31 篇文章 1 订阅

 

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 DataTable m_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.       
  40.         private byte m_Magnify = 0;
  41.         /// <summary>     
  42.         /// 放大系数    
  43.         /// </summary>
  44.         public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
  45.         public Bitmap GetCodeImage(string p_Text)
  46.         {
  47.             if (p_Text.Length != 13) throw new Exception("数字不是13位!");
  48.             string _CodeText = p_Text.Remove(0, 1);
  49.             string _CodeIndex = "101";
  50.             char[] _LeftType = GetValue(p_Text.Substring(0, 1), "Type").ToCharArray();
  51.             for (int i = 0; i != 6; i++)
  52.             {
  53.                 _CodeIndex += GetValue(_CodeText.Substring(0, 1), _LeftType[i].ToString());
  54.                 _CodeText = _CodeText.Remove(0, 1);
  55.             }
  56.             
  57.             _CodeIndex += "01010";
  58.             
  59.             for (int i = 0; i != 6; i++)
  60.             {
  61.                 _CodeIndex += GetValue(_CodeText.Substring(0, 1), "C");
  62.                 _CodeText = _CodeText.Remove(0, 1);
  63.             }          
  64.             _CodeIndex += "101";
  65.             return GetImage(_CodeIndex, p_Text);
  66.         }
  67.         /// <summary>
  68.         /// 获取目标对应的数据
  69.         /// </summary>
  70.         /// <param name="p_Code">编码</param>
  71.         /// <param name="p_Value">类型</param>        
  72.         /// <returns>编码</returns>
  73.         private string GetValue(string p_Value, string p_Type)
  74.         {
  75.             if (m_EAN13 == nullreturn "";
  76.             DataRow[] _Row = m_EAN13.Select("ID='" + p_Value + "'");
  77.             if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString());
  78.             return _Row[0][p_Type].ToString();
  79.         }
  80.         /// <summary>
  81.         /// 绘制编码图形
  82.         /// </summary>
  83.         /// <param name="p_Text">编码</param>
  84.         /// <returns>图形</returns>
  85.         private Bitmap GetImage(string p_Text,string p_ViewText)
  86.         {
  87.             char[] _Value = p_Text.ToCharArray();
  88.             int _FontWidth = 0;
  89.             Font _MyFont = null;
  90.             if (m_FontSize != 0)
  91.             {
  92.                 #region 获取符合大小的字体
  93.                 _MyFont = new Font("宋体", m_FontSize);
  94.                 Bitmap _MyFontBmp = new Bitmap(m_FontSize, m_FontSize);
  95.                 Graphics _FontGraphics = Graphics.FromImage(_MyFontBmp);
  96.                 
  97.                 for (byte i = m_FontSize; i != 0; i--)
  98.                 {
  99.                     SizeF _DrawSize = _FontGraphics.MeasureString(p_ViewText.Substring(0, 1), _MyFont);
  100.                     if (_DrawSize.Height > m_FontSize)
  101.                     {
  102.                         _MyFont = new Font("宋体", i);
  103.                     }
  104.                     else
  105.                     {
  106.                         _FontWidth = (int)_DrawSize.Width;
  107.                         break;
  108.                     }
  109.                 }
  110.                 #endregion
  111.             }
  112.             if (ScanDrawText(_MyFont, p_Text, _FontWidth) == false)
  113.             {
  114.                 _FontWidth = 0;
  115.                 m_FontSize = 0;
  116.             }
  117.             //宽 == 需要绘制的数量*放大倍数 + 两个字的宽   
  118.             Bitmap _CodeImage = new Bitmap(_Value.Length * ((int)m_Magnify + 1) + (_FontWidth * 2), (int)m_Height);
  119.             Graphics _Garphics = Graphics.FromImage(_CodeImage);
  120.             _Garphics.FillRectangle(Brushes.White, new Rectangle(0, 0, _CodeImage.Width, _CodeImage.Height));                
  121.           
  122.             int _Height = 0;
  123.             int _LenEx = _FontWidth;
  124.             for (int i = 0; i != _Value.Length; i++)
  125.             {
  126.                 int _DrawWidth = m_Magnify + 1;
  127.                 if (i == 0 || i == 2 || i==46 || i==48 || i==92 || i==94)
  128.                 {
  129.                     _Height = (int)m_Height;                  
  130.                 }
  131.                 else
  132.                 {
  133.                     _Height = (int)m_Height - m_FontSize;
  134.                 }
  135.                 if (_Value[i] == '1')
  136.                 {
  137.                     _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _DrawWidth, _Height));
  138.                   
  139.                 }
  140.                 else
  141.                 {
  142.                     _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _DrawWidth, _Height));
  143.                 }
  144.                 _LenEx += _DrawWidth;              
  145.             }
  146.             //绘制文字
  147.             if (_FontWidth != 0 && m_FontSize != 0)
  148.             {
  149.                 int _StarX = 0;
  150.                 int _StarY=(int)m_Height - _MyFont.Height;
  151.                 _Garphics.DrawString(p_ViewText.Substring(0, 1), _MyFont, Brushes.Black, 0, _StarY);
  152.                 _StarX = _FontWidth + (3 * (m_Magnify + 1));
  153.                 _Garphics.DrawString(p_ViewText.Substring(1, 6), _MyFont, Brushes.Black, _StarX, _StarY);
  154.                 _StarX = _FontWidth + (50 * (m_Magnify + 1));
  155.                 _Garphics.DrawString(p_ViewText.Substring(7, 6), _MyFont, Brushes.Black, _StarX, _StarY);
  156.             }
  157.             _Garphics.Dispose();
  158.             return _CodeImage;
  159.         }
  160.         /// <summary>
  161.         /// 判断字体是否大与绘制图形
  162.         /// </summary>
  163.         /// <param name="_MyFont">字体</param>
  164.         /// <param name="p_Text">文字</param>
  165.         /// <param name="p_Width">字体的宽</param>
  166.         /// <returns>true可以绘制 False不可以绘制</returns>
  167.         private bool ScanDrawText(Font _MyFont,string p_Text,int p_Width)
  168.         {
  169.             if(_MyFont==null)return false;
  170.             int _Width = (p_Text.Length - 6 - 5) * ((int)m_Magnify + 1);
  171.             if ((p_Width*12) > _Width) return false;
  172.             return true;
  173.         }
  174.         /// <summary>
  175.         /// 获得条码的最后一位(验证位)
  176.         /// </summary>
  177.         /// <param name="ANumbers">条码</param>
  178.         /// <returns></returns>
  179.         public static char EAN13ISBN(string _Numb)
  180.         {
  181.             int _Sum = 0; 
  182.             int _i = 1;   //权值
  183.             foreach (char _Char in _Numb)
  184.             {
  185.                 if ("0123456789".IndexOf(_Char) < 0) continue// 非数字
  186.                 _Sum += (_Char - '0') * _i;
  187.                 _i = _i == 1 ? 3 : 1; 
  188.             }
  189.             return "01234567890"[10 - _Sum % 10];
  190.         }
  191.      
  192.     }
  193. }

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

条码生成Function EAN13(BM As String ) Private BM Dimension EAN_code[10,3] EAN_code[1,1]='00000011110011' EAN_code[1,2]='00110000111111' EAN_code[1,3]='11111100001100' EAN_code[2,1]='00001111000011' EAN_code[2,2]='00111100001111' EAN_code[2,3]='11110000111100' EAN_code[3,1]='00001100001111' EAN_code[3,2]='00001111001111' EAN_code[3,3]='11110011110000' EAN_code[4,1]='00111111001100' EAN_code[4,2]='00001111110011' EAN_code[4,3]='11000000001100' EAN_code[5,1]='00110000001111' EAN_code[5,2]='00001111110011' EAN_code[5,3]='11001111110000' EAN_code[6,1]='00111100000011' EAN_code[6,2]='00111111000011' EAN_code[6,3]='11000011111100' EAN_code[7,1]='00110011111111' EAN_code[7,2]='00000011001111' EAN_code[7,3]='11001100000000' EAN_code[8,1]='00111111001111' EAN_code[8,2]='00001100000011' EAN_code[8,3]='11000000110000' EAN_code[9,1]='00111100111111' EAN_code[9,2]='00000011000011' EAN_code[9,3]='11000011000000' EAN_code[10,1]='00000011001111' EAN_code[10,2]='00001100111111' EAN_code[10,3]='11111100110000' Dimension EAN_left[10] EAN_left[1]='111111' EAN_left[2]='112122' EAN_left[3]='112212' EAN_left[4]='112221' EAN_left[5]='121122' EAN_left[6]='122112' EAN_left[7]='122211' EAN_left[8]='121212' EAN_left[9]='121221' EAN_left[10]='122121' Dimension EAN_mode[8] Store '' To EAN_mode If Len(Alltrim(BM))12 .And. Val(BM)>0 Messagebox('EAN-13编码长度不规范',268,'信息提示') Return '' Else EAN_mode[1]='000000000000000000' EAN_mode[2]='110011' For i=0 To 9 If Val(Substr(BM,1,1))=i For ii=1 To 6 BMZ=Val(Substr(BM,ii+1,1)) MODE=Val(Substr(EAN_left[i+1],ii,1)) EAN_mode[3]=EAN_mode[3]+EAN_code[BMZ+1,MODE] Endf Endi Endf
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值