Beispiel #1
0
        public Size GetCharSize(char c, Color backgroundColor)
        {
            Bitmap bmp = new Bitmap(GetChar(c));

            BitmapProcessor.Trim(ref bmp);

            int minY = bmp.Height;
            int maxY = 0;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    Color color = bmp.GetPixel(x, y);
                    if (!BitmapProcessor.ColorsEqual(color, backgroundColor))
                    {
                        if (y < minY)
                        {
                            minY = y;
                        }
                        if (y > maxY)
                        {
                            maxY = y;
                        }
                        break;
                    }
                }
            }

            int width = bmp.Width;

            bmp.Dispose();

            return(new Size(width, maxY - minY + 1));
        }
Beispiel #2
0
        /// <summary>Повертає зображення символа</summary>
        /// <param name="c">Символ</param>
        protected override Bitmap GetChar(char c)
        {
            Bitmap bitmap = new Bitmap((int)(fontHeight * 3), _imageHeight);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.DrawString(c + "", new System.Drawing.Font(family, fontHeight, fontStyle), new SolidBrush(Color.White), 0, 0);

            BitmapProcessor.BlackAndWhite(ref bitmap, Color.White);
            BitmapProcessor.Trim(ref bitmap, leave.ContainsKey(c) ? leave[c] : 0);

            return(bitmap);
        }
Beispiel #3
0
        /// <summary>Задає висоту зображення шрифта</summary>
        protected virtual void SetImageHeight()
        {
            Bitmap d = GetChar('(');

            BitmapProcessor.Trim(ref d);

            for (int y = d.Height - 1; y >= 0; y--)
            {
                for (int x = 0; x < d.Width; x++)
                {
                    if (d.GetPixel(x, y).R != 255)
                    {
                        _imageHeight = y + 1;
                        return;
                    }
                }
            }
        }
Beispiel #4
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);
        }