EAN-13条码

 
//EAN-13 条码
    public void EAN13(string code, int w, int h)
    {
        int LMethod = 0;
        int rsum = 0;
        int lsum = 0;
        int sh = 0;
        int cWidth = 0;
        int cHeight = 0;
 
        string Barcode;
        string bColor;
 
        string[] Guide = new string[] { "AAAAAA", "AABABB", "AABBAB", "ABAABB", "ABBAAB", "ABBBAA", "ABABAB", "ABABBA", "ABBABA" };
        string[] Rencode = new string[] { "1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100" };
 
        Hashtable Dict = new Hashtable();
        Dict.Add("A", "0001101001100100100110111101010001101100010101111011101101101110001011");
        Dict.Add("B", "0100111011001100110110100001001110101110010000101001000100010010010111");
 
        string cStart = "101";
        string cMid = "01010";
        string cEnd = "101";
 
        if (w < 2)
            w = 2;
 
        if (h < 20)
            h = 20;
 
        cWidth = w;         // 条码单元宽度
        cHeight = h;        // 条码高度
 
        // 检验条码是否符合标准
        if (code.Length != 13)
        {
            Response.Write("<script language='javascript'>alert(' 必须为13位!')</script>" );
            return;
        }
 
        for (int i = 1; i < 13; i++)
        {
            if (IsNumeric(code.Substring(i, 1)))
            {
                if (i % 2 == 0)
                {
                    rsum += int.Parse(code.Substring(i - 1, 1));
                }
                else
                {
                    lsum += int.Parse(code.Substring(i - 1, 1));
                }
            }
            else
            {
                Response.Write("<script language='javascript'>alert(' 必须为数字!')</script>" );
                return;
            }
        }
 
 
        if ((10 - ((rsum * 3 + lsum) % 10)) % 10 != int.Parse(code.Substring(code.Length - 1, 1)))
        {
            Response.Write("<script language='javascript'>alert(' 此条码有错误!!')</script>" );
            return;
        }
 
        // 转换条码
        Barcode = cStart;
        LMethod = int.Parse(code.Substring(0, 1));
 
        for (int i = 2; i < 8; i++)
        {
            Barcode += Convert.ToString(Dict[(Guide[LMethod - 1]).Substring(i - 2, 1)]).Substring((7 * int.Parse(code.Substring(i - 1, 1))), 7);
        }
 
        Barcode += cMid;
 
        for (int i = 8; i < 14; i++)
        {
            Barcode += Rencode[int.Parse(code.Substring(i - 1, 1))];
        }
 
        Barcode += cEnd;
 
        string fg = "#000000";   // 条码前景色
        string bg = "#ffffff";   // 条码背景色
 
        Response.Write("<div style='position:absolute;width:" + (cWidth * 95 + 60) + "px; height:" + (cHeight + 30) + "px; background:" + bg + ";'>");
 
        // 绘制条码
        for (int x = 1; x <= Barcode.Length; x++)
        {
            if (x < 5 || x > 92 || (x > 46 && x < 51))
            {
                sh = 10;
            }
            else
            {
                sh = 0;
            }
 
            if (Barcode.Substring(x - 1, 1) == "1")
            {
                bColor = fg;
            }
            else
            {
                bColor = bg;
            }
 
            Response.Write("<div style='position:absolute;left:" + ((x - 1) * cWidth + 30) + "px;top:5px;width:" + cWidth + "px;height:" + (cHeight + 5 + sh) + "px;background:" + bColor + ";'></div>");
        }
 
        // 加入可读数字标签
        Response.Write("<div style='position:absolute;left:16px;top:" + (cHeight + 10) + "px;background:" + bg + ";color:" + fg + ";font:12px Verdana;'>" + code.Substring(0, 1) + "</div>");
 
        for (int x = 1; x < 7; x++)
        {
            Response.Write("<div style='position:absolute;left:" + ((x * 7 + 2) * cWidth + 22) + "px;top:" + (cHeight + 10) + "px;background:" + bg + ";color:" + fg + ";font:12px Verdana;'>" + code.Substring(x, 1) + "</div>");
            Response.Write("<div style='position:absolute;left:" + ((x * 7 + 47) * cWidth + 24) + "px;top:" + (cHeight + 10) + "px;background:" + bg + ";color:" + fg + ";font:12px Verdana;'>" + code.Substring(x + 6, 1) + "</div>");
        }
 
        Response.Write("<div style='position:absolute; left:30px;top:" + (cHeight + 25) + "px;width:" + (cWidth * 95) + "px;height:10px;text-align:center;background:#ffffff;border:solid 1px #e5e5e5;font:9px verdana;'>+copy; 2005 by 7th. </div>");
        Response.Write("</div>");
 
    }
 
    private bool IsNumeric(string str)
    {
        try
        {
            int.Parse(str);
            return true;
        }
        catch
        {
            return false;
        }
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        EAN13(TextBox1.Text,1,40);
    }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值