C#条形码生成(三)----简单的三种Code128实现

 编写这三类条形码的时候,网上baidu来baidu去,就是没找到关于Code128A,Code128B,Code128C的明确定义,然后就按照网上通用的说法只取相应的字符集来编写了这部分代码

CodeA

public class Code128A : absCode128
    {
        /// 
        /// Code128A条形码,只支持128字符集A(数字、大写字母、控制字符)
        /// 
        /// 
        public Code128A(string rawData)
            : base(rawData)
        {
        }

        protected override bool RawDataCheck()
        {
            //128字符集A对应的ASCII码范围为0~95
            foreach (char c in this._rawData)
            {
                byte tempC = (byte)c;
                if (tempC <= 95)
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        protected override string GetEncodedData()
        {
            StringBuilder tempBuilder = new StringBuilder();
            tempBuilder.Append(Code128.BSList[Code128.StartA]);//加上起始符StartA
            byte sIndex;
            int checkNum = Code128.StartA;//校验字符
            for (int i = 0; i < this._rawData.Length; i++)
            {
                sIndex = Code128.GetSIndexFromA(this._rawData[i]);
                tempBuilder.Append(Code128.BSList[sIndex]);
                checkNum += (i + 1) * sIndex;
            }
            checkNum %= 103;
            tempBuilder.Append(Code128.BSList[checkNum]);//加上校验符
            tempBuilder.Append(Code128.BSList[Code128.Stop]);//加上结束符
            return tempBuilder.ToString();
        }
    }


Code128B

/// 
    /// Code128B条形码,只支持128字符集B(数字、大小字母、字符)
    /// 
    public class Code128B : absCode128
    {
        public Code128B(string rawData)
            : base(rawData)
        {
        }

        protected override bool RawDataCheck()
        {
            //128字符集B对应的ASCII码范围为32~127
            foreach (char c in this._rawData)
            {
                byte tempC = (byte)c;
                if (tempC >= 32 && tempC <= 127)
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        protected override string GetEncodedData()
        {
            StringBuilder tempBuilder = new StringBuilder();
            tempBuilder.Append(Code128.BSList[Code128.StartB]);//加上起始符StartB
            byte sIndex;
            int checkNum = Code128.StartB;//校验字符
            for (int i = 0; i < this._rawData.Length; i++)
            {
                sIndex = Code128.GetSIndexFromB(this._rawData[i]);//字符集B中ASCII码 减去32后就等于符号字符值
                tempBuilder.Append(Code128.BSList[sIndex]);
                checkNum += (i + 1) * sIndex;
            }
            checkNum %= 103;
            tempBuilder.Append(Code128.BSList[checkNum]);//加上校验符
            tempBuilder.Append(Code128.BSList[Code128.Stop]);//加上结束符
            return tempBuilder.ToString();
        }
    }


Code128C

/// 
    /// Code128C条形码,只支持128字符集C(双位数字)
    /// 
    public class Code128C : absCode128
    {
        public Code128C(string rawData)
            : base(rawData)
        {
        }

        protected override bool RawDataCheck()
        {
            return Regex.IsMatch(this._rawData, @"^\d{2,96}$") && this._rawData.Length % 2 == 0;//Code128C 2个数字代表一个数据字符,所以最大可以96个数字
        }

        protected override string GetEncodedData()
        {
            StringBuilder tempBuilder = new StringBuilder();
            tempBuilder.Append(Code128.BSList[Code128.StartC]);//加上起始符StartC
            byte sIndex;
            int checkNum = Code128.StartC;//校验字符,StartC为105
            for (int i = 0; i < this._rawData.Length / 2; i++)
            {
                sIndex = byte.Parse(this._rawData.Substring(i * 2, 2));
                tempBuilder.Append(Code128.BSList[sIndex]);
                checkNum += (i + 1) * sIndex;
            }
            checkNum %= 103;
            tempBuilder.Append(Code128.BSList[checkNum]);//加上校验符
            tempBuilder.Append(Code128.BSList[Code128.Stop]);//加上结束符
            return tempBuilder.ToString();
        }
    }


 

你可能感兴趣的:(C#)