/// <summary> /// Crop the image and return the cropped image /// </summary> /// <param name="image">Image that need to be cropped</param> /// <param name="Start">Starting Point</param> /// <param name="End">Ending Point</param> /// <param name="caller"></param> /// <param name="lineNumber"></param> /// <returns></returns> public static ScreenshotData CropImage(ScreenshotData image, Point Start, Point End, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { if (!ScriptRun.Run) { return(null); } return(Compress(CropImage(Decompress(image), Start, End, lineNumber, caller))); }
/// <summary> /// OCR the image. Need Prepair OCR first! /// </summary> /// <param name="source"></param> /// <param name="lang"></param> /// <returns></returns> public static string OcrImage(ScreenshotData source, string lang) { if (Instance.t == null) { throw new Exception("Run PrepairOcr First!"); } int error = 0; while (source == null && error < 5) { if (Variables.ProchWnd != IntPtr.Zero) { source = Screenshot.ImageCapture(Variables.ProchWnd, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd); } else { source = Screenshot.ImageCapture(Variables.Proc.MainWindowHandle, Variables.WinApiCaptCropStart, Variables.WinApiCaptCropEnd); } error++; } if (source == null) { Variables.AdvanceLog("Source image is null and unable to recapture!"); return(string.Empty); } Image <Bgr, byte> img = new Image <Bgr, byte>(Screenshot.Decompress(source)); if (File.Exists($"C:\\ProgramData\\tessdata\\{lang}.traineddata")) { FileInfo f = new FileInfo(($"C:\\ProgramData\\tessdata\\{lang}.traineddata")); if (f.Length == 0) { throw new FileNotFoundException("The given ocr data is incomplete or not found!"); } } Instance.t.SetImage(img); var result = Instance.t.GetUTF8Text(); if (Instance.NumOnly) { result = new string(result.Where(Char.IsDigit).ToArray()); } return(result.Trim()); }
/// <summary> /// Decompress the byte array back to image for other usage /// </summary> /// <param name="buffer">the byte array of image compressed by Compress(Image image)</param> /// <returns>Image</returns> public static Bitmap Decompress(ScreenshotData buffer) { lock (Instance.locker) { try { if (buffer == null) { //Unable to get back image, lets return black Bitmap bmp = new Bitmap(Variables.EmulatorWidth, Variables.EmulatorHeight); using (Graphics graph = Graphics.FromImage(bmp)) { Rectangle ImageSize = new Rectangle(0, 0, Variables.EmulatorWidth, Variables.EmulatorHeight); graph.FillRectangle(Brushes.Black, ImageSize); } return(bmp); } else { using (MemoryStream stream = new MemoryStream(buffer.imageData)) { return(new Bitmap(stream)); } } } catch (Exception ex) { Variables.AdvanceLog(ex.ToString()); Bitmap bmp = new Bitmap(Variables.EmulatorWidth, Variables.EmulatorHeight); using (Graphics graph = Graphics.FromImage(bmp)) { Rectangle ImageSize = new Rectangle(0, 0, Variables.EmulatorWidth, Variables.EmulatorHeight); graph.FillRectangle(Brushes.Black, ImageSize); } return(bmp); } } }