条形码Code128源代码
public class Code128
{
private DataTable m_Code128 = new DataTable();
private uint m_Height = ;
/// <summary>
/// 高度
/// </summary>
public uint Height { get { return m_Height; } set { m_Height = value; } }
private Font m_ValueFont = null;
/// <summary>
/// 是否显示可见号码 如果为NULL不显示号码
/// </summary>
public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } }
private byte m_Magnify = ;
/// <summary>
/// 放大倍数
/// </summary>
public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } }
/// <summary>
/// 条码类别
/// </summary>
public enum Encode
{
Code128A,
Code128B,
Code128C,
EAN128
}
public Code128()
{
m_Code128.Columns.Add("ID");
m_Code128.Columns.Add("Code128A");
m_Code128.Columns.Add("Code128B");
m_Code128.Columns.Add("Code128C");
m_Code128.Columns.Add("BandCode");
m_Code128.CaseSensitive = true;
#region 数据表
m_Code128.Rows.Add("", " ", " ", "", "");
m_Code128.Rows.Add("", "!", "!", "", "");
m_Code128.Rows.Add("", "\"", "\"", "", "");
m_Code128.Rows.Add("", "#", "#", "", "");
m_Code128.Rows.Add("", "$", "$", "", "");
m_Code128.Rows.Add("", "%", "%", "", "");
m_Code128.Rows.Add("", "&", "&", "", "");
m_Code128.Rows.Add("", "'", "'", "", "");
m_Code128.Rows.Add("", "(", "(", "", "");
m_Code128.Rows.Add("", ")", ")", "", "");
m_Code128.Rows.Add("", "*", "*", "", "");
m_Code128.Rows.Add("", "+", "+", "", "");
m_Code128.Rows.Add("", ",", ",", "", "");
m_Code128.Rows.Add("", "-", "-", "", "");
m_Code128.Rows.Add("", ".", ".", "", "");
m_Code128.Rows.Add("", "/", "/", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", "", "", "", "");
m_Code128.Rows.Add("", ":", ":", "", "");
m_Code128.Rows.Add("", ";", ";", "", "");
m_Code128.Rows.Add("", "<", "<", "", "");
m_Code128.Rows.Add("", "=", "=", "", "");
m_Code128.Rows.Add("", ">", ">", "", "");
m_Code128.Rows.Add("", "?", "?", "", "");
m_Code128.Rows.Add("", "@", "@", "", "");
m_Code128.Rows.Add("", "A", "A", "", "");
m_Code128.Rows.Add("", "B", "B", "", "");
m_Code128.Rows.Add("", "C", "C", "", "");
m_Code128.Rows.Add("", "D", "D", "", "");
m_Code128.Rows.Add("", "E", "E", "", "");
m_Code128.Rows.Add("", "F", "F", "", "");
m_Code128.Rows.Add("", "G", "G", "", "");
m_Code128.Rows.Add("", "H", "H", "", "");
m_Code128.Rows.Add("", "I", "I", "", "");
m_Code128.Rows.Add("", "J", "J", "", "");
m_Code128.Rows.Add("", "K", "K", "", "");
m_Code128.Rows.Add("", "L", "L", "", "");
m_Code128.Rows.Add("", "M", "M", "", "");
m_Code128.Rows.Add("", "N", "N", "", "");
m_Code128.Rows.Add("", "O", "O", "", "");
m_Code128.Rows.Add("", "P", "P", "", "");
m_Code128.Rows.Add("", "Q", "Q", "", "");
m_Code128.Rows.Add("", "R", "R", "", "");
m_Code128.Rows.Add("", "S", "S", "", "");
m_Code128.Rows.Add("", "T", "T", "", "");
m_Code128.Rows.Add("", "U", "U", "", "");
m_Code128.Rows.Add("", "V", "V", "", "");
m_Code128.Rows.Add("", "W", "W", "", "");
m_Code128.Rows.Add("", "X", "X", "", "");
m_Code128.Rows.Add("", "Y", "Y", "", "");
m_Code128.Rows.Add("", "Z", "Z", "", "");
m_Code128.Rows.Add("", "[", "[", "", "");
m_Code128.Rows.Add("", "\\", "\\", "", "");
m_Code128.Rows.Add("", "]", "]", "", "");
m_Code128.Rows.Add("", "^", "^", "", "");
m_Code128.Rows.Add("", "_", "_", "", "");
m_Code128.Rows.Add("", "NUL", "`", "", "");
m_Code128.Rows.Add("", "SOH", "a", "", "");
m_Code128.Rows.Add("", "STX", "b", "", "");
m_Code128.Rows.Add("", "ETX", "c", "", "");
m_Code128.Rows.Add("", "EOT", "d", "", "");
m_Code128.Rows.Add("", "ENQ", "e", "", "");
m_Code128.Rows.Add("", "ACK", "f", "", "");
m_Code128.Rows.Add("", "BEL", "g", "", "");
m_Code128.Rows.Add("", "BS", "h", "", "");
m_Code128.Rows.Add("", "HT", "i", "", "");
m_Code128.Rows.Add("", "LF", "j", "", "");
m_Code128.Rows.Add("", "VT", "k", "", "");
m_Code128.Rows.Add("", "FF", "I", "", "");
m_Code128.Rows.Add("", "CR", "m", "", "");
m_Code128.Rows.Add("", "SO", "n", "", "");
m_Code128.Rows.Add("", "SI", "o", "", "");
m_Code128.Rows.Add("", "DLE", "p", "", "");
m_Code128.Rows.Add("", "DC1", "q", "", "");
m_Code128.Rows.Add("", "DC2", "r", "", "");
m_Code128.Rows.Add("", "DC3", "s", "", "");
m_Code128.Rows.Add("", "DC4", "t", "", "");
m_Code128.Rows.Add("", "NAK", "u", "", "");
m_Code128.Rows.Add("", "SYN", "v", "", "");
m_Code128.Rows.Add("", "ETB", "w", "", "");
m_Code128.Rows.Add("", "CAN", "x", "", "");
m_Code128.Rows.Add("", "EM", "y", "", "");
m_Code128.Rows.Add("", "SUB", "z", "", "");
m_Code128.Rows.Add("", "ESC", "{", "", "");
m_Code128.Rows.Add("", "FS", "|", "", "");
m_Code128.Rows.Add("", "GS", "}", "", "");
m_Code128.Rows.Add("", "RS", "~", "", "");
m_Code128.Rows.Add("", "US", "DEL", "", "");
m_Code128.Rows.Add("", "FNC3", "FNC3", "", "");
m_Code128.Rows.Add("", "FNC2", "FNC2", "", "");
m_Code128.Rows.Add("", "SHIFT", "SHIFT", "", "");
m_Code128.Rows.Add("", "CODEC", "CODEC", "", "");
m_Code128.Rows.Add("", "CODEB", "FNC4", "CODEB", "");
m_Code128.Rows.Add("", "FNC4", "CODEA", "CODEA", "");
m_Code128.Rows.Add("", "FNC1", "FNC1", "FNC1", "");
m_Code128.Rows.Add("", "StartA", "StartA", "StartA", "");
m_Code128.Rows.Add("", "StartB", "StartB", "StartB", "");
m_Code128.Rows.Add("", "StartC", "StartC", "StartC", "");
m_Code128.Rows.Add("", "Stop", "Stop", "Stop", "");
#endregion
}
/// <summary>
/// 获取128图形
/// </summary>
/// <param name="p_Text">文字</param>
/// <param name="p_Code">编码</param>
/// <returns>图形</returns>
public Bitmap GetCodeImage(string p_Text, Encode p_Code)
{
string _ViewText = p_Text;
string _Text = "";
IList<int> _TextNumb = new List<int>();
int _Examine = ; //首位
switch (p_Code)
{
case Encode.Code128C:
_Examine = ;
if (!((p_Text.Length & ) == )) throw new Exception("128C长度必须是偶数");
while (p_Text.Length != )
{
int _Temp = ;
try
{
int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
_Text += GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
_TextNumb.Add(_Temp);
p_Text = p_Text.Remove(, );
}
break;
case Encode.EAN128:
_Examine = ;
if (!((p_Text.Length & ) == )) throw new Exception("EAN128长度必须是偶数");
_TextNumb.Add();
_Text += "";
while (p_Text.Length != )
{
int _Temp = ;
try
{
int _CodeNumb128 = Int32.Parse(p_Text.Substring(, ));
}
catch
{
throw new Exception("128C必须是数字!");
}
_Text += GetValue(Encode.Code128C, p_Text.Substring(, ), ref _Temp);
_TextNumb.Add(_Temp);
p_Text = p_Text.Remove(, );
}
break;
default:
if (p_Code == Encode.Code128A)
{
_Examine = ;
}
else
{
_Examine = ;
} while (p_Text.Length != )
{
int _Temp = ;
string _ValueCode = GetValue(p_Code, p_Text.Substring(, ), ref _Temp);
if (_ValueCode.Length == ) throw new Exception("无效的字符集!" + p_Text.Substring(, ).ToString());
_Text += _ValueCode;
_TextNumb.Add(_Temp);
p_Text = p_Text.Remove(, );
}
break;
}
if (_TextNumb.Count == ) throw new Exception("错误的编码,无数据");
_Text = _Text.Insert(, GetValue(_Examine)); //获取开始位 for (int i = ; i != _TextNumb.Count; i++)
{
_Examine += _TextNumb[i] * (i + );
}
_Examine = _Examine % ; //获得严效位
_Text += GetValue(_Examine); //获取严效位
_Text += ""; //结束位
Bitmap _CodeImage = GetImage(_Text);
GetViewText(_CodeImage, _ViewText);
return _CodeImage;
}
/// <summary>
/// 获取目标对应的数据
/// </summary>
/// <param name="p_Code">编码</param>
/// <param name="p_Value">数值 A b 30</param>
/// <param name="p_SetID">返回编号</param>
/// <returns>编码</returns>
private string GetValue(Encode p_Code, string p_Value, ref int p_SetID)
{
if (m_Code128 == null) return "";
DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'");
if (_Row.Length != ) throw new Exception("错误的编码" + p_Value.ToString());
p_SetID = Int32.Parse(_Row[]["ID"].ToString());
return _Row[]["BandCode"].ToString();
}
/// <summary>
/// 根据编号获得条纹
/// </summary>
/// <param name="p_CodeId"></param>
/// <returns></returns>
private string GetValue(int p_CodeId)
{
DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'");
if (_Row.Length != ) throw new Exception("验效位的编码错误" + p_CodeId.ToString());
return _Row[]["BandCode"].ToString();
}
/// <summary>
/// 获得条码图形
/// </summary>
/// <param name="p_Text">文字</param>
/// <returns>图形</returns>
private Bitmap GetImage(string p_Text)
{
char[] _Value = p_Text.ToCharArray();
int _Width = ;
for (int i = ; i != _Value.Length; i++)
{
_Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + );
}
Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height);
Graphics _Garphics = Graphics.FromImage(_CodeImage);
//Pen _Pen;
int _LenEx = ;
for (int i = ; i != _Value.Length; i++)
{
int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + ); //获取宽和放大系数
if (!((i & ) == ))
{
//_Pen = new Pen(Brushes.White, _ValueNumb);
_Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
}
else
{
//_Pen = new Pen(Brushes.Black, _ValueNumb);
_Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, , _ValueNumb, (int)m_Height));
}
//_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
_LenEx += _ValueNumb;
}
_Garphics.Dispose();
return _CodeImage;
}
/// <summary>
/// 显示可见条码文字 如果小于40 不显示文字
/// </summary>
/// <param name="p_Bitmap">图形</param>
private void GetViewText(Bitmap p_Bitmap, string p_ViewText)
{
if (m_ValueFont == null) return; Graphics _Graphics = Graphics.FromImage(p_Bitmap);
SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont);
if (_DrawSize.Height > p_Bitmap.Height - || _DrawSize.Width > p_Bitmap.Width)
{
_Graphics.Dispose();
return;
} int _StarX = p_Bitmap.Width - (int)_DrawSize.Width;
int _StarY = p_Bitmap.Height - (int)_DrawSize.Height;
_Graphics.FillRectangle(Brushes.White, new Rectangle(, _StarY, p_Bitmap.Width, (int)_DrawSize.Height));
_Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, _StarX / , _StarY);//文字在图片的中间位置
} //12345678
//(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47
//结果为starc +12 +34 +56 +78 +47 +end internal Image GetCodeImage(string p)
{
throw new NotImplementedException();
}
}
条形码Code128源代码的更多相关文章
- C#生成条形码 Code128算法
条形有很多种,Code128是比较常用的一种,是一种高密度条码, CODE128 码可表示从 ASCII 0 到ASCII 127 共128个字符,故称128码.其中包含了数字.字母和符号字符. Co ...
- 基于ASP.NET生成条形码(code128)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- iOS 原生库(AVFoundation)实现二维码扫描,封装的工具类,不依赖第三方库,可高度自定义扫描动画及界面(Swift 4.0)
Create QRScanner.swift file // // QRScanner.swift // NativeQR // // Created by Harvey on 2017/10/24. ...
- 利用Code128字体将文本转换为code128条形码
利用Code128字体将文本转换为code128条形码[转] 最近在做仓储的项目,许多的打印文件都包含条形码,之前一直使用C39P24DhTt字体直接转换为39码,但是最近要求使用code128编 ...
- 如何看懂Code128条形码
条形码就是我们看到的商品上有的那些竖条条. 要不是项目上用到这个或许我一辈子也不会对那个感兴趣. 条形码其实是分成很多类的,虽然他们看起来都差不多…… 常见的条形码的码制被称为39码.128码.417 ...
- ean128与code128 条形码 算法分析
[code128条形码组成] 除终止符(STOP)由13个模块组成外,其他字符均由11个模块组成 就是说,如果用‘1’表示黑线(实模块),用‘0’表示白线(空模块),那么每表示一个字符就需要11条线, ...
- 教你看懂Code128条形码
首 页 条码控件 条码技术 条码新闻 合作伙伴 联系我们 常见问题 电话:010-84827961 当前位置:条形码控件网 > 条形码控件技术文章 > >正文 教你看懂C ...
- Code128条形码如何计算其宽度?如何得出其校验位?
原文链接 Code128条形码是一个非常高密的字母数字条码,能够存储需要的编码数据,它可以编码所有128个ASCII码字符,它使用最少的空间. 在Code128符号体系中,每个数据字符编码都是由11个 ...
- C# vb .NET读取识别条形码线性条码code128
code128是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准 ...
随机推荐
- MapReduce原理讲解
简介 本文主要介绍MapReduce V2的基本原理, 也是笔者在学习MR的学习笔记整理. 本文首先大概介绍下MRV2的客户端跟服务器交互的两个协议, 然后着重介绍MRV2的核心模块MRAppMast ...
- Ejection chain 与交错路
相关文献: Rego, C. (1998). "A Subpath Ejection Method for the Vehicle Routing Problem." Manage ...
- 琐碎-将hadoop源码作为工程导入eclipse
之前写过如何用eclipse看hadoop源码,虽然非官方版的,但是可以达到目的,最重要是简单方便快速 官方版(hadoop2.2.0)的也有: 源码目录为: 和之前的源码目录有很大的不同 编译的时候 ...
- SelectionKey理解(总结)
SelectKey注册了写事件,不在合适的时间去除掉,会一直触发写事件,因为写事件是代码触发的 client.register(selector, SelectionKey.OP_WRITE); 或者 ...
- git workflows
https://www.atlassian.com/git/tutorials/comparing-workflows Comparing Workflows The array of possibl ...
- java中的静态方法
静态方法是属于类的,内存必须为它分配内存空间,这个空间一直由静态方法占用,内存管理器不会由于静态方法没有被调用而将静态方法的存储空间收回,这样如果将所有的方法都声明为静态方法,就会占用大量的内存空间, ...
- Laravel 中查询 where 记录
Laravel 的 Eloquent 使用 Between $query->whereBetween('age',[$from,$to]) 这是生成 And between ... and .. ...
- Jersey(1.19.1) - Hello World, Get started with Jersey using the embedded Grizzly server
Maven Dependencies The following Maven dependencies need to be added to the pom: <dependency> ...
- Jersey(1.19.1) - Sub-resources
@Path may be used on classes and such classes are referred to as root resource classes. @Path may al ...
- Git CMD - merge: Join two or more development histories together
命令格式 git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit] [-s <strategy>] [-X <st ...