Ejemplo n.º 1
0
        public static bool RecognizeText(int x, int y, Size regionSize, string searchWord, out Point hitPoint)
        {
            bool result = false;

            hitPoint = new Point(0, 0);
            if (ocrEngine == null)
            {
                return(result);
            }

            Point  origin = new Point(x - regionSize.Width / 2, y - regionSize.Height / 2);
            Bitmap b      = new Bitmap(regionSize.Width, regionSize.Height);

            using (Graphics g = Graphics.FromImage(b))
            {
                g.CopyFromScreen(origin.X, origin.Y, 0, 0, regionSize, CopyPixelOperation.SourceCopy);
            }
            //b = new Bitmap(Image.FromFile("Capture.JPG"));

            Point      imageLocalPoint;
            IOcrResult ocrResult = ocrEngine.Recognize(b);

            // Try finding variations of the search word for all common mis-recognitions
            List <string> variations = new List <string>();

            variations.Add(searchWord);
            if (searchWord.ToLower().Contains('l'))
            {
                variations.Add(searchWord.ToLower().Replace('l', 'I'));
            }
            if (searchWord.ToUpper().Contains('O'))
            {
                variations.Add(searchWord.ToUpper().Replace('O', '0'));
            }

            foreach (string v in variations)
            {
                if (ocrResult.FindWord(v, out imageLocalPoint))
                {
                    hitPoint = new Point(origin.X + imageLocalPoint.X, origin.Y + imageLocalPoint.Y);
                    result   = true;
                    break;
                }
            }


            string text = "Search word: '" + searchWord + "'\r\n";

            text += ocrResult.GetDebugText();

            ocrResult.Dispose();

            mainForm.Invoke((Action)(() =>
            {
                if (!result)
                {
                    if (ocrResponseForm == null)
                    {
                        ocrResponseForm = new OcrResponseForm();
                        ocrResponseForm.FormClosed += OnOcrResponseFormClosed;
                        ocrResponseForm.FormClosing += OnOcrResponseFormClosing;
                    }

                    string responseText = searchWord + "?";
                    ocrResponseForm.Show(responseText);

                    System.Drawing.Rectangle screenBounds = Screen.PrimaryScreen.Bounds;
                    ocrResponseForm.Left = screenBounds.Right - ocrResponseForm.Width;
                    ocrResponseForm.Top = screenBounds.Top;
                }
                {
                    if (ocrDebugForm == null)
                    {
                        ocrDebugForm = new OcrDebugForm();
                        ocrDebugForm.FormClosed += OnOcrFormClosed;
                        ocrDebugForm.FormClosing += OnOcrFormClosing;
                    }
                    if (ocrDebugForm.ocrPicture.Image != null)
                    {
                        ocrDebugForm.ocrPicture.Image.Dispose();
                    }
                    ocrDebugForm.ocrPicture.Image = b;
                    ocrDebugForm.ocrText.Text = text;
                }
            }));

            return(result);
        }
Ejemplo n.º 2
0
 public static void OnOcrResponseFormClosed(Object sender, FormClosedEventArgs e)
 {
     ocrResponseForm = null;
 }