首页 新闻 搜索 专区 学院

请教关于条码生成的问题

0
悬赏园豆:10 [待解决问题]
// 生成条形码( 128条码,标准参考:GB/T 18347-2001 )
    public class BarCode
    {
        public static byte[] CreateBarCode(string text)
        {
            //查检是否合条件
            bool Checked = CheckErrerCode(text);
            if (!Checked)
            {
                throw new Exception("条形码字符不合要求,不能是汉字或全角字符");
            }
            string BarString = CreateBarString(text);
            return BarCode_Code128C(BarString, 30);
        }

        private static string CreateBarString(string Bardata)
        {
            string BarHead = "bbsbssbssss";     //码头
            string BarBody = "";                //码身
            string BarCheck = "";               //码检
            string BarEnd = "bbsssbbbsbsbb";    //码尾
            int checkNum = 104;

            //循环添加码身,计算码检
            for (int i = 1; i <= Bardata.Length; i++)
            {
                int index = (int)Bardata[i - 1] - 32;
                checkNum += (index * i);
                BarBody += AddBarcodeTag(index);//加入字符值的条码标记
            }

            //码检值计算
            BarCheck = AddBarcodeTag(int.Parse(Convert.ToDouble(checkNum % 103).ToString("0")));

            string barstring = BarHead + BarBody + BarCheck + BarEnd;
            return barstring;
        }



        //增加一个条码标记



        private static string AddBarcodeTag(int CodeIndex)
        {
            string result = "";

            /// <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];
                }
                result += temp;
            }
            return result;
        }

        private static bool CheckErrerCode(string CheckData)
        {
            foreach (char i in CheckData)
            {
                byte[] tmp = UnicodeEncoding.Default.GetBytes(i.ToString());
                if (tmp.Length > 1)
                {
                    return false;
                }
            }
            return true;
        }

        private static byte[] BarCode_Code128C(string BarString, int Height)
        {
            Bitmap BarCode = new Bitmap(BarString.Length, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            try
            {
                char[] CharString = BarString.ToCharArray();
                for (int i = 0; i < CharString.Length; i++)
                {
                    for (int j = 0; j < Height; j++)
                    {
                        if (CharString[i] == 'b')
                        {
                            BarCode.SetPixel(i, j, Color.Black);
                        }
                        else
                        {
                            BarCode.SetPixel(i, j, Color.White);
                        }
                    }
                }
                MemoryStream Stream = new MemoryStream();
                BarCode.Save(Stream, ImageFormat.Jpeg);
                return Stream.ToArray();
            }
            catch
            {
                return null;
            }
        }
    }

重点来了,这样生成的效果变成只有条码,一般常见的条码下面那行字数字没有生成上去,请问各位大师应该修改。把文字也生成进去。

时光刺客的主页 时光刺客 | 初学一级 | 园豆:60
提问于:2013-08-20 15:50
< >
分享
所有回答(2)
0

如果这个些代码中有那个功能的话就是循环少了;

如果代码中没有那个功能的话在最后补上就可以了吧;

seaconch | 园豆:4823 (老鸟四级) | 2013-08-23 16:57
0

原来还可以直接生成条码,学习了,留个记号

zhengyingcan | 园豆:12 (初学一级) | 2013-09-02 15:42
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册