⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iec16022ecc200.cs

📁 DataMatrix二维条码识别算法,基于Windows
💻 CS
📖 第 1 页 / 共 4 页
字号:
/** 
 *
 * IEC16022Sharp DataMatrix bar code generation lib
 *   Fabrizio Accatino
 * 
 *   C# porting of:
 *      IEC16022 bar code generation
 *      Adrian Kennard, Andrews & Arnold Ltd
 *      with help from Cliff Hones on the RS coding
 *      (C version currently maintained by Stefan Schmidt)
 * 
 * (c) 2004 Adrian Kennard, Andrews & Arnold Ltd
 * (c) 2006 Stefan Schmidt <stefan@datenfreihafen.org>
 * (c) 2007 Fabrizio Accatino <fhtino@yahoo.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */


using System;
using System.Collections.Generic;
using System.Text;



namespace IEC16022Sharp
{

    internal class IEC16022ecc200
    {


        #region FABRY: ADDED

        private string _errorMessage = null;
        public string ErrorMessage { get { return _errorMessage; } }

        #endregion



        #region FABRY: REWRITE

        private bool isdigit(byte b)
        {
            return (b >= '0' && b <= '9');
        }

        private bool isupper(byte b)
        {
            return (b >= 'A' && b <= 'Z');
        }

        private bool islower(byte b)
        {
            return (b >= 'a' && b <= 'z');
        }

        private char tolower(byte b)
        {
            return ((char)b).ToString().ToLower()[0];
            // TODO: risciverlo meglio usando gli intervalli ascii
            // Tipo....
            //if (b >= 'A' && b <= 'Z')
            //    return (char)('a' + b - 'A');
            //else
            //    return (char)b;
        }

        #endregion




        private struct TempStruct1
        {
            // number of bytes of source that can be encoded in a row at this point
            // using this encoding mode
            public short s;
            // number of bytes of target generated encoding from this point to end if
            // already in this encoding mode
            public short t;
        } ;



        private int MAXBARCODE = 3116;

        private char[] encchr = "ACTXEB".ToCharArray();

        private ecc200matrix_s[] ecc200matrix = new ecc200matrix_s[] {
            new ecc200matrix_s(10, 10, 10, 10, 3, 3, 5),	//
            new ecc200matrix_s(12, 12, 12, 12, 5, 5, 7),	//
            new ecc200matrix_s(8, 18, 8, 18, 5, 5, 7),	//
            new ecc200matrix_s(14, 14, 14, 14, 8, 8, 10),	//
            new ecc200matrix_s(8, 32, 8, 16, 10, 10, 11),	//
            new ecc200matrix_s(16, 16, 16, 16, 12, 12, 12),	//
            new ecc200matrix_s(12, 26, 12, 26, 16, 16, 14),	//
            new ecc200matrix_s(18, 18, 18, 18, 18, 18, 14),	//
            new ecc200matrix_s(20, 20, 20, 20, 22, 22, 18),	//
            new ecc200matrix_s(12, 36, 12, 18, 22, 22, 18),	//
            new ecc200matrix_s(22, 22, 22, 22, 30, 30, 20),	//
            new ecc200matrix_s(16, 36, 16, 18, 32, 32, 24),	//
            new ecc200matrix_s(24, 24, 24, 24, 36, 36, 24),	//
            new ecc200matrix_s(26, 26, 26, 26, 44, 44, 28),	//
            new ecc200matrix_s(16, 48, 16, 24, 49, 49, 28),	//
            new ecc200matrix_s(32, 32, 16, 16, 62, 62, 36),	//
            new ecc200matrix_s(36, 36, 18, 18, 86, 86, 42),	//
            new ecc200matrix_s(40, 40, 20, 20, 114, 114, 48),	//
            new ecc200matrix_s(44, 44, 22, 22, 144, 144, 56),	//
            new ecc200matrix_s(48, 48, 24, 24, 174, 174, 68),	//
            new ecc200matrix_s(52, 52, 26, 26, 204, 102, 42),	//
            new ecc200matrix_s(64, 64, 16, 16, 280, 140, 56),	//
            new ecc200matrix_s(72, 72, 18, 18, 368, 92, 36),	//
            new ecc200matrix_s(80, 80, 20, 20, 456, 114, 48),	//
            new ecc200matrix_s(88, 88, 22, 22, 576, 144, 56),	//
            new ecc200matrix_s(96, 96, 24, 24, 696, 174, 68),	//
            new ecc200matrix_s(104, 104, 26, 26, 816, 136, 56),	//
            new ecc200matrix_s(120, 120, 20, 20, 1050, 175, 68),	//
            new ecc200matrix_s(132, 132, 22, 22, 1304, 163, 62),	//
            new ecc200matrix_s(144, 144, 24, 24, 1558, 156, 62)	// 156*4+155*2
        };


        //Enum.GetNames(typeof(EncType)).Length

        byte[,] switchcost = new byte[6, 6]  {
            {0, 1, 1, 1, 1, 2},	// From E_ASCII
            {1, 0, 2, 2, 2, 3},	// From E_C40
            {1, 2, 0, 2, 2, 3},	// From E_TEXT
            {1, 2, 2, 0, 2, 3},	// From E_X12
            {1, 2, 2, 2, 0, 3},	// From E_EDIFACT
            {0, 1, 1, 1, 1, 0}	// From E_BINARY
        };



        private void ecc200placementbit(int[] array, int NR, int NC, int r, int c, int p, char b)
        {
            if (r < 0)
            {
                r += NR;
                c += 4 - ((NR + 4) % 8);
            }
            if (c < 0)
            {
                c += NC;
                r += 4 - ((NC + 4) % 8);
            }
            array[r * NC + c] = (p << 3) + b;
        }


