public void Execute(Arguments arguments) { manager = AbbyyManager.Instance; System.Drawing.Rectangle rectangle = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates(); System.Drawing.Bitmap partOfScreen = RobotWin32.GetPartOfScreen(rectangle); IEngine engine = manager.Engine; DocumentProcessingParams processingParams = engine.CreateDocumentProcessingParams(); RecognizerParams recognizingParams = processingParams.PageProcessingParams.RecognizerParams; recognizingParams.SetPredefinedTextLanguage(arguments.Language.Value); engine.LoadPredefinedProfile(AbbyyManager.TextAccuracyProfile); FRDocument imageDocument = engine.CreateFRDocument(); using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) { partOfScreen.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Position = 0; IReadStream imageStream = new StreamNet2AbbyyAdapter(stream); imageDocument.AddImageFileFromStream(imageStream); } imageDocument.Process(processingParams); Scripter.Variables.SetVariableValue(arguments.Result.Value, new TextStructure(imageDocument.PlainText.Text)); }
public void Execute(Arguments arguments) { if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1) { throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1."); } using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image1.Value, nameof(arguments.Image1))) using (Bitmap bitmap2 = (string.IsNullOrEmpty(arguments.Image2?.Value)) ? RobotWin32.GetPartOfScreen( Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value), bitmap1.PixelFormat) : Imaging.OpenImageFile(arguments.Image2.Value, nameof(arguments.Image2))) { Rectangle foundRectangle = Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value); if (foundRectangle == Rectangle.Empty) { throw new ArgumentException("Image was not found in specified search area."); } else { Point foundPoint = (!arguments.CenterResult.Value) ? new Point(foundRectangle.X, foundRectangle.Y) : new Point(foundRectangle.X + foundRectangle.Width / 2, foundRectangle.Y + foundRectangle.Height / 2); foundPoint = new Point(foundPoint.X + arguments.OffsetX.Value, foundPoint.Y + arguments.OffsetY.Value); Scripter.Variables.SetVariableValue(arguments.Result.Value, new PointStructure(foundPoint)); } } }
public void Execute(Arguments arguments) { var screenArea = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates(); var screenImage = RobotWin32.GetPartOfScreen(screenArea); var timeout = (int)arguments.Timeout.Value.TotalMilliseconds; var languages = arguments.Languages.Value.Split(',').ToList(); var googleApi = new GoogleCloudApi(); var text = googleApi.RecognizeText(screenImage, languages, timeout); if (string.IsNullOrEmpty(text)) { throw new NullReferenceException("Ocr was unable to find text"); } Scripter.Variables.SetVariableValue(arguments.Result.Value, new TextStructure(text)); }
public void Execute(Arguments arguments) { Rectangle rectangle = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates(); Bitmap partOfScreen = RobotWin32.GetPartOfScreen(rectangle); int timeout = (int)arguments.Timeout.Value.TotalMilliseconds; List <string> languages = arguments.Languages.Value.Split(',').ToList(); string search = arguments.Search.Value; GoogleCloudApi googleApi = new GoogleCloudApi(); Rectangle output = googleApi.RecognizeText(partOfScreen, search, languages, timeout); if (Equals(output, new Rectangle(-1, -1, -1, -1))) { throw new NullReferenceException("Ocr was unable to find text"); } Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(output)); }
public void Execute(Arguments arguments) { var screenArea = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates(); var screenImage = RobotWin32.GetPartOfScreen(screenArea); var timeout = (int)arguments.Timeout.Value.TotalMilliseconds; var languages = arguments.Languages.Value.Split(',').ToList(); var search = arguments.Search.Value; var googleApi = new GoogleCloudApi(); var rectangle = googleApi.FindTextPosition(screenImage, search, languages, timeout); if (rectangle == null) { throw new NullReferenceException("Ocr was unable to find text position"); } Scripter.Variables.SetVariableValue(arguments.Result.Value, new RectangleStructure(rectangle.Value)); }
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; } }
public void Execute(Arguments arguments) { if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1) { throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1."); } using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image1.Value, nameof(arguments.Image1))) using (Bitmap bitmap2 = (string.IsNullOrEmpty(arguments.Image2?.Value)) ? RobotWin32.GetPartOfScreen( Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value), bitmap1.PixelFormat) : Imaging.OpenImageFile(arguments.Image2.Value, nameof(arguments.Image2))) { bool found = Rectangle.Empty != Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value); Scripter.Variables.SetVariableValue(arguments.Result.Value, new BooleanStructure(found)); } }
public void Execute(Arguments arguments) { if (arguments.Threshold.Value < 0 || arguments.Threshold.Value > 1) { throw new ArgumentOutOfRangeException("Threshold must be a value from 0 to 1."); } using (Bitmap bitmap1 = Imaging.OpenImageFile(arguments.Image.Value, nameof(arguments.Image))) { int timeout = (int)arguments.Timeout.Value.TotalMilliseconds; long start = Environment.TickCount; Rectangle foundRectangle = Rectangle.Empty; while (Math.Abs(Environment.TickCount - start) < timeout && Scripter.Stopped == false && foundRectangle == Rectangle.Empty) { using ( Bitmap bitmap2 = RobotWin32.GetPartOfScreen( Imaging.ParseRectanglePositionFromArguments(arguments.ScreenSearchArea.Value, arguments.Relative.Value), bitmap1.PixelFormat)) { foundRectangle = Imaging.IsImageInImage(bitmap1, bitmap2, (double)arguments.Threshold.Value); Application.DoEvents(); } } if (foundRectangle == Rectangle.Empty) { throw new TimeoutException("Image was not found in specified search area."); } else { Point foundPoint = (!arguments.CenterResult.Value) ? new Point(foundRectangle.X, foundRectangle.Y) : new Point(foundRectangle.X + foundRectangle.Width / 2, foundRectangle.Y + foundRectangle.Height / 2); foundPoint = new Point(foundPoint.X + arguments.OffsetX.Value, foundPoint.Y + arguments.OffsetY.Value); Scripter.Variables.SetVariableValue(arguments.Result.Value, new PointStructure(foundPoint)); } } }
public static Bitmap GetScreenshot(this RectangleStructure screenArea, BooleanStructure relative) { if (screenArea.Value.Width < 1 || screenArea.Value.Height < 1) { throw new ArgumentException("ScreenSearchArea argument's parts can't be negative. Both width and height must be bigger than zero."); } try { var screenSearchArea = screenArea.Value; if (relative.Value) { screenSearchArea = screenArea.Value.GetRelativeArea(); } return(RobotWin32.GetPartOfScreen(screenSearchArea, PixelFormat.Format24bppRgb)); } catch (Exception ex) { throw new Exception($"Could not get the screenshot image. Message: {ex.Message}", ex); } }
public void Execute(Arguments arguments) { if ((arguments.Language?.Value == null) && (arguments.Dictionary?.Value == null)) { arguments.Language = new TextStructure("English"); } Rectangle rectangle = !arguments.Relative.Value ? arguments.Area.Value : arguments.Area.Value.ToAbsoluteCoordinates(); Bitmap partOfScreen = RobotWin32.GetPartOfScreen(rectangle); AbbyyManager manager = AbbyyManager.Instance; FineReaderDocument imageDocument = null; using (MemoryStream stream = new MemoryStream()) { partOfScreen.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); stream.Position = 0; imageDocument = manager.CreateDocument(stream); } manager.ProcessDocument(imageDocument, null, arguments.Language?.Value, arguments.LanguageWeight.Value, arguments.DictionaryWeight.Value, ListConverter.ExtractDictionary(arguments.Dictionary?.Value)); Scripter.Variables.SetVariableValue(arguments.Result.Value, new IntegerStructure(imageDocument.ID)); }
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; } }