Exemplo n.º 1
0
        public static Rectangle ParseRectanglePositionFromArguments(Rectangle screenSearchArea, bool relative)
        {
            Rectangle result = screenSearchArea;

            if (result.X < 0 ||
                result.Y < 0 ||
                result.Width < 1 ||
                result.Height < 1)
            {
                throw new ArgumentException("ScreenSearchArea argument's parts can't be negative. Both width and height must be bigger than zero.");
            }

            if (relative)
            {
                IntPtr handle = RobotWin32.GetForegroundWindow();
                if (handle.ToInt32() == 0)
                {
                    throw new ApplicationException("Cannot find foreground window.");
                }
                RobotWin32.Rect foregroundWindowRect = new RobotWin32.Rect();
                if (RobotWin32.GetWindowRectangle(handle, ref foregroundWindowRect) == false)
                {
                    throw new ApplicationException("Cannot get foreground window rect.");
                }

                result = new Rectangle(result.X + foregroundWindowRect.Left,
                                       result.Y + foregroundWindowRect.Top,
                                       result.Width,
                                       result.Height);
            }
            return(result);
        }
        public void Execute(Arguments arguments)
        {
            var rectangle = arguments.Area.Value;

            if (!rectangle.IsValidRectangle())
            {
                throw new ArgumentException("Argument Area is not a valid rectangle");
            }
            if (arguments.Relative.Value)
            {
                var foregroundWindowRect = new RobotWin32.Rect();
                RobotWin32.GetWindowRectangle(RobotWin32.GetForegroundWindow(), ref foregroundWindowRect);
                rectangle = new Rectangle(rectangle.X + foregroundWindowRect.Left,
                                          rectangle.Y + foregroundWindowRect.Top,
                                          Math.Min(rectangle.Width, foregroundWindowRect.Right - foregroundWindowRect.Left) - rectangle.X,
                                          Math.Min(rectangle.Height, foregroundWindowRect.Bottom - foregroundWindowRect.Top) - rectangle.Y);
            }
            var partOfScreen = RobotWin32.GetPartOfScreen(rectangle);
            var imgToParse   = OcrOfflineHelper.RescaleImage(partOfScreen, arguments.Sensitivity.Value);
            var language     = arguments.Language.Value;
            var dataPath     = OcrOfflineHelper.GetResourcesFolder(language);

            try
            {
                using (var tEngine = new TesseractEngine(dataPath, language, EngineMode.TesseractAndCube))
                    using (var img = PixConverter.ToPix(imgToParse))
                        using (var page = tEngine.Process(img))
                        {
                            var text = page.GetText();
                            if (string.IsNullOrEmpty(text))
                            {
                                throw new NullReferenceException("Ocr was unable to find any text");
                            }
                            Scripter.Variables.SetVariableValue(arguments.Result.Value, new Language.TextStructure(text));
                        }
            }
            catch (TesseractException)
            {
                throw new ApplicationException("Ocr engine exception, possibly missing language data in folder: " + dataPath);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 3
0
        public Rectangle GetRectangle()
        {
            if (AutomationElement.Properties.BoundingRectangle.TryGetValue(out var boundingRectNoDefault))
            {
                return(boundingRectNoDefault);
            }

            if (AutomationElement.FrameworkAutomationElement.NativeWindowHandle != IntPtr.Zero)
            {
                var    rect      = new RobotWin32.Rect();
                IntPtr wndHandle = AutomationElement.FrameworkAutomationElement.NativeWindowHandle;
                if (RobotWin32.GetWindowRectangle(wndHandle, ref rect))
                {
                    return(new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top));
                }
            }
            throw new NotSupportedException("Cannot get rectangle for that kind of UI element.");
        }
        public void RelativeTest()
        {
            IntPtr hTesterAppWindow = testerApp.MainWindowHandle;

            RobotWin32.Rect windowRect = new RobotWin32.Rect();
            RobotWin32.GetWindowRectangle(hTesterAppWindow, ref windowRect);
            int titleBarHeight = 24;

            scripter.Text = ($"ocrabbyy.processscreen area {SpecialChars.Text}0,0,{windowRect.Right - windowRect.Left},{titleBarHeight}{SpecialChars.Text} relative true");
            scripter.Run();
            int documentId = scripter.Variables.GetVariableValue <int>("result");

            FineReaderDocument document = AbbyyManager.Instance.GetDocument(documentId);

            Assert.IsNotNull(document);
            string plainText = document.GetAllText();

            Assert.IsTrue(plainText.Contains(appTitle));
        }
Exemplo n.º 5
0
        public System.Windows.Rect GetRectangle()
        {
            object boundingRectNoDefault =
                automationElement.GetCurrentPropertyValue(AutomationElement.BoundingRectangleProperty, true);

            if (boundingRectNoDefault != AutomationElement.NotSupported)
            {
                return((System.Windows.Rect)boundingRectNoDefault);
            }
            else if (automationElement.Current.NativeWindowHandle != 0)
            {
                RobotWin32.Rect rect      = new RobotWin32.Rect();
                IntPtr          wndHandle = new IntPtr(automationElement.Current.NativeWindowHandle);
                if (RobotWin32.GetWindowRectangle(wndHandle, ref rect))
                {
                    return(new System.Windows.Rect(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top));
                }
            }
            throw new NotSupportedException("Cannot get rectangle for that kind of UI element.");
        }
        public void FindWindowTitleTest()
        {
            string   appTitle = "TestApp";
            Scripter scripter = new Scripter();

            scripter.InitVariables.Clear();
            System.Diagnostics.Process testerApp       = null;
            List <GStruct.Structure>   windowTitleRect = null;

            try
            {
                scripter = new Scripter();
                scripter.InitVariables.Clear();
                testerApp = AbbyTests.StartFormTester($"Title {appTitle}");
                IntPtr          hTesterAppWindow = testerApp.MainWindowHandle;
                RobotWin32.Rect windowRect       = new RobotWin32.Rect();
                RobotWin32.GetWindowRectangle(hTesterAppWindow, ref windowRect);
                int titleBarHeight = 24;

                scripter.Text = ($@"ocrabbyy.processscreen area {SpecialChars.Text}{windowRect.Left},{windowRect.Top},{windowRect.Right},{windowRect.Bottom}{SpecialChars.Text}
                                    ocrabbyy.find {SpecialChars.Text}{appTitle}{SpecialChars.Text} result {nameof(windowTitleRect)}");
                scripter.Run();
                windowTitleRect = scripter.Variables.GetVariableValue <List <GStruct.Structure> >(nameof(windowTitleRect));
                Assert.AreNotEqual(0, windowTitleRect.Count);
                System.Drawing.Rectangle titleRect = ((GStruct.RectangleStructure)windowTitleRect[0]).Value;

                Assert.IsTrue(titleRect.Top > 0 & titleRect.Top < titleBarHeight, "Top edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Bottom > 0 & titleRect.Bottom <= titleBarHeight, "Bottom edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Left > 0 & titleRect.Left < 50, "Left edge position of found rectangle is incorrect");
                Assert.IsTrue(titleRect.Right > 0 & titleRect.Right < windowRect.Right);
            }
            finally
            {
                if (testerApp != null && !testerApp.HasExited)
                {
                    testerApp.Kill();
                }
            }
        }
        public void LanguageTest()
        {
            IntPtr hTesterAppWindow = testerApp.MainWindowHandle;

            RobotWin32.Rect windowRect = new RobotWin32.Rect();
            RobotWin32.GetWindowRectangle(hTesterAppWindow, ref windowRect);
            int titleBarHeight = 24;

            string appTitle2 = "шестьсот";  // in case someone worry what this mean, it's six hundred

            scripter.Text = ($@"keyboard {SpecialChars.Text}title {appTitle2}{SpecialChars.Text}{SpecialChars.KeyBegin}enter{SpecialChars.KeyEnd}
                                ocrabbyy.processscreen area {SpecialChars.Text}{windowRect.Left},{windowRect.Top},{windowRect.Right},{windowRect.Top + titleBarHeight}{SpecialChars.Text} language russian");
            scripter.Run();

            int documentId = scripter.Variables.GetVariableValue <int>("result");

            FineReaderDocument document = AbbyyManager.Instance.GetDocument(documentId);

            Assert.IsNotNull(document);
            string plainText = document.GetAllText();

            Assert.IsTrue(plainText.Contains(appTitle2));
        }
        public void Execute(Arguments arguments)
        {
            var rectangle = arguments.Area.Value;

            if (!rectangle.IsValidRectangle())
            {
                throw new ArgumentException("Argument Area is not a valid rectangle");
            }
            if (arguments.Relative.Value)
            {
                var foregroundWindowRect = new RobotWin32.Rect();
                RobotWin32.GetWindowRectangle(RobotWin32.GetForegroundWindow(), ref foregroundWindowRect);
                rectangle = new Rectangle(rectangle.X + foregroundWindowRect.Left,
                                          rectangle.Y + foregroundWindowRect.Top,
                                          Math.Min(rectangle.Width, foregroundWindowRect.Right - foregroundWindowRect.Left - rectangle.X),
                                          Math.Min(rectangle.Height, foregroundWindowRect.Bottom - foregroundWindowRect.Top - rectangle.Y));
            }
            var partOfScreen = RobotWin32.GetPartOfScreen(rectangle);
            var language     = arguments.Language.Value;
            var imgToParse   = OcrOfflineHelper.RescaleImage(partOfScreen, arguments.Sensitivity.Value);
            var search       = arguments.Search.Value.ToLower().Trim();
            var dataPath     = OcrOfflineHelper.GetResourcesFolder(language);

            try
            {
                using (var tEngine = new TesseractEngine(dataPath, language, EngineMode.TesseractAndCube))
                    using (var img = PixConverter.ToPix(imgToParse))
                        using (var page = tEngine.Process(img))
                        {
                            var rectResult                 = new Rectangle(-1, -1, -1, -1);
                            var rectanglesWithWords        = GetWords(page.GetHOCRText(0), arguments.Sensitivity.Value);
                            var searchWords                = search.Split(' ');
                            List <Rectangle> rectangleList = new List <Rectangle>();
                            if (searchWords.Length > 1)
                            {
                                rectangleList = rectanglesWithWords.Where(x => searchWords.Contains(x.Value)).Select(a => a.Key).ToList();
                                if (rectangleList.Count() == searchWords.Length)
                                {
                                    rectResult = UniteRectangles(rectangleList);
                                }
                            }
                            else if (rectanglesWithWords.ContainsValue(search))
                            {
                                rectResult = rectanglesWithWords.Where(x => x.Value == search).First().Key;
                            }
                            if (Equals(rectResult, new Rectangle(-1, -1, -1, -1)) || (searchWords.Length > 1 && rectangleList.Count() != searchWords.Length))
                            {
                                throw new NullReferenceException("Ocr was unable to find text");
                            }
                            Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(rectResult));
                        }
            }
            catch (TesseractException)
            {
                throw new ApplicationException("Ocr engine exception, possibly missing language data in folder: " + dataPath);
            }
            catch (Exception e)
            {
                throw e;
            }
        }