老手网

code128条形码打印

分类:C# 作者:oldhand 来源: 发布:2013-06-04 15:45:54
斑马条码打印机(ZPL)可以通过^BC指令来进行code128条形码的打印。

ZPL指令很简单,很多地方可以搜到介绍。
但128的指令集有三种,ABC三种子集,如何使用很少有介绍。
下面我将给出c#的子集源码。
通过源码,很容易理解了。



	/* 直接将字符串转换为code128 A子集,
	   转换完毕业,可以直接使用
	   实例: A6135*G0311040025A000D0502 条码打印 =>	   
	   ^BY2,3,41^FT13,70^BCN,,Y,N
	   ^FD>93322171921103916191717162016161821331616163616211618^FS
	*/
	public static string code128(string barcode)
        {
            string pattern = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ*";
            string[] code = {"16","17","18","19","20","21","22","23","24","25",
                             "33","34","35","36","37","38","39","40","41","42",
                             "43","44","45","46","47","48","49","50","51","52",
                             "53","54","55","56","57","58","10"};
            string printcode = "9";
             for (int i = 0; i < barcode.Length; i++)
             {
                 printcode += code[pattern.IndexOf(barcode[i])];
             }
            return printcode;

        
        }
        public static bool checkcode128(string barcode,int pos)
        {
            string pattern = "0123456789";
            if (barcode.Length > pos + 3)
            {
                if (pattern.IndexOf(barcode[pos]) >= 0 &&
                    pattern.IndexOf(barcode[pos + 1]) >= 0 &&
                    pattern.IndexOf(barcode[pos + 2]) >= 0 &&
                    pattern.IndexOf(barcode[pos + 3]) >= 0)
                    return true;
            }
            return false;
        }
	/* 将字符串转换为code128,智能选择字符子集
	   转换完毕业,可以直接使用
	   实例: A6135*G0311040025A000D0502 条码打印 =>	   
	   ^BY2,3,41^FT13,70^BCN,,Y,N
	   ^FD>:A>56135>6*G>50311040025>6A000D>50502^FS
	*/
        public static string newcode128(string barcode)
        {
            string printcode = ":";
            string pattern = "0123456789";
            bool code = true;
            for (int i = 0; i < barcode.Length; i++)
            {               
                    if (code)
                    {
                        if (pattern.IndexOf(barcode[i]) == -1)
                        {
                            printcode += barcode[i];
                        }
                        else
                        {
			    //// Code128编码,若有连续4个数字在一起,需要转换子集C来打印,来缩短条形码的宽度
                            if (checkcode128(barcode,i))
                            {
			        /// 转换为子集C  子集C只能支持数字,但打印出来宽度是最小的
                                printcode += ">5";
                                printcode += barcode[i];
                                code = false;
                            }
                            else
                            {
                                printcode += barcode[i];
                            }
                        }
                    }
                    else
                    {
                        if (pattern.IndexOf(barcode[i]) >= 0)
                        {
                            printcode += barcode[i];
                        }
                        else
                        {
			    ////// 转换为子集B 子集B支持数字,字母与符号,但打印出来宽度是最大的
                            printcode += ">6";
                            printcode += barcode[i];
                            code = true;
                        }
                    }

            }
            return printcode;


        }

 

-
分享到:

Copyright © 2012 - 2014 oldhand.cn All Rights Reserved. 粤ICP备13025864号