Update ScreenMatch.cs
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
Namhyeon Go 2024-09-18 15:51:24 +09:00
parent c36d7fb71f
commit 12ce97c385

View File

@ -121,6 +121,7 @@ public class ScreenMatch
private int sampleAdjustY = 0; private int sampleAdjustY = 0;
private List<string> sampleAny; private List<string> sampleAny;
private List<string> sampleNodup; private List<string> sampleNodup;
private Size sampleNodupSize;
private Queue<Bitmap> sampleOutdated; private Queue<Bitmap> sampleOutdated;
private byte thresholdConvertToBinary = 191; private byte thresholdConvertToBinary = 191;
private string tesseractDataPath; private string tesseractDataPath;
@ -139,6 +140,11 @@ public class ScreenMatch
Width = 128, Width = 128,
Height = 128 Height = 128
}; };
sampleNodupSize = new Size
{
Width = 128,
Height = 64
};
sampleOutdated = new Queue<Bitmap>(); sampleOutdated = new Queue<Bitmap>();
// Read values from configration file // Read values from configration file
@ -425,6 +431,23 @@ public class ScreenMatch
return results; return results;
} }
public Bitmap cropBitmap(Bitmap bitmap, Point matchPosition, Size templateSize, Size sampleSize, int dx = 0, int dy = 0)
{
// Adjust coordinates to the center
int x = matchPosition.X + (templateSize.Width / 2);
int y = matchPosition.Y + (templateSize.Height / 2);
// Set range of crop image
int cropX = Math.Max((x - sampleSize.Width / 2) + dx, 0);
int cropY = Math.Max((y - sampleSize.Height / 2) + dy, 0);
int cropWidth = Math.Min(sampleSize.Width, bitmap.Width - cropX);
int cropHeight = Math.Min(sampleSize.Height, bitmap.Height - cropY);
Rectangle cropArea = new Rectangle(cropX, cropY, cropWidth, cropHeight);
// Crop image
return bitmap.Clone(cropArea, bitmap.PixelFormat);
}
public string InspectSample(Bitmap bitmap, Point matchPosition, Size templateSize, string templateName, Size sampleSize) public string InspectSample(Bitmap bitmap, Point matchPosition, Size templateSize, string templateName, Size sampleSize)
{ {
if (bitmap == null) if (bitmap == null)
@ -437,36 +460,25 @@ public class ScreenMatch
throw new ArgumentException("matchPosition cannot be empty."); throw new ArgumentException("matchPosition cannot be empty.");
} }
// initial text // initialize the text
string text = ""; string text = "";
// Adjust coordinates
int positionX = matchPosition.X + (templateSize.Width / 2);
int positionY = matchPosition.Y + (templateSize.Height / 2);
// Set range of crop image
int cropX = Math.Max((positionX - sampleSize.Width / 2) + sampleAdjustX, 0);
int cropY = Math.Max((positionY - sampleSize.Height / 2) + sampleAdjustY, 0);
int cropWidth = Math.Min(sampleSize.Width, bitmap.Width - cropX);
int cropHeight = Math.Min(sampleSize.Height, bitmap.Height - cropY);
Rectangle cropArea = new Rectangle(cropX, cropY, cropWidth, cropHeight);
// Crop image // Crop image
Bitmap croppedBitmap = bitmap.Clone(cropArea, bitmap.PixelFormat); Bitmap croppedBitmap = cropBitmap(bitmap, matchPosition, templateSize, sampleSize, sampleAdjustX, sampleAdjustY);
// Save to the outdated samples // Save to the outdated samples
if (sampleNodup.Contains(templateName)) if (sampleNodup.Contains(templateName))
{ {
int croppedBitmapHash = croppedBitmap.GetHashCode(); Bitmap croppedNodupBitmap = cropBitmap(bitmap, matchPosition, templateSize, sampleNodupSize);
bool bitmapExists = sampleOutdated.Any(x => x.GetHashCode() == croppedBitmapHash); int bitmapHash = croppedNodupBitmap.GetHashCode();
bool bitmapExists = sampleOutdated.Any(x => x.GetHashCode() == bitmapHash);
if (bitmapExists) if (bitmapExists)
{ {
throw new InvalidOperationException($"This may be a duplicate request. {templateName}"); throw new InvalidOperationException($"This may be a duplicate request. {templateName}");
} }
else else
{ {
sampleOutdated.Enqueue((Bitmap)croppedBitmap.Clone()); sampleOutdated.Enqueue((Bitmap)croppedNodupBitmap.Clone());
parent.Log($"Added to the image queue. {templateName}"); parent.Log($"Added to the image queue. {templateName}");
} }
} }