PDF417条码生成类

本人网上看了有几个人需求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);

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
TCPDF是一种用于生成PDF文档的PHP库,而PDF417是一种二维条码格式。下面是使用TCPDF生成PDF417二维条码的步骤: 1. 首先,你需要引入TCPDF库。可以通过下载TCPDF库并将其放置在你的项目中,然后使用`require_once`函数将其引入。 2. 创建一个TCPDF实例。你可以使用`new TCPDF()`来创建一个新的TCPDF对象。 3. 配置TCPDF对象。你可以设置文档属性,如页面大小、页面方向、字体等。你可以使用TCPDF提供的方法来设置这些属性。 4. 创建一个新的页面,并设置页面的大小和方向。 5. 使用TCPDF提供的方法来生成PDF417二维条码。你可以使用`write2DBarcode`方法来生成二维条码,传入需要生成条码的数据和条码型(在这种情况下是PDF417)。 6. 输出PDF文档。你可以使用`Output`方法将生成PDF文档输出到浏览器或保存到文件中。 7. 最后,记得调用`Close`方法关闭TCPDF对象。 使用TCPDF生成PDF417二维条码的示例代码如下: ```php require_once('tcpdf/tcpdf.php'); $pdf = new TCPDF(); // 创建TCPDF实例 $pdf->SetCreator('MyPDFCreator'); $pdf->SetAuthor('Me'); $pdf->SetTitle('PDF417 Barcode Generator'); $pdf->SetMargins(10, 10, 10); // 设置页面边距 $pdf->AddPage(); // 创建一个新的页面 $pdf->SetXY(10, 10); // 设置初始坐标 $barcodeData = 'Hello, PDF417!'; // 设置需要生成条码的数据 $pdf->write2DBarcode($barcodeData, 'PDF417'); // 生成PDF417二维条码 $pdf->Output('pdf417_barcode.pdf', 'D'); // 输出PDF文档并保存为文件 $pdf->Close(); // 关闭TCPDF对象 ``` 这样,你就可以使用TCPDF库在PHP中生成PDF417二维条码
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我是小数位

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

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

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

打赏作者

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

抵扣说明:

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

余额充值