Update ScreenMatch.cs

This commit is contained in:
Namhyeon Go 2024-09-06 16:10:58 +09:00
parent da08bb7bb9
commit 28435522de

View File

@ -11,6 +11,7 @@ using System.ServiceProcess;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using System.Linq;
using Tesseract; using Tesseract;
using WelsonJS.Service; using WelsonJS.Service;
@ -112,10 +113,15 @@ public class ScreenMatch
private bool busy = false; private bool busy = false;
private List<string> _params = new List<string>(); private List<string> _params = new List<string>();
private bool isSearchFromEnd = false; private bool isSearchFromEnd = false;
private byte thresholdConvertToBinary = 191;
private bool isSaveToFile = false; private bool isSaveToFile = false;
private bool isUseSampleClipboard = false; private bool isUseSampleClipboard = false;
private bool isUseSampleOCR = false; private bool isUseSampleOCR = false;
private int sampleWidth = 128;
private int sampleHeight = 128;
private int sampleAdjustX = 0;
private int sampleAdjustY = 0;
private string sampleOnly = "";
private byte thresholdConvertToBinary = 191;
private string tesseractDataPath; private string tesseractDataPath;
private string tesseractLanguage; private string tesseractLanguage;
@ -143,36 +149,79 @@ public class ScreenMatch
if (!String.IsNullOrEmpty(screen_time_params)) if (!String.IsNullOrEmpty(screen_time_params))
{ {
string[] ss = screen_time_params.Split(','); var screen_time_configs = screen_time_params
foreach (string s in ss) { .Split(',')
AddParam(s); .Select(pair => pair.Split('='))
} .ToDictionary(
} parts => parts[0],
parts => parts.Length > 1 ? parts[1] : parts[0]
);
if (_params.Contains("backward")) foreach (var config in screen_time_configs)
{
switch (config.Key)
{
case "backward":
{ {
isSearchFromEnd = true; isSearchFromEnd = true;
this.parent.Log("Use the backward search when screen time"); this.parent.Log("Use the backward search when screen time");
break;
} }
if (_params.Contains("save")) case "save":
{ {
isSaveToFile = true; isSaveToFile = true;
this.parent.Log("Will be save an image file when capture the screens"); this.parent.Log("Will be save an image file when capture the screens");
break;
} }
if (_params.Contains("sample_clipboard")) case "sample_clipboard":
{ {
isUseSampleClipboard = true; isUseSampleClipboard = true;
this.parent.Log("Use Clipboard within a 128x128 pixel range around specific coordinates."); this.parent.Log("Use Clipboard within a 128x128 pixel range around specific coordinates.");
break;
} }
if (_params.Contains("sample_ocr")) case "sample_ocr":
{ {
tesseractDataPath = Path.Combine(workingDirectory, "app/assets/tessdata_best"); tesseractDataPath = Path.Combine(workingDirectory, "app/assets/tessdata_best");
tesseractLanguage = "eng"; tesseractLanguage = "eng";
isUseSampleOCR = true; isUseSampleOCR = true;
this.parent.Log("Use OCR within a 128x128 pixel range around specific coordinates."); this.parent.Log("Use OCR within a 128x128 pixel range around specific coordinates.");
break;
}
case "sample_width":
{
int.TryParse(config.Value, out sampleWidth);
break;
}
case "sample_height":
{
int.TryParse(config.Value, out sampleHeight);
break;
}
case "sample_adjust_x":
{
int.TryParse(config.Value, out sampleAdjustX);
break;
}
case "sample_adjust_y":
{
int.TryParse(config.Value, out sampleAdjustY);
break;
}
case "sample_only":
{
sampleOnly = config.Value;
break;
}
}
}
} }
SetMode(screen_time_mode); SetMode(screen_time_mode);
@ -191,11 +240,6 @@ public class ScreenMatch
} }
} }
public void AddParam(string _param)
{
_params.Add(_param);
}
public void SetThreshold(double threshold) public void SetThreshold(double threshold)
{ {
this.threshold = threshold; this.threshold = threshold;
@ -304,6 +348,13 @@ public class ScreenMatch
} }
Point matchPosition = FindTemplate(_mainImage, (Bitmap)image.Clone(), out double maxCorrelation); Point matchPosition = FindTemplate(_mainImage, (Bitmap)image.Clone(), out double maxCorrelation);
string text = "";
if (String.IsNullOrEmpty(sampleOnly) || (!String.IsNullOrEmpty(sampleOnly) && sampleOnly == filename))
{
text = InspectSample((Bitmap)mainImage.Clone(), matchPosition.X, matchPosition.Y, imageWidth, imageHeight, sampleWidth, sampleHeight);
}
if (matchPosition != Point.Empty) if (matchPosition != Point.Empty)
{ {
results.Add(new ScreenMatchResult results.Add(new ScreenMatchResult
@ -312,7 +363,7 @@ public class ScreenMatch
ScreenNumber = i, ScreenNumber = i,
Position = matchPosition, Position = matchPosition,
MaxCorrelation = maxCorrelation, MaxCorrelation = maxCorrelation,
Text = InspectSample((Bitmap)mainImage.Clone(), matchPosition.X, matchPosition.Y, imageWidth, imageHeight, 128, 128) Text = text
}); });
} }
} }
@ -346,8 +397,8 @@ public class ScreenMatch
y = y + (b / 2); y = y + (b / 2);
// Set range of crop image // Set range of crop image
int cropX = Math.Max(x - w / 2, 0); int cropX = Math.Max(x - w / 2, 0) + sampleAdjustX;
int cropY = Math.Max(y - h / 2, 0); int cropY = Math.Max(y - h / 2, 0) + sampleAdjustY;
int cropWidth = Math.Min(w, bitmap.Width - cropX); int cropWidth = Math.Min(w, bitmap.Width - cropX);
int cropHeight = Math.Min(h, bitmap.Height - cropY); int cropHeight = Math.Min(h, bitmap.Height - cropY);
Rectangle cropArea = new Rectangle(cropX, cropY, cropWidth, cropHeight); Rectangle cropArea = new Rectangle(cropX, cropY, cropWidth, cropHeight);