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

📄 iec16022ecc200.cs

📁 DataMatrix二维条码识别算法,基于Windows
💻 CS
📖 第 1 页 / 共 4 页
字号:
                                    {
                                        // special case, switch to ASCII for last 1 ot two bytes
                                        bs = (char)4;
                                        bl = t;
                                        b = (char)EncType.E_ASCII;
                                    }
                                }
                            }
                        }
                    }
                    enc[p, (int)EncType.E_EDIFACT].t = (short)bl;
                    enc[p, (int)EncType.E_EDIFACT].s = (short)bs;
                    if (bl != 0 && b == (int)EncType.E_EDIFACT)
                        enc[p, b].s += enc[p + bs, b].s;
                }
                // Binary
                bl = 0;
                for (e = (char)0; e < (int)EncType.E_MAX; e++)
                    if (enc[p + 1, e].t != 0
                        &&
                        ((t =
                          enc[p + 1, e].t + switchcost[(int)EncType.E_BINARY, e] +
                          ((e == (int)EncType.E_BINARY
                        && enc[p + 1, e].t == 249) ? 1 : 0))
                         < bl || !(bl != 0)))
                    {
                        bl = t;
                        b = e;
                    }
                enc[p, (int)EncType.E_BINARY].t = (short)(1 + bl);
                enc[p, (int)EncType.E_BINARY].s = (short)1;
                if (bl != 0 && b == (int)EncType.E_BINARY)
                    enc[p, b].s += enc[p + 1, b].s;
                /*
                 * fprintf (stderr, "%d:", p); for (e = 0; e < E_MAX; e++) fprintf \
                 * (stderr, " %c*%d/%d", encchr[e], enc[p][e].s, enc[p][e].t); \
                 * fprintf (stderr, "\n");
                 */
            }


            //char* encoding ;
            //encoding = safemalloc(l + 1);


            if (true)
            {
                // VECCHIA
                byte[] encoding = new byte[l + 1];
                p = 0;
                {
                    char cur = (char)EncType.E_ASCII;	// starts ASCII
                    while (p < l)
                    {
                        int t = 0;
                        int m = 0;
                        char b = (char)0;
                        for (e = (char)0; e < (int)EncType.E_MAX; e++)
                            if (enc[p, e].t != 0
                                && ((t = enc[p, e].t + switchcost[cur, e]) <
                                m || t == m && e == cur || !(m != 0)))
                            {
                                b = e;
                                m = t;
                            }
                        cur = b;
                        m = enc[p, b].s;

                        if (!(p != 0) && lenp != 0)
                            lenp = enc[p, b].t;

                        while (p < l && (m--) != 0)
                            encoding[p++] = (byte)encchr[b];
                    }
                }
                encoding[p] = 0;
                return encoding;

            }
            else
            {
                //// NUOVA
                //byte[] encoding = new byte[l];
                //p = 0;
                //{
                //    char cur = (char)EncType.E_ASCII;	// starts ASCII
                //    while (p < l)
                //    {
                //        int t = 0;
                //        int m = 0;
                //        char b = (char)0;
                //        for (e = (char)0; e < (int)EncType.E_MAX; e++)
                //            if (enc[p, e].t != 0
                //                && ((t = enc[p, e].t + switchcost[cur, e]) <
                //                m || t == m && e == cur || !(m != 0)))
                //            {
                //                b = e;
                //                m = t;
                //            }
                //        cur = b;
                //        m = enc[p, b].s;

                //        if (!(p != 0) && lenp != 0)
                //            lenp = enc[p, b].t;

                //        while (p < l && (m--) != 0)
                //            encoding[p++] = (byte)encchr[b];
                //    }
                //}
                ////encoding[p] = 0;
                //return encoding;
            }
        }





        public byte[] iec16022ecc200(ref int Wptr, ref int Hptr, ref  byte[] encodingptr, int barcodelen, byte[] barcode, ref int lenp, ref int maxp, ref  int eccp)
        {
            byte[] binary = new byte[3000];	// encoded raw data and ecc to place in barcode
            int W = 0, H = 0;
            byte[] encoding = null;
            byte[] grid;

            int matrix_IDX;    //struct ecc200matrix_s *matrix;

            //memset(binary, 0, sizeof(binary));

            if (encodingptr != null)
                encoding = encodingptr;
            if (Wptr != 0)
                W = Wptr;
            if (Hptr != 0)
                H = Hptr;

            // encoding
            if (W != 0)
            {
                // known size

                // in C era:  for (matrix = ecc200matrix; matrix->W && (matrix->W != W || matrix->H != H); matrix++) ;
                for (matrix_IDX = 0; ecc200matrix[matrix_IDX].W != 0 && (ecc200matrix[matrix_IDX].W != W || ecc200matrix[matrix_IDX].H != H); matrix_IDX++) ;


                if (!(ecc200matrix[matrix_IDX].W != 0))
                {
                    _errorMessage = "Invalid size " + W + "x" + H;
                    return null;
                }
                if (encoding == null)
                {
                    int len = 0;
                    byte[] e = encmake(barcodelen, barcode, ref len, (char)1);
                    if (e != null && len != ecc200matrix[matrix_IDX].Bytes)
                    {
                        // try not an exact fit
                        e = encmake(barcodelen, barcode, ref len, (char)0);
                        if (len > ecc200matrix[matrix_IDX].Bytes)
                        {
                            _errorMessage = "Cannot make barcode fit " + W + "x" + H;
                            return null;
                        }
                    }
                    encoding = e;
                }
            }
            else
            {
                // find a suitable encoding
                int dummyint = 0;
                if (encoding == null)
                    encoding = encmake(barcodelen, barcode, ref dummyint, (char)1);

                if (encoding != null)
                {
                    // find one that fits chosen encoding
                    matrix_IDX = 0;
                    for (; ecc200matrix[matrix_IDX].W != 0; matrix_IDX++)
                    {
                        int dummyInt2 = 0;
                        char rv = ecc200encode(binary, ecc200matrix[matrix_IDX].Bytes, barcode, barcodelen, encoding, ref dummyInt2);
                        if (!(rv == (char)0))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    int len = 0;
                    byte[] e;
                    e = encmake(barcodelen, barcode, ref len, (char)1);
                    for (matrix_IDX = 0; ecc200matrix[matrix_IDX].W != 0 && ecc200matrix[matrix_IDX].Bytes != len; matrix_IDX++) ;
                    if (e != null && !(ecc200matrix[matrix_IDX].W != 0))
                    {
                        // try for non exact fit
                        e = encmake(barcodelen, barcode, ref len, (char)0);
                        for (matrix_IDX = 0; ecc200matrix[matrix_IDX].W != 0 && ecc200matrix[matrix_IDX].Bytes < len; matrix_IDX++) ;
                    }
                    encoding = e;
                }
                if (!(ecc200matrix[matrix_IDX].W != 0))
                {
                    _errorMessage = "Cannot find suitable size, barcode too long";
                    return null;
                }
                W = ecc200matrix[matrix_IDX].W;
                H = ecc200matrix[matrix_IDX].H;
            }


            //FABRY DEBUG
            //Console.WriteLine(Encoding.ASCII.GetString(barcode));
            //Console.WriteLine(Encoding.ASCII.GetString(encoding));


            if (!(ecc200encode(binary, ecc200matrix[matrix_IDX].Bytes, barcode, barcodelen, encoding, ref lenp) != 0))
            {
                _errorMessage = "Barcode too long for " + W + "x" + H;
                return null;
            }


            // ecc code
            ecc200(binary, ecc200matrix[matrix_IDX].Bytes, ecc200matrix[matrix_IDX].Datablock, ecc200matrix[matrix_IDX].RSblock);
            {			
                // placement
                int x, y, NC, NR;
                int[] places;

                NC = W - 2 * (W / ecc200matrix[matrix_IDX].FW);
                NR = H - 2 * (H / ecc200matrix[matrix_IDX].FH);

                places = new int[NC * NR];   // safemalloc(NC * NR * sizeof(int));

                ecc200placement(places, NR, NC);
                grid = new byte[W * H];      // safemalloc(W * H);

                for (y = 0; y < H; y += ecc200matrix[matrix_IDX].FH)
                {
                    for (x = 0; x < W; x++)
                        grid[y * W + x] = 1;
                    for (x = 0; x < W; x += 2)
                        grid[(y + ecc200matrix[matrix_IDX].FH - 1) * W + x] = 1;
                }
                for (x = 0; x < W; x += ecc200matrix[matrix_IDX].FW)
                {
                    for (y = 0; y < H; y++)
                        grid[y * W + x] = 1;
                    for (y = 0; y < H; y += 2)
                        grid[y * W + x + ecc200matrix[matrix_IDX].FW - 1] = 1;
                }
                for (y = 0; y < NR; y++)
                {
                    for (x = 0; x < NC; x++)
                    {
                        int v = places[(NR - y - 1) * NC + x];
                        if (v == 1 || v > 7 && (binary[(v >> 3) - 1] & (1 << (v & 7))) != 0)
                            grid[(1 + y + 2 * (y / (ecc200matrix[matrix_IDX].FH - 2))) * W +
                                1 + x + 2 * (x / (ecc200matrix[matrix_IDX].FW - 2))] = 1;
                    }
                }
            }


            Wptr = W;
            Hptr = H;
            encodingptr = encoding;
            maxp = ecc200matrix[matrix_IDX].Bytes;
            eccp = (ecc200matrix[matrix_IDX].Bytes + 2) / ecc200matrix[matrix_IDX].Datablock * ecc200matrix[matrix_IDX].RSblock;

            return grid;
        }

    }

}

⌨️ 快捷键说明

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