<经验杂谈>C#生成条形码

虽然二维码满天飞,但也不能忘了条形码,本篇介绍可以在C#中使用的1D/2D编码解码器。条形码的应用已经非常普遍,几乎所有超市里面的商品上面都印有条形码;

条形码的标准

条形码的标准有ENA条形码、UPC条形码、二五条形码、交叉二五条形码、库德巴条形码、三九条形码和128条形码等,而商品上最常使用的就是EAN商品条形码。EAN商品条形码亦称通用商品条形码,由国际物品编码协会制定,通用于世界各地,是目前国际上使用最广泛的一种商品条形码。我国目前在国内推行使用的也是这种商品条形码。EAN商品条形码分为EAN-13(标准版)和EAN-8(缩短版)两种。以下是C#生成条形码的类库:

public class BarCodeHelper
    {
        public class Code128
        {
            private readonly DataTable _mCode128 = new DataTable();
            private uint _mHeight = 40;

            /// <summary>
            /// 高度
            /// </summary>
            public uint Height { get { return _mHeight; } set { _mHeight = value; } }
            private Font _mValueFont;

            /// <summary>
            /// 是否显示可见号码  如果为NULL不显示号码
            /// </summary>
            public Font ValueFont { get { return _mValueFont; } set { _mValueFont = value; } }
            private byte _mMagnify;

            /// <summary>
            /// 放大倍数
            /// </summary>
            public byte Magnify { get { return _mMagnify; } set { _mMagnify = value; } }

            /// <summary>
            /// 条码类别
            /// </summary>
            public enum Encode
            {
                Code128A,
                Code128B,
                Code128C,
                Ean128
            }

            public Code128()
            {
                _mCode128.Columns.Add("ID");
                _mCode128.Columns.Add("Code128A");
                _mCode128.Columns.Add("Code128B");
                _mCode128.Columns.Add("Code128C");
                _mCode128.Columns.Add("BandCode");
                _mCode128.CaseSensitive = true;
                #region 数据表
                _mCode128.Rows.Add("0", " ", " ", "00", "212222");
                _mCode128.Rows.Add("1", "!", "!", "01", "222122");
                _mCode128.Rows.Add("2", "\"", "\"", "02", "222221");
                _mCode128.Rows.Add("3", "#", "#", "03", "121223");
                _mCode128.Rows.Add("4", "$", "$", "04", "121322");
                _mCode128.Rows.Add("5", "%", "%", "05", "131222");
                _mCode128.Rows.Add("6", "&", "&", "06", "122213");
                _mCode128.Rows.Add("7", "‘", "‘", "07", "122312");
                _mCode128.Rows.Add("8", "(", "(", "08", "132212");
                _mCode128.Rows.Add("9", ")", ")", "09", "221213");
                _mCode128.Rows.Add("10", "*", "*", "10", "221312");
                _mCode128.Rows.Add("11", "+", "+", "11", "231212");
                _mCode128.Rows.Add("12", ",", ",", "12", "112232");
                _mCode128.Rows.Add("13", "-", "-", "13", "122132");
                _mCode128.Rows.Add("14", ".", ".", "14", "122231");
                _mCode128.Rows.Add("15", "/", "/", "15", "113222");
                _mCode128.Rows.Add("16", "0", "0", "16", "123122");
                _mCode128.Rows.Add("17", "1", "1", "17", "123221");
                _mCode128.Rows.Add("18", "2", "2", "18", "223211");
                _mCode128.Rows.Add("19", "3", "3", "19", "221132");
                _mCode128.Rows.Add("20", "4", "4", "20", "221231");
                _mCode128.Rows.Add("21", "5", "5", "21", "213212");
                _mCode128.Rows.Add("22", "6", "6", "22", "223112");
                _mCode128.Rows.Add("23", "7", "7", "23", "312131");
                _mCode128.Rows.Add("24", "8", "8", "24", "311222");
                _mCode128.Rows.Add("25", "9", "9", "25", "321122");
                _mCode128.Rows.Add("26", ":", ":", "26", "321221");
                _mCode128.Rows.Add("27", ";", ";", "27", "312212");
                _mCode128.Rows.Add("28", "<", "<", "28", "322112");
                _mCode128.Rows.Add("29", "=", "=", "29", "322211");
                _mCode128.Rows.Add("30", ">", ">", "30", "212123");
                _mCode128.Rows.Add("31", "?", "?", "31", "212321");
                _mCode128.Rows.Add("32", "@", "@", "32", "232121");
                _mCode128.Rows.Add("33", "A", "A", "33", "111323");
                _mCode128.Rows.Add("34", "B", "B", "34", "131123");
                _mCode128.Rows.Add("35", "C", "C", "35", "131321");
                _mCode128.Rows.Add("36", "D", "D", "36", "112313");
                _mCode128.Rows.Add("37", "E", "E", "37", "132113");
                _mCode128.Rows.Add("38", "F", "F", "38", "132311");
                _mCode128.Rows.Add("39", "G", "G", "39", "211313");
                _mCode128.Rows.Add("40", "H", "H", "40", "231113");
                _mCode128.Rows.Add("41", "I", "I", "41", "231311");
                _mCode128.Rows.Add("42", "J", "J", "42", "112133");
                _mCode128.Rows.Add("43", "K", "K", "43", "112331");
                _mCode128.Rows.Add("44", "L", "L", "44", "132131");
                _mCode128.Rows.Add("45", "M", "M", "45", "113123");
                _mCode128.Rows.Add("46", "N", "N", "46", "113321");
                _mCode128.Rows.Add("47", "O", "O", "47", "133121");
                _mCode128.Rows.Add("48", "P", "P", "48", "313121");
                _mCode128.Rows.Add("49", "Q", "Q", "49", "211331");
                _mCode128.Rows.Add("50", "R", "R", "50", "231131");
                _mCode128.Rows.Add("51", "S", "S", "51", "213113");
                _mCode128.Rows.Add("52", "T", "T", "52", "213311");
                _mCode128.Rows.Add("53", "U", "U", "53", "213131");
                _mCode128.Rows.Add("54", "V", "V", "54", "311123");
                _mCode128.Rows.Add("55", "W", "W", "55", "311321");
                _mCode128.Rows.Add("56", "X", "X", "56", "331121");
                _mCode128.Rows.Add("57", "Y", "Y", "57", "312113");
                _mCode128.Rows.Add("58", "Z", "Z", "58", "312311");
                _mCode128.Rows.Add("59", "[", "[", "59", "332111");
                _mCode128.Rows.Add("60", "\\", "\\", "60", "314111");
                _mCode128.Rows.Add("61", "]", "]", "61", "221411");
                _mCode128.Rows.Add("62", "^", "^", "62", "431111");
                _mCode128.Rows.Add("63", "_", "_", "63", "111224");
                _mCode128.Rows.Add("64", "NUL", "`", "64", "111422");
                _mCode128.Rows.Add("65", "SOH", "a", "65", "121124");
                _mCode128.Rows.Add("66", "STX", "b", "66", "121421");
                _mCode128.Rows.Add("67", "ETX", "c", "67", "141122");
                _mCode128.Rows.Add("68", "EOT", "d", "68", "141221");
                _mCode128.Rows.Add("69", "ENQ", "e", "69", "112214");
                _mCode128.Rows.Add("70", "ACK", "f", "70", "112412");
                _mCode128.Rows.Add("71", "BEL", "g", "71", "122114");
                _mCode128.Rows.Add("72", "BS", "h", "72", "122411");
                _mCode128.Rows.Add("73", "HT", "i", "73", "142112");
                _mCode128.Rows.Add("74", "LF", "j", "74", "142211");
                _mCode128.Rows.Add("75", "VT", "k", "75", "241211");
                _mCode128.Rows.Add("76", "FF", "I", "76", "221114");
                _mCode128.Rows.Add("77", "CR", "m", "77", "413111");
                _mCode128.Rows.Add("78", "SO", "n", "78", "241112");
                _mCode128.Rows.Add("79", "SI", "o", "79", "134111");
                _mCode128.Rows.Add("80", "DLE", "p", "80", "111242");
                _mCode128.Rows.Add("81", "DC1", "q", "81", "121142");
                _mCode128.Rows.Add("82", "DC2", "r", "82", "121241");
                _mCode128.Rows.Add("83", "DC3", "s", "83", "114212");
                _mCode128.Rows.Add("84", "DC4", "t", "84", "124112");
                _mCode128.Rows.Add("85", "NAK", "u", "85", "124211");
                _mCode128.Rows.Add("86", "SYN", "v", "86", "411212");
                _mCode128.Rows.Add("87", "ETB", "w", "87", "421112");
                _mCode128.Rows.Add("88", "CAN", "x", "88", "421211");
                _mCode128.Rows.Add("89", "EM", "y", "89", "212141");
                _mCode128.Rows.Add("90", "SUB", "z", "90", "214121");
                _mCode128.Rows.Add("91", "ESC", "{", "91", "412121");
                _mCode128.Rows.Add("92", "FS", "|", "92", "111143");
                _mCode128.Rows.Add("93", "GS", "}", "93", "111341");
                _mCode128.Rows.Add("94", "RS", "~", "94", "131141");
                _mCode128.Rows.Add("95", "US", "DEL", "95", "114113");
                _mCode128.Rows.Add("96", "FNC3", "FNC3", "96", "114311");
                _mCode128.Rows.Add("97", "FNC2", "FNC2", "97", "411113");
                _mCode128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311");
                _mCode128.Rows.Add("99", "CODEC", "CODEC", "99", "113141");
                _mCode128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131");
                _mCode128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141");
                _mCode128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131");
                _mCode128.Rows.Add("103", "StartA", "StartA", "StartA", "211412");
                _mCode128.Rows.Add("104", "StartB", "StartB", "StartB", "211214");
                _mCode128.Rows.Add("105", "StartC", "StartC", "StartC", "211232");
                _mCode128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112");
                #endregion
            }

            /// <summary>
            /// 获取128图形
            /// </summary>
            /// <param name="pText">文字</param>
            /// <param name="pCode">编码</param>
            /// <returns>图形</returns>
            public Bitmap GetCodeImage(string pText, Encode pCode)
            {
                string viewText = pText;
                string text = "";
                IList<int> textNumb = new List<int>();
                int examine;    //首位
                switch (pCode)
                {
                    case Encode.Code128C:
                        examine = 105;
                        if ((pText.Length & 1) != 0) throw new Exception("128C长度必须是偶数");
                        while (pText.Length != 0)
                        {
                            var temp = 0;
                            try
                            {
                                var codeNumb128 = Int32.Parse(pText.Substring(0, 2));
                            }
                            catch
                            {
                                throw new Exception("128C必须是数字!");
                            }
                            text += GetValue(pCode, pText.Substring(0, 2), ref temp);
                            textNumb.Add(temp);
                            pText = pText.Remove(0, 2);
                        }
                        break;
                    case Encode.Ean128:
                        examine = 105;
                        if ((pText.Length & 1) != 0) throw new Exception("EAN128长度必须是偶数");
                        textNumb.Add(102);
                        text += "411131";
                        while (pText.Length != 0)
                        {
                            var temp = 0;
                            try
                            {
                                var codeNumb128 = Int32.Parse(pText.Substring(0, 2));
                            }
                            catch
                            {
                                throw new Exception("128C必须是数字!");
                            }
                            text += GetValue(Encode.Code128C, pText.Substring(0, 2), ref temp);
                            textNumb.Add(temp);
                            pText = pText.Remove(0, 2);
                        }
                        break;
                    default:
                        examine = pCode == Encode.Code128A ? 103 : 104;

                        while (pText.Length != 0)
                        {
                            var temp = 0;
                            var valueCode = GetValue(pCode, pText.Substring(0, 1), ref temp);
                            if (valueCode.Length == 0) throw new Exception("无效的字符集!" + pText.Substring(0, 1));
                            text += valueCode;
                            textNumb.Add(temp);
                            pText = pText.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"; //结束位
                var codeImage = GetImage(text);
                GetViewText(codeImage, viewText);
                return codeImage;
            }

            /// <summary>
            /// 获取目标对应的数据
            /// </summary>
            /// <param name="pCode">编码</param>
            /// <param name="pValue">数值 A b  30</param>
            /// <param name="pSetId">返回编号</param>
            /// <returns>编码</returns>
            private string GetValue(Encode pCode, string pValue, ref int pSetId)
            {
                if (_mCode128 == null) return "";
                var row = _mCode128.Select(pCode + "=‘" + pValue + "‘");
                if (row.Length != 1) throw new Exception("错误的编码" + pValue);
                pSetId = Int32.Parse(row[0]["ID"].ToString());
                return row[0]["BandCode"].ToString();
            }

            /// <summary>
            /// 根据编号获得条纹
            /// </summary>
            /// <param name="pCodeId"></param>
            /// <returns></returns>
            private string GetValue(int pCodeId)
            {
                var row = _mCode128.Select("ID=‘" + pCodeId + "‘");
                if (row.Length != 1) throw new Exception("验效位的编码错误" + pCodeId);
                return row[0]["BandCode"].ToString();
            }

            /// <summary>
            /// 获得条码图形
            /// </summary>
            /// <param name="pText">文字</param>
            /// <returns>图形</returns>
            private Bitmap GetImage(string pText)
            {
                var value = pText.ToCharArray();
                var width = 0;
                for (int i = 0; i != value.Length; i++)
                {
                    width += Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + 1);
                }
                var codeImage = new Bitmap(width, (int)_mHeight);
                var garphics = Graphics.FromImage(codeImage);
                //Pen _Pen;
                var lenEx = 0;
                for (int i = 0; i != value.Length; i++)
                {
                    int valueNumb = Int32.Parse(value[i].ToString(CultureInfo.InvariantCulture)) * (_mMagnify + 1);  //获取宽和放大系数
                    garphics.FillRectangle((i & 1) != 0 ? Brushes.White : Brushes.Black,
                        new Rectangle(lenEx, 0, valueNumb, (int)_mHeight));
                    //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height));
                    lenEx += valueNumb;
                }
                garphics.Dispose();
                return codeImage;
            }

            /// <summary>
            /// 显示可见条码文字 如果小于40 不显示文字
            /// </summary>
            /// <param name="pBitmap">图形</param>
            /// <param name="pViewText"></param>
            private void GetViewText(Bitmap pBitmap, string pViewText)
            {
                if (_mValueFont == null) return;

                Graphics graphics = Graphics.FromImage(pBitmap);
                SizeF drawSize = graphics.MeasureString(pViewText, _mValueFont);
                if (drawSize.Height > pBitmap.Height - 10 || drawSize.Width > pBitmap.Width)
                {
                    graphics.Dispose();
                    return;
                }

                int starY = pBitmap.Height - (int)drawSize.Height;
                graphics.FillRectangle(Brushes.White, new Rectangle(0, starY, pBitmap.Width, (int)drawSize.Height));
                graphics.DrawString(pViewText, _mValueFont, Brushes.Black, 0, 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();
            }
        }
    }
时间: 2024-11-02 23:38:56

<经验杂谈>C#生成条形码的相关文章

C#生成条形码

C#利用Zxing.net生成条形码和二维码并实现打印的功能     开篇:zxing.net是.net平台下编解条形码和二维码的工具. 下载地址:http://pan.baidu.com/s/1i3BXPLN Step1:使用VS2010新建一个窗体程序项目: Step2:添加三个类:分别是BarCodeClass.cs.DocementBase.cs.imageDocument.cs.(下一步贴出这些类的代码);;;;添加下载回来的引用zxing.dll. >说明: <1>   Ba

使用html2canvas实现批量生成条形码

/*前台代码*/ 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="GenerateCodeBar.aspx.cs" 2 Inherits="CodeBar_GenerateCodeBar" EnableEventValidation="false" %> 3 4 <!DOCTYPE html PUBLIC "-/

iOS开发——生成条形码,二维码

- (void)viewDidLoad { [super viewDidLoad]; self.imageView.image = [self generateBarCode:@"1524829417" width:60 height:60]; self.imageView2.image = [self generateQRCode:@"1524829417" width:60 height:60]; } - (UIImage *)generateQRCode:(N

php生成条形码

前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成平行线的图案.在日常生活中,条形码可以标出物品的生产国.制造厂家.商品名称.生产日期.图书分类号.邮件地点起止.类别.日期等许多信息.条形码编码格式具体请参考 打印出来的优惠券,商家需要用验证器读取条形码,来获得其有效性

生成条形码和二维码并实现打印的功能

生成条形码和二维码并实现打印的功能     开篇:平台下编解条形码和二维码的工具. 下载地址:http://pan.baidu.com/s/1kTr3Vuf Step1:使用VS2010新建一个窗体程序项目: Step2:添加三个类:分别是BarCodeClass.cs.DocementBase.cs.imageDocument.cs.(下一步贴出这些类的代码);;;;添加下载回来的引用zxing.dll. >说明: <1>   BarCodeClass.cs主要用来实现条形码和二维码的

C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)

原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391&fid=1692834292 如果不存在了则自行搜索下载. 1.BarcodeLib.dll 一维条码库支持以下条码格式 UPC-A UPC-E UPC 2 Digit Ext.

【PHP】php 生成条形码

1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成平行线的图案.在日常生活中,条形码可以标出物品的生产国.制造厂家.商品名称.生产日期.图书分类号.邮件地点起止.类别.日期等许多信息.条形码编码格式具体请参考 打印出来的优惠券,商家需要用验证器读取条形码,来获得其有效性. 2.如何生成条形码? 首先找到强大的开源资料,在barcode官网下

C#生成条形码图片的简单方法

本文实例讲述了C#生成条形码图片的简单方法.分享给大家供大家参考.具体实现方法如下: 实现原理: 其实Windows本身就有一个字体是用来显示条形码的. 只要将数字改为这种字体就变成了条形码. windows字体库下,有如下八种字体可以用来将数字转换成条形码: Code39AzaleaNarrow1 Code39AzaleaNarrow2 Code39AzaleaNarrow3 Code39AzaleaRegular1 Code39AzaleaRegular2 Code39AzaleaWide1

C#实现用于生成条形码的类

本文实例讲述了C#实现用于生成条形码的类.分享给大家供大家参考.具体如下: 这个C#类可以用来生成39码 12位标准条形码 using System.Collections; using System.Text.RegularExpressions; namespace DotNet.Utilities { public class BarCodeToHTML { public static string get39(string s, int width, int height) { Hash