zoukankan      html  css  js  c++  java
  • 生成条形码

    <一>路由
         routes.MapRoute(
                   name: "home",
                   url: "home/GetBar/{id}",
                   defaults: new { controller = "Home", action = "GetBar", id = UrlParameter.Optional }
               );
    <二>HTML页
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>条形码</title>
        <script src="~/Scripts/jquery-1.10.2.js"></script>
        <script>
    
            function wds_createbar() {
                var num = $("#codenum").val();
                $("#img-div").attr("src", "/home/GetBar?rom="+num);
            }
    
        </script>
    </head>
    <body>
        <div>
            <input type="text" value="" id="codenum"/>
            <input type="button" value="生成条形码" onclick="wds_createbar()"/><br>
            <div style=" 200px;height: 200px" id="wds-bar-div">
                <img id="img-div" />
            </div>
        </div>
    </body>
    </html>
    <三>控制器
    
        public void GetBar()
            {
                var strNum = Request.QueryString["rom"];
                if (!string.IsNullOrEmpty(strNum))
                {
                    Bitmap b = GetCode39(strNum);           
                    var stream = new MemoryStream();
                    b.Save(stream, ImageFormat.Png);
                    var imagebytes = stream.ToArray();
                    Response.OutputStream.Write(imagebytes, 0, imagebytes.Length);
    
                   //在电脑上形成图片
                    //string filename1 = "d:\22.jpg";=
                    //b.Save(filename1, ImageFormat.Jpeg);
    
    
                }       
            }
    
    
    //生成条形码的方法
            private Bitmap GetCode39(string strSource)
            {
                int x = 5; //左邊界
                int y = 0; //上邊界
                int WidLength = 4; //粗BarCode長度
                int NarrowLength =2; //細BarCode長度
                int BarCodeHeight =72; //BarCode高度
                int intSourceLength = strSource.Length;
                string strEncode = "010010100"; //編碼字串 初值為 起始符號 *
    
                string AlphaBet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*"; //Code39的字母
    
                string[] Code39 = //Code39的各字母對應碼
      {
           /* 0 */ "000110100",
           /* 1 */ "100100001",
           /* 2 */ "001100001",
           /* 3 */ "101100000",
           /* 4 */ "000110001",
           /* 5 */ "100110000",
           /* 6 */ "001110000",
           /* 7 */ "000100101",
           /* 8 */ "100100100",
           /* 9 */ "001100100",
           /* A */ "100001001",
           /* B */ "001001001",
           /* C */ "101001000",
           /* D */ "000011001",
           /* E */ "100011000",
           /* F */ "001011000",
           /* G */ "000001101",
           /* H */ "100001100",
           /* I */ "001001100",
           /* J */ "000011100",
           /* K */ "100000011",
           /* L */ "001000011",
           /* M */ "101000010",
           /* N */ "000010011",
           /* O */ "100010010",
           /* P */ "001010010",
           /* Q */ "000000111",
           /* R */ "100000110",
           /* S */ "001000110",
           /* T */ "000010110",
           /* U */ "110000001",
           /* V */ "011000001",
           /* W */ "111000000",
           /* X */ "010010001",
           /* Y */ "110010000",
           /* Z */ "011010000",
           /* - */ "010000101",
           /* . */ "110000100",
           /*' '*/ "011000100",
           /* $ */ "010101000",
           /* / */ "010100010",
           /* + */ "010001010",
           /* % */ "000101010",
           /* * */ "010010100"
      };
    
    
                strSource = strSource.ToUpper();
    
                //實作圖片
                Bitmap objBitmap = new Bitmap(
                  ((WidLength * 3 + NarrowLength * 7) * (intSourceLength + 2)) + (x * 2),
                  BarCodeHeight + (y * 2));
    
                Graphics objGraphics = Graphics.FromImage(objBitmap); //宣告GDI+繪圖介面
    
                //填上底色
                objGraphics.FillRectangle(Brushes.White, 0, 0, objBitmap.Width, objBitmap.Height);
    
                for (int i = 0; i < intSourceLength; i++)
                {
                    if (AlphaBet.IndexOf(strSource[i]) == -1 || strSource[i] == '*') //檢查是否有非法字元
                    {
                        objGraphics.DrawString("含有非法字元", SystemFonts.DefaultFont, Brushes.Red, x, y);
                        return objBitmap;
                    }
                    //查表編碼
                    strEncode = string.Format("{0}0{1}", strEncode, Code39[AlphaBet.IndexOf(strSource[i])]);
                }
    
                strEncode = string.Format("{0}0010010100", strEncode); //補上結束符號 *
    
                int intEncodeLength = strEncode.Length; //編碼後長度
                int intBarWidth;
    
                for (int i = 0; i < intEncodeLength; i++) //依碼畫出Code39 BarCode
                {
                    intBarWidth = strEncode[i] == '1' ? WidLength : NarrowLength;
                    objGraphics.FillRectangle(i % 2 == 0 ? Brushes.Black : Brushes.White,
                      x, y, intBarWidth, BarCodeHeight);
                    x += intBarWidth;
                }
                return objBitmap;
            } 
  • 相关阅读:
    spring事务调用失效问题
    redis的主从、哨兵配置
    Lucene介绍与入门使用
    超详细“零”基础kafka入门篇
    消息队列——RabbitMQ学习笔记
    linux中RabbitMQ安装教程
    JAVA正则
    JAVA String类
    JAVA lang包介绍
    JAVA枚举
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/4369475.html
Copyright © 2011-2022 走看看