/// <summary> /// Ctor /// Initializes a new instance of the <see cref="Code128"/> class with value /// </summary> /// <param name="text"></param> public Code128(string text) : base(text, XSize.Empty, CodeDirection.LeftToRight) { this.code128Code = Code128Type.B; this.EndChar = '*'; this.StartChar = '*'; }
/// <summary> /// 获取目标对应的数据 /// </summary> /// <param name="code">编码</param> /// <param name="value">数值 A b 30</param> /// <param name="setId">返回编号</param> /// <returns>编码</returns> private string GetValue(Code128Type code, string value, ref int setId) { if (_code128 == null) { return(""); } DataRow[] row = _code128.Select(code.ToString() + "='" + value + "'"); if (row.Length != 1) { throw new Exception("错误的编码" + value.ToString()); } setId = Int32.Parse(row[0]["ID"].ToString()); return(row[0]["BandCode"].ToString()); }
private void InsertStartandCodeCharacters() { if (_code128Type != Code128Type.Auto) { switch (_code128Type) { case Code128Type.A: _formattedData.Insert(0, "START_A"); break; case Code128Type.B: _formattedData.Insert(0, "START_B"); break; case Code128Type.C: _formattedData.Insert(0, "START_C"); break; default: throw new Exception("EC128-4: Unknown start type in fixed type encoding."); } } else { var matchA = new Regex(@"^[\x00-\x5F\xC8-\xCF]*"); var matchB = new Regex(@"^[\x20-\x7F\xC8-\xCF]*"); var matchC = new Regex(@"^[0-9]*$"); if (matchC.Match(_data).Length >= 2) { _formattedData.Insert(0, "START_C"); _code128Type = Code128Type.C; } else if (matchA.Match(_data).Length >= matchB.Match(_data).Length) { _formattedData.Insert(0, "START_A"); _code128Type = Code128Type.A; } else { _formattedData.Insert(0, "START_B"); _code128Type = Code128Type.B; } } }
/// <summary> /// Initializes a new instance of the <see cref="Code128"/> class. /// Constructor /// </summary> public Code128() : base("", XSize.Empty, CodeDirection.LeftToRight) { if (Patterns == null) { Patterns = new Dictionary<Byte, Byte[]>(); Patterns.Add(0, new Byte[] { 2, 1, 2, 2, 2, 2 }); Patterns.Add(1, new Byte[] { 2, 2, 2, 1, 2, 2 }); Patterns.Add(2, new Byte[] { 2, 2, 2, 2, 2, 1 }); Patterns.Add(3, new Byte[] { 1, 2, 1, 2, 2, 3 }); Patterns.Add(4, new Byte[] { 1, 2, 1, 3, 2, 2 }); Patterns.Add(5, new Byte[] { 1, 3, 1, 2, 2, 2 }); Patterns.Add(6, new Byte[] { 1, 2, 2, 2, 1, 3 }); Patterns.Add(7, new Byte[] { 1, 2, 2, 3, 1, 2 }); Patterns.Add(8, new Byte[] { 1, 3, 2, 2, 1, 2 }); Patterns.Add(9, new Byte[] { 2, 2, 1, 2, 1, 3 }); Patterns.Add(10, new Byte[] { 2, 2, 1, 3, 1, 2 }); Patterns.Add(11, new Byte[] { 2, 3, 1, 2, 1, 2 }); Patterns.Add(12, new Byte[] { 1, 1, 2, 2, 3, 2 }); Patterns.Add(13, new Byte[] { 1, 2, 2, 1, 3, 2 }); Patterns.Add(14, new Byte[] { 1, 2, 2, 2, 3, 1 }); Patterns.Add(15, new Byte[] { 1, 1, 3, 2, 2, 2 }); Patterns.Add(16, new Byte[] { 1, 2, 3, 1, 2, 2 }); Patterns.Add(17, new Byte[] { 1, 2, 3, 2, 2, 1 }); Patterns.Add(18, new Byte[] { 2, 2, 3, 2, 1, 1 }); Patterns.Add(19, new Byte[] { 2, 2, 1, 1, 3, 2 }); Patterns.Add(20, new Byte[] { 2, 2, 1, 2, 3, 1 }); Patterns.Add(21, new Byte[] { 2, 1, 3, 2, 1, 2 }); Patterns.Add(22, new Byte[] { 2, 2, 3, 1, 1, 2 }); Patterns.Add(23, new Byte[] { 3, 1, 2, 1, 3, 1 }); Patterns.Add(24, new Byte[] { 3, 1, 1, 2, 2, 2 }); Patterns.Add(25, new Byte[] { 3, 2, 1, 1, 2, 2 }); Patterns.Add(26, new Byte[] { 3, 2, 1, 2, 2, 1 }); Patterns.Add(27, new Byte[] { 3, 1, 2, 2, 1, 2 }); Patterns.Add(28, new Byte[] { 3, 2, 2, 1, 1, 2 }); Patterns.Add(29, new Byte[] { 3, 2, 2, 2, 1, 1 }); Patterns.Add(30, new Byte[] { 2, 1, 2, 1, 2, 3 }); Patterns.Add(31, new Byte[] { 2, 1, 2, 3, 2, 1 }); Patterns.Add(32, new Byte[] { 2, 3, 2, 1, 2, 1 }); Patterns.Add(33, new Byte[] { 1, 1, 1, 3, 2, 3 }); Patterns.Add(34, new Byte[] { 1, 3, 1, 1, 2, 3 }); Patterns.Add(35, new Byte[] { 1, 3, 1, 3, 2, 1 }); Patterns.Add(36, new Byte[] { 1, 1, 2, 3, 1, 3 }); Patterns.Add(37, new Byte[] { 1, 3, 2, 1, 1, 3 }); Patterns.Add(38, new Byte[] { 1, 3, 2, 3, 1, 1 }); Patterns.Add(39, new Byte[] { 2, 1, 1, 3, 1, 3 }); Patterns.Add(40, new Byte[] { 2, 3, 1, 1, 1, 3 }); Patterns.Add(41, new Byte[] { 2, 3, 1, 3, 1, 1 }); Patterns.Add(42, new Byte[] { 1, 1, 2, 1, 3, 3 }); Patterns.Add(43, new Byte[] { 1, 1, 2, 3, 3, 1 }); Patterns.Add(44, new Byte[] { 1, 3, 2, 1, 3, 1 }); Patterns.Add(45, new Byte[] { 1, 1, 3, 1, 2, 3 }); Patterns.Add(46, new Byte[] { 1, 1, 3, 3, 2, 1 }); Patterns.Add(47, new Byte[] { 1, 3, 3, 1, 2, 1 }); Patterns.Add(48, new Byte[] { 3, 1, 3, 1, 2, 1 }); Patterns.Add(49, new Byte[] { 2, 1, 1, 3, 3, 1 }); Patterns.Add(50, new Byte[] { 2, 3, 1, 1, 3, 1 }); Patterns.Add(51, new Byte[] { 2, 1, 3, 1, 1, 3 }); Patterns.Add(52, new Byte[] { 2, 1, 3, 3, 1, 1 }); Patterns.Add(53, new Byte[] { 2, 1, 3, 1, 3, 1 }); Patterns.Add(54, new Byte[] { 3, 1, 1, 1, 2, 3 }); Patterns.Add(55, new Byte[] { 3, 1, 1, 3, 2, 1 }); Patterns.Add(56, new Byte[] { 3, 3, 1, 1, 2, 1 }); Patterns.Add(57, new Byte[] { 3, 1, 2, 1, 1, 3 }); Patterns.Add(58, new Byte[] { 3, 1, 2, 3, 1, 1 }); Patterns.Add(59, new Byte[] { 3, 3, 2, 1, 1, 1 }); Patterns.Add(60, new Byte[] { 3, 1, 4, 1, 1, 1 }); Patterns.Add(61, new Byte[] { 2, 2, 1, 4, 1, 1 }); Patterns.Add(62, new Byte[] { 4, 3, 1, 1, 1, 1 }); Patterns.Add(63, new Byte[] { 1, 1, 1, 2, 2, 4 }); Patterns.Add(64, new Byte[] { 1, 1, 1, 4, 2, 2 }); Patterns.Add(65, new Byte[] { 1, 2, 1, 1, 2, 4 }); Patterns.Add(66, new Byte[] { 1, 2, 1, 4, 2, 1 }); Patterns.Add(67, new Byte[] { 1, 4, 1, 1, 2, 2 }); Patterns.Add(68, new Byte[] { 1, 4, 1, 2, 2, 1 }); Patterns.Add(69, new Byte[] { 1, 1, 2, 2, 1, 4 }); Patterns.Add(70, new Byte[] { 1, 1, 2, 4, 1, 2 }); Patterns.Add(71, new Byte[] { 1, 2, 2, 1, 1, 4 }); Patterns.Add(72, new Byte[] { 1, 2, 2, 4, 1, 1 }); Patterns.Add(73, new Byte[] { 1, 4, 2, 1, 1, 2 }); Patterns.Add(74, new Byte[] { 1, 4, 2, 2, 1, 1 }); Patterns.Add(75, new Byte[] { 2, 4, 1, 2, 1, 1 }); Patterns.Add(76, new Byte[] { 2, 2, 1, 1, 1, 4 }); Patterns.Add(77, new Byte[] { 4, 1, 3, 1, 1, 1 }); Patterns.Add(78, new Byte[] { 2, 4, 1, 1, 1, 2 }); Patterns.Add(79, new Byte[] { 1, 3, 4, 1, 1, 1 }); Patterns.Add(80, new Byte[] { 1, 1, 1, 2, 4, 2 }); Patterns.Add(81, new Byte[] { 1, 2, 1, 1, 4, 2 }); Patterns.Add(82, new Byte[] { 1, 2, 1, 2, 4, 1 }); Patterns.Add(83, new Byte[] { 1, 1, 4, 2, 1, 2 }); Patterns.Add(84, new Byte[] { 1, 2, 4, 1, 1, 2 }); Patterns.Add(85, new Byte[] { 1, 2, 4, 2, 1, 1 }); Patterns.Add(86, new Byte[] { 4, 1, 1, 2, 1, 2 }); Patterns.Add(87, new Byte[] { 4, 2, 1, 1, 1, 2 }); Patterns.Add(88, new Byte[] { 4, 2, 1, 2, 1, 1 }); Patterns.Add(89, new Byte[] { 2, 1, 2, 1, 4, 1 }); Patterns.Add(90, new Byte[] { 2, 1, 4, 1, 2, 1 }); Patterns.Add(91, new Byte[] { 4, 1, 2, 1, 2, 1 }); Patterns.Add(92, new Byte[] { 1, 1, 1, 1, 4, 3 }); Patterns.Add(93, new Byte[] { 1, 1, 1, 3, 4, 1 }); Patterns.Add(94, new Byte[] { 1, 3, 1, 1, 4, 1 }); Patterns.Add(95, new Byte[] { 1, 1, 4, 1, 1, 3 }); Patterns.Add(96, new Byte[] { 1, 1, 4, 3, 1, 1 }); Patterns.Add(97, new Byte[] { 4, 1, 1, 1, 1, 3 }); Patterns.Add(98, new Byte[] { 4, 1, 1, 3, 1, 1 }); Patterns.Add(99, new Byte[] { 1, 1, 3, 1, 4, 1 }); Patterns.Add(100, new Byte[] { 1, 1, 4, 1, 3, 1 }); Patterns.Add(101, new Byte[] { 3, 1, 1, 1, 4, 1 }); Patterns.Add(102, new Byte[] { 4, 1, 1, 1, 3, 1 }); Patterns.Add(103, new Byte[] { 2, 1, 1, 4, 1, 2 }); Patterns.Add(104, new Byte[] { 2, 1, 1, 2, 1, 4 }); Patterns.Add(105, new Byte[] { 2, 1, 1, 2, 3, 2 }); Patterns.Add(106, new Byte[] { 2, 3, 3, 1, 1, 1, 2 }); } this.code128Code = Code128Type.B; }
/// <summary> /// Encodes data. /// </summary> /// <param name="data">Data to encode.</param> /// <param name="code128Type">Type of encoding to lock to. (Code 128A, Code 128B, Code 128C)</param> public Code128(string data, Code128Type code128Type) { _code128Type = code128Type; _data = data; }
/// <summary> /// 获取Code128条形码图片 /// </summary> /// <param name="content">条码内容</param> /// <param name="type">条码类型</param> /// <returns>Code128条形码</returns> public Bitmap GetImage(string content, Code128Type type) { string viewText = content; string text = ""; IList <int> textNumb = new List <int>(); int examine = 0;//首位 switch (type) { case Code128Type.Code128C: examine = 105; if ((content.Length & 1) != 0) { throw new Exception("128C长度必须是偶数"); } while (content.Length != 0) { int temp = 0; if (!long.TryParse(content.Substring(0, 2), out _)) { throw new Exception("128C必须是数字!"); } text += GetValue(type, content.Substring(0, 2), ref temp); textNumb.Add(temp); content = content.Remove(0, 2); } break; case Code128Type.EAN128: examine = 105; if ((content.Length & 1) != 0) { throw new Exception("EAN128长度必须是偶数"); } textNumb.Add(102); text += "411131"; while (content.Length != 0) { int temp = 0; if (!long.TryParse(content.Substring(0, 2), out _)) { throw new Exception("EAN128必须是数字!"); } text += GetValue(Code128Type.Code128C, content.Substring(0, 2), ref temp); textNumb.Add(temp); content = content.Remove(0, 2); } break; default: examine = type == Code128Type.Code128A ? 103 : 104; while (content.Length != 0) { int temp = 0; string valueCode = GetValue(type, content.Substring(0, 1), ref temp); if (valueCode.Length == 0) { throw new Exception("无效的字符集!" + content.Substring(0, 1)); } text += valueCode; textNumb.Add(temp); content = content.Remove(0, 1); } break; } if (textNumb.Count == 0) { throw new Exception("错误的编码,无数据"); } text = text.Insert(0, GetValue(examine));//获取开始位 for (int i = 0; i != textNumb.Count; i++) { examine += textNumb[i] * (i + 1); } examine = examine % 103; //获得严效位 text += GetValue(examine); //获取严效位 text += "2331112"; //结束位 Bitmap image = GetImage(text); GetViewText(image, viewText); return(image); }