        private void ecc200placementblock(int[] array, int NR, int NC, int r, int c, int p)
        {
            ecc200placementbit(array, NR, NC, r - 2, c - 2, p, (char)7);
            ecc200placementbit(array, NR, NC, r - 2, c - 1, p, (char)6);
            ecc200placementbit(array, NR, NC, r - 1, c - 2, p, (char)5);
            ecc200placementbit(array, NR, NC, r - 1, c - 1, p, (char)4);
            ecc200placementbit(array, NR, NC, r - 1, c - 0, p, (char)3);
            ecc200placementbit(array, NR, NC, r - 0, c - 2, p, (char)2);
            ecc200placementbit(array, NR, NC, r - 0, c - 1, p, (char)1);
            ecc200placementbit(array, NR, NC, r - 0, c - 0, p, (char)0);
        }

        private void ecc200placementcornerA(int[] array, int NR, int NC, int p)
        {
            ecc200placementbit(array, NR, NC, NR - 1, 0, p, (char)7);
            ecc200placementbit(array, NR, NC, NR - 1, 1, p, (char)6);
            ecc200placementbit(array, NR, NC, NR - 1, 2, p, (char)5);
            ecc200placementbit(array, NR, NC, 0, NC - 2, p, (char)4);
            ecc200placementbit(array, NR, NC, 0, NC - 1, p, (char)3);
            ecc200placementbit(array, NR, NC, 1, NC - 1, p, (char)2);
            ecc200placementbit(array, NR, NC, 2, NC - 1, p, (char)1);
            ecc200placementbit(array, NR, NC, 3, NC - 1, p, (char)0);
        }


        private void ecc200placementcornerB(int[] array, int NR, int NC, int p)
        {
            ecc200placementbit(array, NR, NC, NR - 3, 0, p, (char)7);
            ecc200placementbit(array, NR, NC, NR - 2, 0, p, (char)6);
            ecc200placementbit(array, NR, NC, NR - 1, 0, p, (char)5);
            ecc200placementbit(array, NR, NC, 0, NC - 4, p, (char)4);
            ecc200placementbit(array, NR, NC, 0, NC - 3, p, (char)3);
            ecc200placementbit(array, NR, NC, 0, NC - 2, p, (char)2);
            ecc200placementbit(array, NR, NC, 0, NC - 1, p, (char)1);
            ecc200placementbit(array, NR, NC, 1, NC - 1, p, (char)0);
        }

        private void ecc200placementcornerC(int[] array, int NR, int NC, int p)
        {
            ecc200placementbit(array, NR, NC, NR - 3, 0, p, (char)7);
            ecc200placementbit(array, NR, NC, NR - 2, 0, p, (char)6);
            ecc200placementbit(array, NR, NC, NR - 1, 0, p, (char)5);
            ecc200placementbit(array, NR, NC, 0, NC - 2, p, (char)4);
            ecc200placementbit(array, NR, NC, 0, NC - 1, p, (char)3);
            ecc200placementbit(array, NR, NC, 1, NC - 1, p, (char)2);
            ecc200placementbit(array, NR, NC, 2, NC - 1, p, (char)1);
            ecc200placementbit(array, NR, NC, 3, NC - 1, p, (char)0);
        }

        private void ecc200placementcornerD(int[] array, int NR, int NC, int p)
        {
            ecc200placementbit(array, NR, NC, NR - 1, 0, p, (char)7);
            ecc200placementbit(array, NR, NC, NR - 1, NC - 1, p, (char)6);
            ecc200placementbit(array, NR, NC, 0, NC - 3, p, (char)5);
            ecc200placementbit(array, NR, NC, 0, NC - 2, p, (char)4);
            ecc200placementbit(array, NR, NC, 0, NC - 1, p, (char)3);
            ecc200placementbit(array, NR, NC, 1, NC - 3, p, (char)2);
            ecc200placementbit(array, NR, NC, 1, NC - 2, p, (char)1);
            ecc200placementbit(array, NR, NC, 1, NC - 1, p, (char)0);
        }



        // NOTA da C a C#:  if( c )  -->  if ( (c!=0) )
        // unsigned char --> byte

        private void ecc200placement(int[] array, int NR, int NC)
        {
            int r, c, p;
            // invalidate
            for (r = 0; r < NR; r++)
                for (c = 0; c < NC; c++)
                    array[r * NC + c] = 0;
            // start
            p = 1;
            r = 4;
            c = 0;
            do
            {
                // check corner
                if (r == NR && !(c != 0))
                    ecc200placementcornerA(array, NR, NC, p++);
                if ((r == NR - 2) && !(c != 0) && ((NC % 4) != 0))
                    ecc200placementcornerB(array, NR, NC, p++);
                if (r == NR - 2 && !(c != 0) && (NC % 8) == 4)
                    ecc200placementcornerC(array, NR, NC, p++);
                if (r == NR + 4 && c == 2 && !((NC % 8) != 0))
                    ecc200placementcornerD(array, NR, NC, p++);
                // up/right
                do
                {
                    if (r < NR && c >= 0 && !(array[r * NC + c] != 0))
                        ecc200placementblock(array, NR, NC, r, c, p++);
                    r -= 2;
                    c += 2;
                }
                while (r >= 0 && c < NC);
                r++;
                c += 3;
                // down/left
                do
                {
                    if (r >= 0 && c < NC && !(array[r * NC + c] != 0))
                        ecc200placementblock(array, NR, NC, r, c, p++);
                    r += 2;
                    c -= 2;
                }
                while (r < NR && c >= 0);
                r += 3;
                c++;
            }
            while (r < NR || c < NC);
            // unfilled corner
            if (!(array[NR * NC - 1] != 0))
                array[NR * NC - 1] = array[NR * NC - NC - 2] = 1;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -