From 2f4b95313fc8b6b3f5697e6090a55ebea7f5efc6 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 2 Sep 2024 15:30:20 +0900 Subject: [PATCH] Update ScreenMatch.cs --- .../WelsonJS.Service/ScreenMatch.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs b/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs index 5bf5531..4a49463 100644 --- a/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs +++ b/WelsonJS.Toolkit/WelsonJS.Service/ScreenMatch.cs @@ -283,6 +283,8 @@ public class ScreenMatch parent.Log($"Trying match the template {image.Tag as string} on the screen {i}..."); string filename = image.Tag as string; + int imageWidth = image.Width; + int imageHeight = image.Height; Bitmap _mainImage; if (filename.StartsWith("binary_")) @@ -300,7 +302,7 @@ public class ScreenMatch string text; if (isOCR128) { - text = OCR((Bitmap)mainImage.Clone(), matchPosition.X, matchPosition.Y, 128, 128); + text = OCR((Bitmap)mainImage.Clone(), matchPosition.X, matchPosition.Y, imageWidth, imageHeight, 128, 128); } else { @@ -332,18 +334,30 @@ public class ScreenMatch return results; } - public string OCR(Bitmap bitmap, int x, int y, int w, int h) + public string OCR(Bitmap bitmap, int x, int y, int a, int b, int w, int h) { + if (bitmap == null) + { + throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null."); + } + string text = ""; + // Adjust coordinates + x = x + (a / 2); + y = y + (b / 2); + + // Set range of crop image int cropX = Math.Max(x - w / 2, 0); int cropY = Math.Max(y - h / 2, 0); int cropWidth = Math.Min(w, bitmap.Width - cropX); int cropHeight = Math.Min(h, bitmap.Height - cropY); - Rectangle cropArea = new Rectangle(cropX, cropY, cropWidth, cropHeight); + + // Crop image Bitmap croppedBitmap = bitmap.Clone(cropArea, bitmap.PixelFormat); + // OCR using (var engine = new TesseractEngine(tesseractDataPath, tesseractLanguage, EngineMode.Default)) { using (var img = PixConverter.ToPix(croppedBitmap))