在线生成条形码的解决方案(39码、EAN-13)

感谢博主:转自:http://xoyozo.eyuyao.com/blog/barcode.html

public partial class ReceivablesFormView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = Get("6901028045575", 2, 60);

            this.barcode.InnerHtml = code;
        }

        /// <summary>
        /// 获取 EAN-13 条形码(HTML 版)
        /// </summary>
        /// <param name="s">欲编码的字符串</param>
        /// <param name="width">单个条形宽(px)</param>
        /// <param name="height">条形高(px)</param>
        /// <returns></returns>
        public static string Get(string s, int width, int height)
        {
            int checkcode_input = -1;//输入的校验码
            if (!Regex.IsMatch(s, @"^\d{12}$"))
            {
                if (s.Length != 13)
                {
                    return "字符串长度不正确!";
                }
                else if (!Regex.IsMatch(s, @"^\d{13}$"))
                {
                    return "存在不允许的字符!";
                }
                else
                {
                    checkcode_input = int.Parse(s[12].ToString());
                    s = s.Substring(0, 12);
                }
            }

            int sum_even = 0;//偶数位之和
            int sum_odd = 0;//奇数位之和

            for (int i = 0; i < 12; i++)
            {
                if (i % 2 == 0)
                {
                    sum_odd += int.Parse(s[i].ToString());
                }
                else
                {
                    sum_even += int.Parse(s[i].ToString());
                }
            }

            int checkcode = (10 - (sum_even * 3 + sum_odd) % 10) % 10;//校验码

            if (checkcode_input > 0 && checkcode_input != checkcode)
            {
                return "输入的校验码错误!";
            }

            s += checkcode;//变成13位

            // 000000000101左侧42个01010右侧35个校验7个101000000000
            // 6        101左侧6位 01010右侧5位 校验1位101000000000

            string result_bin = "";//二进制串
            result_bin += "000000000101";

            string type = ean13type(s[0]);
            for (int i = 1; i < 7; i++)
            {
                result_bin += ean13(s[i], type[i - 1]);
            }
            result_bin += "01010";
            for (int i = 7; i < 13; i++)
            {
                result_bin += ean13(s[i], ‘C‘);
            }
            result_bin += "101000000000";

            string result_html = "";//HTML代码
            string color = "";//颜色
            int height_bottom = width * 5;
            foreach (char c in result_bin)
            {
                color = c == ‘0‘ ? "#FFFFFF" : "#000000";
                result_html += "<div style=\"width:" + width + "px;height:" + height + "px;float:left;background:" + color + ";\"></div>";
            }
            result_html += "<div style=\"clear:both\"></div>";

            result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;text-align:center;\">" + s[0] + "</div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            for (int i = 1; i < 7; i++)
            {
                result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
            }
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            for (int i = 7; i < 13; i++)
            {
                result_html += "<div style=\"float:left;width:" + (width * 7) + "px;color:#000000;text-align:center;\">" + s[i] + "</div>";
            }
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#FFFFFF;\"></div>";
            result_html += "<div style=\"float:left;width:" + width + "px;height:" + height_bottom + "px;background:#000000;\"></div>";
            result_html += "<div style=\"float:left;color:#000000;width:" + (width * 9) + "px;\"></div>";
            result_html += "<div style=\"clear:both\"></div>";

            return "<div style=\"background:#FFFFFF;padding:0px;font-size:" + (width * 10) + "px;font-family:‘楷体‘;\">" + result_html + "</div>";
        }
        private static string ean13(char c, char type)
        {
            switch (type)
            {
                case ‘A‘:
                    {
                        switch (c)
                        {
                            case ‘0‘: return "0001101";
                            case ‘1‘: return "0011001";
                            case ‘2‘: return "0010011";
                            case ‘3‘: return "0111101";//011101
                            case ‘4‘: return "0100011";
                            case ‘5‘: return "0110001";
                            case ‘6‘: return "0101111";
                            case ‘7‘: return "0111011";
                            case ‘8‘: return "0110111";
                            case ‘9‘: return "0001011";
                            default: return "Error!";
                        }
                    }
                case ‘B‘:
                    {
                        switch (c)
                        {
                            case ‘0‘: return "0100111";
                            case ‘1‘: return "0110011";
                            case ‘2‘: return "0011011";
                            case ‘3‘: return "0100001";
                            case ‘4‘: return "0011101";
                            case ‘5‘: return "0111001";
                            case ‘6‘: return "0000101";//000101
                            case ‘7‘: return "0010001";
                            case ‘8‘: return "0001001";
                            case ‘9‘: return "0010111";
                            default: return "Error!";
                        }
                    }
                case ‘C‘:
                    {
                        switch (c)
                        {
                            case ‘0‘: return "1110010";
                            case ‘1‘: return "1100110";
                            case ‘2‘: return "1101100";
                            case ‘3‘: return "1000010";
                            case ‘4‘: return "1011100";
                            case ‘5‘: return "1001110";
                            case ‘6‘: return "1010000";
                            case ‘7‘: return "1000100";
                            case ‘8‘: return "1001000";
                            case ‘9‘: return "1110100";
                            default: return "Error!";
                        }
                    }
                default: return "Error!";
            }
        }
        private static string ean13type(char c)
        {
            switch (c)
            {
                case ‘0‘: return "AAAAAA";
                case ‘1‘: return "AABABB";
                case ‘2‘: return "AABBAB";
                case ‘3‘: return "AABBBA";
                case ‘4‘: return "ABAABB";
                case ‘5‘: return "ABBAAB";
                case ‘6‘: return "ABBBAA";//中国
                case ‘7‘: return "ABABAB";
                case ‘8‘: return "ABABBA";
                case ‘9‘: return "ABBABA";
                default: return "Error!";
            }
        }
    }
时间: 2024-09-29 11:26:54

在线生成条形码的解决方案(39码、EAN-13)的相关文章

C#---生成条形码,二维码,分界线并打印

分享一个可以生成条形码,二维码的dll,不多说,直接上代码: Printer: 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Drawing; 7 using ThoughtWorks.QRCode.Codec; 8 9 namespace PrintFun 10

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

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

使用谷歌Z生成条形码以及二维码

下载地址:http://zxingnet.codeplex.com/ zxing.net是.net平台下编解条形码和二维码的工具,使用非常方便. 首先下载二进制dll文件,引入工程: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.

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

ZXing生成条形码、二维码、带logo二维码

采用的是开源的ZXing,Maven配置如下,jar包下载地址,自己选择版本下载,顺便推荐下Maven Repository <!-- https://mvnrepository.com/artifact/com.google.zxing/core --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version

winform生成条形码和二维码(ZXing.Net)

首先在项目添加ZXing.Net. 工具-->Nuget包管理器-->Nuget程序包  在所搜栏输入 ZXing.Net 如下图: 添加完成后会看见: 效果图: 所有代码: /// <summary> /// 条形码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> priva

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.

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

使用JS创建条形码在线生成工具-toolfk.com

本文要推荐的[ToolFk]是一款程序员经常使用的线上免费测试工具箱,ToolFk 特色是专注于程序员日常的开发工具,不用安装任何软件,只要把内容贴上按一个执行按钮,就能获取到想要的内容结果.ToolFk还支持 BarCode条形码在线生成. QueryList采集器. PHP代码在线运行. PHP混淆.加密.解密. Python代码在线运行.JavaScript在线运行.YAML格式化工具.HTTP模拟查询工具.HTML在线工具箱.JavaScript在线工具箱.CSS在线工具箱.JSON在线