Пример #1
0
        /// <summary>Розпінає текст</summary>
        /// <param name="bmp">Зображення для розпізнавання</param>
        /// <param name="allowedChars">Список дозволених символів</param>
        /// <param name="textColor">Колір, яким написаний текст</param>
        public string RecognizeText(Bitmap bmp, char[] allowedChars, Color textColor, object maxDiff, int maxColorDiff, Size expand)
        {
            if (!loaded)
            {
                Load();
            }

            if (textColor != Color.Empty)
            {
                BitmapProcessor.BlackAndWhite(ref bmp, textColor, maxColorDiff);
            }

            Bitmap expanded = BitmapProcessor.Expand(bmp, expand);

            bmp.Dispose();
            bmp = expanded;

            BitmapProcessor.Trim(ref bmp, 2);

            string result = new string(' ', bmp.Width);

            if (BitmapProcessor.IsEmpty(bmp))
            {
                return("");
            }

            foreach (CharCollection cc in chars)
            {
                foreach (Char c in cc.chars)
                {
                    if (allowedChars != null && !allowedChars.Contains(c.c))
                    {
                        continue;
                    }

                    int maxDiffInt = 0;

                    if (maxDiff is int)
                    {
                        maxDiffInt = (int)maxDiff;
                    }
                    else if (maxDiff is double)
                    {
                        maxDiffInt = (int)((double)c.bmp.Size.Width * (double)c.bmp.Size.Height * (double)maxDiff);
                    }

                    List <int> pos = BitmapProcessor.FindSmallerSubimage(bmp, c.bmp, maxDiffInt, int.MaxValue, false);

                    foreach (int p in pos)
                    {
                        bool ok = true;

                        for (int i = 0; i < c.bmp.Width; i++)
                        {
                            if (result[p + i] != ' ')
                            {
                                ok = false;
                                break;
                            }
                        }

                        if (ok)
                        {
                            result = result.Remove(p, 1);
                            result = result.Insert(p, c.c + "");

                            for (int i = 1; i < c.bmp.Width; i++)
                            {
                                if (result[p + i] == ' ')
                                {
                                    result = result.Remove(p + i, 1);
                                    result = result.Insert(p + i, "^");
                                }
                            }
                        }
                    }
                }
            }

            result = result.Replace("^", "");
            result = result.Replace(new string(' ', _spaceLength), "`");
            result = result.Replace(" ", "");
            result = result.Replace("`", " ");
            if (allowedChars != null && !allowedChars.Contains(' '))
            {
                result = result.Replace(" ", "");
            }
            result = result.Trim();

            return(result);
        }