C# 條形碼 生成函式 (Code 128 標準參考:GB/T 18347-2001)

lowsong發表於2009-05-08

 最近在做單據列印,發現客戶要求用到條形碼,在網上找了,發現只有一些條形碼的標準,但打出來發現根本不能掃,還要加某些字型.

以下程式碼經過實際掃描測試~!

原創,轉載請說明~!

 

[code=C#]

 

    /// <summary>
    /// 生成條形碼( 128條碼,標準參考:GB/T 18347-2001 )

    /// BY JUNSON 20090508
    /// </summary>

    public class BarCode
    {
        /// <summary>
        /// 條形碼生成函式
        /// </summary>
        /// <param name="text">條型碼字串</param>
        /// <returns></returns>
        public static Bitmap BuildBarCode(string text)
        {
            //查檢是否合條件TEXT
            bool ck = CheckErrerCode(text);
            if (!ck)
                throw new Exception("條形碼字元不合要求,不能是漢字或全形字元");

            string barstring = BuildBarString(text);
            return KiCode128C(barstring, 30);
           
        }

        /// <summary>
        /// 建立條碼字串
        /// </summary>
        /// <param name="tex">條碼內容</param>
        /// <returns></returns>
        private static string BuildBarString(string tex)
        {
            string barstart = "bbsbssbssss";    //碼頭
            string barbody = "";                //碼身
            string barcheck = "";               //碼檢
            string barend = "bbsssbbbsbsbb";    //碼尾
           
            int checkNum = 104;
            //迴圈新增碼身,計算碼檢
            for (int i = 1; i <= tex.Length; i++)
            {
                int index = (int)tex[i - 1] - 32;
                checkNum += (index * i);

                barbody += AddSimpleTag(index);//加入字元值的條碼標記
            }
            //碼檢值計算

            barcheck = AddSimpleTag(int.Parse(Convert.ToDouble(checkNum % 103).ToString("0")));


            string barstring = barstart + barbody + barcheck + barend;
            return barstring;
        }

        //增加一個條碼標記
        private static string AddSimpleTag(int CodeIndex)
        {
            string res = "";

            /// <summary>1-4的條的字元標識 </summary>
            string[] TagB ={ "", "b", "bb", "bbb", "bbbb" };
            /// <summary>1-4的空的字元標識 </summary>
            string[] TagS ={ "", "s", "ss", "sss", "ssss" };
            string[] Code128List = new string[] {
                "212222","222122","222221","121223","121322","131222","122213","122312","132212","221213",
                "221312","231212","112232","122132","122231","113222","123122","123221","223211","221132",
                "221231","213212","223112","312131","311222","321122","321221","312212","322112","322211",
                "212123","212321","232121","111323","131123","131321","112313","132113","132311","211313",
                "231113","231311","112133","112331","132131","113123","113321","133121","313121","211331",
                "231131","213113","213311","213131","311123","311321","331121","312113","312311","332111",
                "314111","221411","431111","111224","111422","121124","121421","141122","141221","112214",
                "112412","122114","122411","142112","142211","241211","221114","413111","241112","134111",
                "111242","121142","121241","114212","124112","124211","411212","421112","421211","212141",
                "214121","412121","111143","111341","131141","114113","114311","411113","411311","113141",
                "114131","311141","411131","211412","211214","211232" };

            string tag = Code128List[CodeIndex];

            for (int i = 0; i < tag.Length; i++)
            {
                string temp = "";
                int num = int.Parse(tag[i].ToString());
                if (i % 2 == 0)
                {
                    temp = TagB[num];
                }
                else
                {
                    temp = TagS[num];
                }
                res += temp;
            }
            return res;
        }

        /// <summary>
        /// 檢查條形碼文字是否合條件(不能是漢字或全形字元)
        /// </summary>
        /// <param name="cktext"></param>
        /// <returns></returns>
        private static bool CheckErrerCode(string cktext)
        {
            foreach (char c in cktext)
            {
                byte[] tmp = System.Text.UnicodeEncoding.Default.GetBytes(c.ToString());

                if (tmp.Length > 1)
                    return false;
            }
            return true;
        }

        /// <summary>生成條碼 </summary>
        /// <param name="BarString">條碼模式字串</param>  //Format32bppArgb
        /// <param name="Height">生成的條碼高度</param>
        /// <returns>條碼圖形</returns>
        private static Bitmap KiCode128C(string BarString, int _Height)
        {

            Bitmap b = new Bitmap(BarString.Length, _Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            //using (Graphics grp = Graphics.FromImage(b))
            //{
            try
            {
                char[] cs = BarString.ToCharArray();


                for (int i = 0; i < cs.Length; i++)
                {
                    for (int j = 0; j < _Height; j++)
                    {
                        if (cs[i] == 'b')
                        {
                            b.SetPixel(i, j, Color.Black);
                        }
                        else
                        {
                            b.SetPixel(i, j, Color.White);
                        }
                    }
                }

                //grp.DrawString(text, SystemFonts.CaptionFont, Brushes.Black, new PointF(leftEmpty, b.Height - botEmpty));

                return b;
            }
            catch
            {
                return null;
            }
            //}
        }
    }

 

 

[/code]

相關文章