PDF417条码生成类

这是一个C#实现的PDF417条码生成类库,提供了生成PDF417条码所需的编码、图像处理等功能。通过设置不同属性可以调整条码的错误校验级别、尺寸等参数。
摘要由CSDN通过智能技术生成

本人网上看了有几个人需求pdf417条码的需求,顾把本人现成的代码贴出来分享

 

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace LabelCore.Template
{
    public class Pdf417lib
    {
        private void InitBlock()
        {
            codewords = new int[MAX_DATA_CODEWORDS + 2];
        }

        virtual protected internal int MaxSquare
        {
            get
            {
                if (codeColumns > 21)
                {
                    codeColumns = 29;
                    codeRows = 32;
                }
                else
                {
                    codeColumns = 16;
                    codeRows = 58;
                }
                return MAX_DATA_CODEWORDS + 2;
            }

        }
        /// <summary>Gets the raw image bits of the barcode. The image will have to
        /// be scaled in the Y direction by <CODE>yHeight</CODE>.
        /// </summary>
        /// <returns> The raw barcode image
        /// </returns>
        virtual public sbyte[] OutBits
        {
            get
            {
                return this.outBits;
            }

        }
        /// <summary>Gets the number of X pixels of <CODE>outBits</CODE>.</summary>
        /// <returns> the number of X pixels of <CODE>outBits</CODE>
        /// </returns>
        virtual public int BitColumns
        {
            get
            {
                return this.bitColumns;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the number of Y pixels of <CODE>outBits</CODE>.
        /// It is also the number of rows in the barcode.
        /// </summary>
        /// <returns> the number of Y pixels of <CODE>outBits</CODE>
        /// </returns>
        /// <summary>Sets the number of barcode rows. This number may be changed
        /// to keep the barcode valid.
        /// </summary>
        /// <param name="codeRows">the number of barcode rows
        /// </param>
        virtual public int CodeRows
        {
            get
            {
                return this.codeRows;
            }

            set
            {
                this.codeRows = value;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the number of barcode data columns.</summary>
        /// <returns> he number of barcode data columns
        /// </returns>
        /// <summary>Sets the number of barcode data columns.
        /// This number may be changed to keep the barcode valid.
        /// </summary>
        /// <param name="codeColumns">the number of barcode data columns
        /// </param>
        virtual public int CodeColumns
        {
            get
            {
                return this.codeColumns;
            }

            set
            {
                this.codeColumns = value;
            }

        }
        /// <summary>Gets the codeword array. This array is always 928 elements long.
        /// It can be writen to if the option <CODE>PDF417_USE_RAW_CODEWORDS</CODE>
        /// is set.
        /// </summary>
        /// <returns> the codeword array
        /// </returns>
        virtual public int[] Codewords
        {
            get
            {
                return this.codewords;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the length of the codewords.</summary>
        /// <returns> the length of the codewords
        /// </returns>
        /// <summary>Sets the length of the codewords.</summary>
        /// <param name="lenCodewords">the length of the codewords
        /// </param>
        virtual public int LenCodewords
        {
            get
            {
                return this.lenCodewords;
            }

            set
            {
                this.lenCodewords = value;
            }
        }

        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the error level correction used for the barcode. It may different
        /// from the previously set value.
        /// </summary>
        /// <returns> the error level correction used for the barcode
        /// </returns>
        /// <summary>Sets the error level correction for the barcode.</summary>
        /// <param name="errorLevel">the error level correction for the barcode
        /// </param>
        virtual public int ErrorLevel
        {
            get
            {
                return this.errorLevel;
            }

            set
            {
                this.errorLevel = value;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the options to generate the barcode.</summary>
        /// <returns> the options to generate the barcode
        /// </returns>
        /// <summary>Sets the options to generate the barcode. This can be all
        /// the <CODE>PDF417_*</CODE> constants.
        /// </summary>
        /// <param name="options">the options to generate the barcode
        /// </param>
        virtual public int Options
        {
            get
            {
                return this.options;
            }

            set
            {
                this.options = value;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the barcode aspect ratio.</summary>
        /// <returns> the barcode aspect ratio
        /// </returns>
        /// <summary>Sets the barcode aspect ratio. A ratio or 0.5 will make the
        /// barcode width twice as large as the height.
        /// </summary>
        /// <param name="aspectRatio">the barcode aspect ratio
        /// </param>
        virtual public float AspectRatio
        {
            get
            {
                return this.aspectRatio;
            }

            set
            {
                this.aspectRatio = value;
            }

        }
        //UPGRADE_NOTE: Respective javadoc comments were merged.  It should be changed in order to comply with .NET documentation conventions.
        /// <summary>Gets the Y pixel height relative to X.</summary>
        /// <returns> the Y pixel height relative to X
        /// </returns>
        /// <summary>Sets the Y pixel height relative to X. It is usually 3.</summary>
        /// <param name="yHeight">the Y pixel height relative to X
        /// </param>
        virtual public float YHeight
        {
            get
            {
                return this.yHeight;
            }

            set
            {
                this.yHeight = value;
            }

        }

        /// <summary>Auto-size is made based on <CODE>aspectRatio</CODE> and <CODE>yHeight</CODE>. </summary>
        public const int PDF417_USE_ASPECT_RATIO = 0;
        /// <summary>The size of the barcode will be at least <CODE>codeColumns*codeRows</CODE>. </summary>
        public const int PDF417_FIXED_RECTANGLE = 1;
        /// <summary>The size will be at least <CODE>codeColumns</CODE>
        /// with a variable number of <CODE>codeRows</CODE>.
        /// </summary>
        public const int PDF417_FIXED_COLUMNS = 2;
        /// <summary>The size will be at least <CODE>codeRows</CODE>
        /// with a variable number of <CODE>codeColumns</CODE>.
        /// </summary>
        public const int PDF417_FIXED_ROWS = 4;
        /// <summary>The error level correction is set automatically according
        /// to ISO 15438 recomendations.
        /// </summary>
        public const int PDF417_AUTO_ERROR_LEVEL = 0;
        /// <summary>The error level correction is set by the user. It can be 0 to 8. </summary>
        public const int PDF417_USE_ERROR_LEVEL = 16;
        /// <summary>No <CODE>text</CODE> interpretation is done and the content of <CODE>codewords</CODE>
        /// is used directly.
        /// </summary>
        public const int PDF417_USE_RAW_CODEWORDS = 64;
        /// <summary>Inverts the output bits of the raw bitmap that is normally
        /// bit one for black. It has only effect for the raw bitmap.
        /// </summary>
        public const int PDF417_INVERT_BITMAP = 128;


        protected internal int bitPtr;
        protected internal int cwPtr;
        protected internal SegmentList segmentList;

        /// <summary>Creates a new <CODE>BarcodePDF417</CODE> with the default settings. </summary>
        public Pdf417lib()
        {
            InitBlock();
            setDefaultParameters();
        }

        protected internal virtual bool checkSegmentType(Segment segment, char type)
        {
            if (segment == null)
                return false;
            return segment.type == type;
        }

        protected internal virtual int getSegmentLength(Segment segment)
        {
            if (segment == null)
                return 0;
            return segment.end - segment.start;
        }

        /// <summary>Set the default settings that correspond to <CODE>PDF417_USE_ASPECT_RATIO</CODE>
        /// and <CODE>PDF417_AUTO_ERROR_LEVEL</CODE>.
        /// </summary>
        public virtual void setDefaultParameters()
        {
            options = 0;
            outBits = null;
            text = new sbyte[0];
            yHeight = 3;
            aspectRatio = 0.5f;
        }

        protected internal virtual void outCodeword17(int codeword)
        {
            int bytePtr = bitPtr / 8;
            int bit = bitPtr - bytePtr * 8;
            outBits[bytePtr++] |= (sbyte)(codeword >> (9 + bit));
            outBits[bytePtr++] |= (sbyte)(codeword >> (1 + bit));
            codeword <<= 8;
            outBits[bytePtr] |= (sbyte)(codeword >> (1 + bit));
            bitPtr += 17;
        }

        protected internal virtual void outCodeword18(int codeword)
        {
            int bytePtr = bitPtr / 8;
            int bit = bitPtr - bytePtr * 8;
            outBits[bytePtr++] |= (sbyte)(codeword >> (10 + bit));
            outBits[bytePtr++] |= (sbyte)(codeword >> (2 + bit));
            codeword <<= 8;
            outBits[bytePtr] |= (sbyte)(codeword >> (2 + bit));
            if (bit == 7)
                outBits[++bytePtr] |= unchecked((sbyte)0x80);
            bitPtr += 18;
        }

        protected internal virtual void outCodeword(int codeword)
        {
            outCodeword17(codeword);
        }

        protected internal virtual void outStopPattern()
        {
            outCodeword18(STOP_PATTERN);
        }

        protected internal virtual void outStartPattern()
        {
            outCodeword17(START_PATTERN);
        }

        protected internal virtual void outPaintCode()
        {
            int codePtr = 0;
            bitColumns = START_CODE_SIZE * (codeColumns + 3) + STOP_SIZE;
            int lenBits = ((bitColumns - 1) / 8 + 1) * codeRows;
            outBits = new sbyte[lenBits];
            for (int row = 0; row < codeRows; ++row)
            {
                bitPtr = ((bitColumns - 1) / 8 + 1) * 8 * row;
                int rowMod = row % 3;
                int[] cluster = CLUSTERS[rowMod];
                outStartPattern();
                int edge = 0;
                switch (rowMod)
                {

                    case 0:
                        edge = 30 * (row / 3) + ((codeRows - 1) / 3);
                        break;

                    case 1:
                        edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3);
                        break;

                    default:
                        edge = 30 * (row / 3) + codeColumns - 1;
                        break;

                }
                outCodeword(cluster[edge]);

                for (int column = 0; column < codeColumns; ++column)
                {
                    outCodeword(cluster[codewords[codePtr++]]);
                }

                switch (rowMod)
                {

                    case 0:
                        edge = 30 * (row / 3) + codeColumns - 1;
                        break;

                    case 1:
                        edge = 30 * (row / 3) + ((codeRows - 1) / 3);
                        break;

                    default:
                        edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3);
                        break;

                }
                outCodeword(cluster[edge]);
                outStopPattern();
            }
            if ((options & PDF417_INVERT_BITMAP) != 0)
            {
                for (int k = 0; k < outBits.Length; ++k)
                    //outBits[k] ^= 0xff;
                    outBits[k] ^= unchecked((sbyte)0xff);
            }
        }

        protected internal virtual void calculateErrorCorrection(int dest)
        {
            if (errorLevel < 0 || errorLevel > 8)
                errorLevel = 0;
            int[] A = ERROR_LEVEL[errorLevel];
            int Alength = 2 << errorLevel;
            for (int k = 0; k < Alength; ++k)
                codewords[dest + k] = 0;
            int lastE = Alength - 1;
            for (int k = 0; k < lenCodewords; ++k)
            {
                int t1 = codewords[k] + codewords[dest];
                for (int e = 0; e <= lastE; ++e)
                {
                    int t2 = (t1 * A[lastE - e]) % MOD;
                    int t3 = MOD - t2;
                    codewords[dest + e] = ((e == lastE ? 0 : codewords[dest + e + 1]) + t3) % MOD;
                }
            }
            for (int k = 0; k < Alength; ++k)
                codewords[dest + k] = (MOD - codewords[dest + k]) % MOD;
        }

        protected internal virtual int getTextTypeAndValue(int maxLength, int idx)
        {
            if (idx >= maxLength)
                return 0;
            char c = (char)(text[idx] & 0xff);
            if (c >= 'A' && c <= 'Z')
                return (ALPHA + c - 'A');
            if (c >= 'a' && c <= 'z')
                return (LOWER + c - 'a');
            if (c == ' ')
                return (ALPHA + LOWER + MIXED + SPACE);
            int ms = MIXED_SET.IndexOf((System.Char)c);
            int ps = PUNCTUATION_SET.IndexOf((System.Char)c);
            if (ms < 0 && ps < 0)
                return (ISBYTE + c);

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是小数位

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值