2024-08-12 03:47:19 +00:00
|
|
|
|
// ScreenMatching.cs
|
|
|
|
|
// https://github.com/gnh1201/welsonjs
|
2024-10-19 14:28:16 +00:00
|
|
|
|
// https://catswords-oss.rdbl.io/5719744820/8803957194
|
2024-08-12 03:47:19 +00:00
|
|
|
|
using System;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using System.Collections.Generic;
|
2024-08-24 13:24:26 +00:00
|
|
|
|
using System.Diagnostics;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2024-09-18 18:29:43 +00:00
|
|
|
|
using System.IO.Hashing;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2024-08-02 07:51:49 +00:00
|
|
|
|
using System.ServiceProcess;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using System.Text;
|
2024-09-04 07:18:05 +00:00
|
|
|
|
using System.Threading;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using System.Windows.Forms;
|
2024-09-06 07:10:58 +00:00
|
|
|
|
using System.Linq;
|
2024-09-02 05:17:06 +00:00
|
|
|
|
using Tesseract;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
using WelsonJS.Service;
|
|
|
|
|
|
2024-08-24 12:32:44 +00:00
|
|
|
|
public class ScreenMatch
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
|
|
|
|
// User32.dll API 함수 선언
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern bool IsWindowVisible(IntPtr hWnd);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern int GetWindowRect(IntPtr hWnd, out RECT lpRect);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern IntPtr GetDC(IntPtr hWnd);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
2024-08-13 02:21:48 +00:00
|
|
|
|
private static extern int GetWindowTextLength(IntPtr hWnd);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
[DllImport("gdi32.dll")]
|
|
|
|
|
private static extern bool BitBlt(IntPtr hDestDC, int x, int y, int nWidth, int nHeight, IntPtr hSrcDC, int xSrc, int ySrc, int dwRop);
|
2024-08-13 02:21:48 +00:00
|
|
|
|
|
2024-08-24 13:24:26 +00:00
|
|
|
|
[DllImport("user32.dll", SetLastError = true)]
|
2024-08-25 13:43:34 +00:00
|
|
|
|
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
|
|
|
|
|
|
|
|
|
[DllImport("user32.dll")]
|
|
|
|
|
private static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/60872044/how-to-get-scaling-factor-for-each-monitor-e-g-1-1-25-1-5
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
private struct DEVMODE
|
|
|
|
|
{
|
|
|
|
|
private const int CCHDEVICENAME = 0x20;
|
|
|
|
|
private const int CCHFORMNAME = 0x20;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
|
|
|
|
|
public string dmDeviceName;
|
|
|
|
|
public short dmSpecVersion;
|
|
|
|
|
public short dmDriverVersion;
|
|
|
|
|
public short dmSize;
|
|
|
|
|
public short dmDriverExtra;
|
|
|
|
|
public int dmFields;
|
|
|
|
|
public int dmPositionX;
|
|
|
|
|
public int dmPositionY;
|
|
|
|
|
public ScreenOrientation dmDisplayOrientation;
|
|
|
|
|
public int dmDisplayFixedOutput;
|
|
|
|
|
public short dmColor;
|
|
|
|
|
public short dmDuplex;
|
|
|
|
|
public short dmYResolution;
|
|
|
|
|
public short dmTTOption;
|
|
|
|
|
public short dmCollate;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
|
|
|
|
|
public string dmFormName;
|
|
|
|
|
public short dmLogPixels;
|
|
|
|
|
public int dmBitsPerPel;
|
|
|
|
|
public int dmPelsWidth;
|
|
|
|
|
public int dmPelsHeight;
|
|
|
|
|
public int dmDisplayFlags;
|
|
|
|
|
public int dmDisplayFrequency;
|
|
|
|
|
public int dmICMMethod;
|
|
|
|
|
public int dmICMIntent;
|
|
|
|
|
public int dmMediaType;
|
|
|
|
|
public int dmDitherType;
|
|
|
|
|
public int dmReserved1;
|
|
|
|
|
public int dmReserved2;
|
|
|
|
|
public int dmPanningWidth;
|
|
|
|
|
public int dmPanningHeight;
|
|
|
|
|
}
|
2024-08-24 13:24:26 +00:00
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
private const int SRCCOPY = 0x00CC0020;
|
|
|
|
|
|
|
|
|
|
// 델리게이트 선언
|
|
|
|
|
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
|
|
|
|
|
|
|
|
|
// RECT 구조체 선언
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct RECT
|
|
|
|
|
{
|
|
|
|
|
public int Left;
|
|
|
|
|
public int Top;
|
|
|
|
|
public int Right;
|
|
|
|
|
public int Bottom;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 07:53:31 +00:00
|
|
|
|
private ServiceMain parent;
|
2024-08-12 03:47:19 +00:00
|
|
|
|
private List<Bitmap> templateImages;
|
2024-08-12 12:05:17 +00:00
|
|
|
|
private string templateDirectoryPath;
|
2024-08-25 13:43:34 +00:00
|
|
|
|
private string outputDirectoryPath;
|
2024-08-12 12:05:17 +00:00
|
|
|
|
private int templateCurrentIndex = 0;
|
2024-10-20 19:16:46 +00:00
|
|
|
|
private double threshold = 0.3;
|
2024-08-24 05:57:01 +00:00
|
|
|
|
private string mode;
|
2024-10-08 05:59:04 +00:00
|
|
|
|
private bool busy;
|
2024-08-24 07:16:07 +00:00
|
|
|
|
private List<string> _params = new List<string>();
|
2024-08-25 16:23:02 +00:00
|
|
|
|
private bool isSearchFromEnd = false;
|
|
|
|
|
private bool isSaveToFile = false;
|
2024-09-18 05:33:15 +00:00
|
|
|
|
private Size sampleSize;
|
2024-09-18 18:29:43 +00:00
|
|
|
|
private int sampleAdjustX;
|
|
|
|
|
private int sampleAdjustY;
|
2024-09-18 05:33:15 +00:00
|
|
|
|
private List<string> sampleAny;
|
2024-09-18 18:29:43 +00:00
|
|
|
|
private List<string> sampleClipboard;
|
|
|
|
|
private List<string> sampleOcr;
|
2024-09-18 05:33:15 +00:00
|
|
|
|
private List<string> sampleNodup;
|
2024-09-18 06:51:24 +00:00
|
|
|
|
private Size sampleNodupSize;
|
2024-10-06 18:39:28 +00:00
|
|
|
|
private Queue<Bitmap> outdatedSamples;
|
2024-09-02 05:37:45 +00:00
|
|
|
|
private string tesseractDataPath;
|
|
|
|
|
private string tesseractLanguage;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
private void SetBusy(bool busy)
|
|
|
|
|
{
|
|
|
|
|
this.busy = busy;
|
|
|
|
|
parent.Log($"State changed: busy={busy}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 18:39:28 +00:00
|
|
|
|
public class TemplateInfo
|
|
|
|
|
{
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public int Index { get; set; }
|
|
|
|
|
|
|
|
|
|
public TemplateInfo(string fileName, int index)
|
|
|
|
|
{
|
|
|
|
|
FileName = fileName;
|
|
|
|
|
Index = index;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SampleInfo
|
|
|
|
|
{
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
public uint Crc32 { get; set; }
|
|
|
|
|
|
|
|
|
|
public SampleInfo(string fileName, uint crc32)
|
|
|
|
|
{
|
|
|
|
|
FileName = fileName;
|
|
|
|
|
Crc32 = crc32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 12:32:44 +00:00
|
|
|
|
public ScreenMatch(ServiceBase parent, string workingDirectory)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-12 03:47:19 +00:00
|
|
|
|
this.parent = (ServiceMain)parent;
|
2024-09-18 05:33:15 +00:00
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
SetBusy(false);
|
|
|
|
|
|
2024-08-12 12:05:17 +00:00
|
|
|
|
templateDirectoryPath = Path.Combine(workingDirectory, "app/assets/img/_templates");
|
2024-08-25 13:43:34 +00:00
|
|
|
|
outputDirectoryPath = Path.Combine(workingDirectory, "app/assets/img/_captured");
|
2024-08-12 04:00:04 +00:00
|
|
|
|
templateImages = new List<Bitmap>();
|
2024-09-18 18:29:43 +00:00
|
|
|
|
|
|
|
|
|
// Initialize variables for sampling process
|
2024-09-18 05:33:15 +00:00
|
|
|
|
sampleSize = new Size
|
|
|
|
|
{
|
|
|
|
|
Width = 128,
|
|
|
|
|
Height = 128
|
|
|
|
|
};
|
2024-09-18 18:29:43 +00:00
|
|
|
|
sampleAdjustX = 0;
|
|
|
|
|
sampleAdjustY = 0;
|
|
|
|
|
sampleAny = new List<string>();
|
|
|
|
|
sampleClipboard = new List<string>();
|
|
|
|
|
sampleOcr = new List<string>();
|
|
|
|
|
sampleNodup = new List<string>();
|
2024-09-18 06:51:24 +00:00
|
|
|
|
sampleNodupSize = new Size
|
|
|
|
|
{
|
2024-10-20 19:16:46 +00:00
|
|
|
|
Width = 180,
|
|
|
|
|
Height = 60
|
2024-09-18 06:51:24 +00:00
|
|
|
|
};
|
2024-10-06 18:39:28 +00:00
|
|
|
|
outdatedSamples = new Queue<Bitmap>();
|
2024-08-02 07:51:49 +00:00
|
|
|
|
|
2024-08-24 05:57:01 +00:00
|
|
|
|
// Read values from configration file
|
2024-08-25 03:56:01 +00:00
|
|
|
|
string screen_time_mode;
|
|
|
|
|
string screen_time_params;
|
2024-08-24 05:57:01 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-03 13:27:26 +00:00
|
|
|
|
screen_time_mode = this.parent.GetSettingsHandler().Read("SCREEN_TIME_MODE", "Service");
|
|
|
|
|
screen_time_params = this.parent.GetSettingsHandler().Read("SCREEN_TIME_PARAMS", "Service");
|
2024-08-24 05:57:01 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-08-25 03:56:01 +00:00
|
|
|
|
screen_time_mode = null;
|
|
|
|
|
screen_time_params = null;
|
2024-08-24 05:57:01 +00:00
|
|
|
|
this.parent.Log($"Failed to read from configration file: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 03:56:01 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(screen_time_params))
|
2024-08-24 07:13:06 +00:00
|
|
|
|
{
|
2024-09-06 07:10:58 +00:00
|
|
|
|
var screen_time_configs = screen_time_params
|
|
|
|
|
.Split(',')
|
|
|
|
|
.Select(pair => pair.Split('='))
|
|
|
|
|
.ToDictionary(
|
|
|
|
|
parts => parts[0],
|
|
|
|
|
parts => parts.Length > 1 ? parts[1] : parts[0]
|
|
|
|
|
);
|
|
|
|
|
|
2024-09-06 08:03:11 +00:00
|
|
|
|
var config_keys = new string[]
|
2024-09-06 07:10:58 +00:00
|
|
|
|
{
|
2024-09-06 08:03:11 +00:00
|
|
|
|
"process_name",
|
|
|
|
|
"sample_width",
|
|
|
|
|
"sample_height",
|
|
|
|
|
"sample_adjust_x",
|
|
|
|
|
"sample_adjust_y",
|
2024-09-18 05:33:15 +00:00
|
|
|
|
"sample_any",
|
|
|
|
|
"sample_nodup",
|
2024-09-06 08:03:11 +00:00
|
|
|
|
"backward",
|
|
|
|
|
"save",
|
|
|
|
|
"sample_clipboard",
|
|
|
|
|
"sample_ocr"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (var config_key in config_keys)
|
|
|
|
|
{
|
|
|
|
|
string config_value;
|
|
|
|
|
screen_time_configs.TryGetValue(config_key, out config_value);
|
|
|
|
|
|
|
|
|
|
if (config_value != null)
|
2024-09-06 07:10:58 +00:00
|
|
|
|
{
|
2024-09-06 08:03:11 +00:00
|
|
|
|
switch (config_key)
|
|
|
|
|
{
|
|
|
|
|
case "backward":
|
|
|
|
|
{
|
|
|
|
|
isSearchFromEnd = true;
|
|
|
|
|
this.parent.Log("Use the backward search when screen time");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "save":
|
|
|
|
|
{
|
|
|
|
|
isSaveToFile = true;
|
|
|
|
|
this.parent.Log("Will be save an image file when capture the screens");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 19:16:46 +00:00
|
|
|
|
case "threshold":
|
|
|
|
|
{
|
|
|
|
|
double.TryParse(config_value, out double t);
|
|
|
|
|
threshold = t;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 08:03:11 +00:00
|
|
|
|
case "sample_clipboard":
|
|
|
|
|
{
|
2024-09-18 18:29:43 +00:00
|
|
|
|
sampleClipboard = new List<string>(config_value.Split(':'));
|
2024-09-06 08:03:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_ocr":
|
|
|
|
|
{
|
|
|
|
|
tesseractDataPath = Path.Combine(workingDirectory, "app/assets/tessdata_best");
|
|
|
|
|
tesseractLanguage = "eng";
|
2024-09-18 18:29:43 +00:00
|
|
|
|
sampleOcr = new List<string>(config_value.Split(':'));
|
2024-09-06 08:03:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_width":
|
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
int.TryParse(config_value, out int w);
|
|
|
|
|
sampleSize.Width = w;
|
2024-09-06 08:03:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_height":
|
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
int.TryParse(config_value, out int h);
|
|
|
|
|
sampleSize.Height = h;
|
2024-09-06 08:03:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-20 19:16:46 +00:00
|
|
|
|
case "sample_nodup_width":
|
|
|
|
|
{
|
|
|
|
|
int.TryParse(config_value, out int w);
|
|
|
|
|
sampleNodupSize.Width = w;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_nodup_height":
|
|
|
|
|
{
|
|
|
|
|
int.TryParse(config_value, out int h);
|
|
|
|
|
sampleNodupSize.Height = h;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-06 08:03:11 +00:00
|
|
|
|
case "sample_adjust_x":
|
|
|
|
|
{
|
|
|
|
|
int.TryParse(config_value, out sampleAdjustX);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_adjust_y":
|
|
|
|
|
{
|
|
|
|
|
int.TryParse(config_value, out sampleAdjustY);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
case "sample_any":
|
|
|
|
|
{
|
|
|
|
|
sampleAny = new List<string>(config_value.Split(':'));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "sample_nodup":
|
2024-09-06 08:03:11 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
sampleNodup = new List<string>(config_value.Split(':'));
|
2024-09-06 08:03:11 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-06 07:10:58 +00:00
|
|
|
|
}
|
2024-08-24 07:13:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-02 05:17:06 +00:00
|
|
|
|
|
2024-08-25 16:23:02 +00:00
|
|
|
|
SetMode(screen_time_mode);
|
|
|
|
|
LoadTemplateImages();
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 04:44:48 +00:00
|
|
|
|
public void SetMode(string mode)
|
2024-08-12 07:53:31 +00:00
|
|
|
|
{
|
2024-08-24 05:57:01 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(mode))
|
|
|
|
|
{
|
|
|
|
|
this.mode = mode;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.mode = "screen";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 04:44:48 +00:00
|
|
|
|
public void SetThreshold(double threshold)
|
|
|
|
|
{
|
|
|
|
|
this.threshold = threshold;
|
2024-08-12 07:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
public void LoadTemplateImages()
|
|
|
|
|
{
|
2024-08-24 06:40:15 +00:00
|
|
|
|
string[] files;
|
2024-08-12 04:00:04 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-12 12:05:17 +00:00
|
|
|
|
files = Directory.GetFiles(templateDirectoryPath, "*.png");
|
2024-08-12 04:00:04 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-08-24 06:40:15 +00:00
|
|
|
|
files = new string[]{};
|
2024-08-24 04:44:48 +00:00
|
|
|
|
parent.Log($"Failed to read the directory structure: {ex.Message}");
|
2024-08-12 04:00:04 +00:00
|
|
|
|
}
|
2024-07-29 12:43:14 +00:00
|
|
|
|
|
|
|
|
|
foreach (var file in files)
|
|
|
|
|
{
|
2024-08-26 06:24:38 +00:00
|
|
|
|
string filename = Path.GetFileName(file);
|
2024-10-03 13:27:26 +00:00
|
|
|
|
|
|
|
|
|
string realpath;
|
|
|
|
|
string altpath = parent.GetUserVariablesHandler().GetValue(filename);
|
|
|
|
|
if (!String.IsNullOrEmpty(altpath))
|
|
|
|
|
{
|
|
|
|
|
realpath = altpath;
|
|
|
|
|
parent.Log($"Use the alternative image: {realpath}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
realpath = file;
|
|
|
|
|
parent.Log($"Use the default image: {realpath}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bitmap bitmap = new Bitmap(realpath)
|
2024-08-24 04:44:48 +00:00
|
|
|
|
{
|
2024-08-26 06:24:38 +00:00
|
|
|
|
Tag = filename
|
2024-08-24 04:44:48 +00:00
|
|
|
|
};
|
2024-08-26 06:24:38 +00:00
|
|
|
|
|
2024-09-20 07:53:50 +00:00
|
|
|
|
if (!filename.StartsWith("no_"))
|
2024-08-26 06:24:38 +00:00
|
|
|
|
{
|
2024-09-20 07:53:50 +00:00
|
|
|
|
if (filename.StartsWith("binary_"))
|
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
templateImages.Add(ImageQuantize(bitmap));
|
2024-09-20 07:53:50 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
templateImages.Add(bitmap);
|
|
|
|
|
}
|
2024-08-26 06:24:38 +00:00
|
|
|
|
}
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 07:53:31 +00:00
|
|
|
|
// 캡쳐 및 템플릿 매칭 진행
|
|
|
|
|
public List<ScreenMatchResult> CaptureAndMatch()
|
|
|
|
|
{
|
2024-08-26 06:24:38 +00:00
|
|
|
|
List<ScreenMatchResult> results = new List<ScreenMatchResult>();
|
|
|
|
|
|
2024-09-04 05:12:28 +00:00
|
|
|
|
if (busy)
|
2024-08-26 06:24:38 +00:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Waiting done a previous job...");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-29 06:34:54 +00:00
|
|
|
|
if (templateImages.Count > 0)
|
2024-08-12 07:53:31 +00:00
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
SetBusy(true);
|
2024-08-29 06:34:54 +00:00
|
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
|
{
|
|
|
|
|
case "screen": // 화면 기준
|
|
|
|
|
results = CaptureAndMatchAllScreens();
|
2024-10-08 05:59:04 +00:00
|
|
|
|
SetBusy(false);
|
2024-08-29 06:34:54 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case "window": // 윈도우 핸들 기준
|
|
|
|
|
results = CaptureAndMatchAllWindows();
|
2024-10-08 05:59:04 +00:00
|
|
|
|
SetBusy(false);
|
2024-08-29 06:34:54 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2024-10-08 05:59:04 +00:00
|
|
|
|
SetBusy(false);
|
2024-08-29 06:34:54 +00:00
|
|
|
|
throw new Exception($"Unknown capture mode {mode}");
|
|
|
|
|
}
|
2024-08-12 07:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 06:24:38 +00:00
|
|
|
|
return results;
|
2024-08-12 07:53:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
// 화면을 기준으로 찾기
|
|
|
|
|
public List<ScreenMatchResult> CaptureAndMatchAllScreens()
|
|
|
|
|
{
|
|
|
|
|
var results = new List<ScreenMatchResult>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < Screen.AllScreens.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
Screen screen = Screen.AllScreens[i];
|
|
|
|
|
Bitmap mainImage = CaptureScreen(screen);
|
|
|
|
|
|
2024-08-12 12:05:17 +00:00
|
|
|
|
Bitmap image = templateImages[templateCurrentIndex];
|
2024-09-18 05:33:15 +00:00
|
|
|
|
string templateName = image.Tag as string;
|
2024-10-06 18:39:28 +00:00
|
|
|
|
TemplateInfo nextTemplateInfo = parent.GetNextTemplateInfo();
|
2024-09-20 07:53:50 +00:00
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
Size templateSize = new Size
|
|
|
|
|
{
|
|
|
|
|
Width = image.Width,
|
|
|
|
|
Height = image.Height
|
|
|
|
|
};
|
2024-09-02 05:17:06 +00:00
|
|
|
|
|
2024-09-18 06:01:05 +00:00
|
|
|
|
parent.Log($"Trying match the template {templateName} on the screen {i}...");
|
|
|
|
|
|
2024-10-06 18:39:28 +00:00
|
|
|
|
if (!String.IsNullOrEmpty(nextTemplateInfo.FileName) && templateName != nextTemplateInfo.FileName)
|
2024-09-20 07:53:50 +00:00
|
|
|
|
{
|
|
|
|
|
parent.Log($"Ignored the template {templateName}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
Bitmap out_mainImage;
|
|
|
|
|
string out_filename;
|
2024-09-18 05:33:15 +00:00
|
|
|
|
if (templateName.StartsWith("binary_"))
|
2024-08-26 06:24:38 +00:00
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
out_mainImage = ImageQuantize((Bitmap)mainImage.Clone());
|
|
|
|
|
out_filename = $"{DateTime.Now:yyyy-MM-dd hh mm ss} binary.png";
|
2024-09-02 05:17:06 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
out_mainImage = mainImage;
|
|
|
|
|
out_filename = $"{DateTime.Now:yyyy-MM-dd hh mm ss}.png";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isSaveToFile)
|
|
|
|
|
{
|
|
|
|
|
string out_filepath = Path.Combine(outputDirectoryPath, out_filename);
|
|
|
|
|
((Bitmap)out_mainImage.Clone()).Save(out_filepath);
|
|
|
|
|
parent.Log($"Screenshot saved: {out_filepath}");
|
2024-08-26 06:24:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 18:39:28 +00:00
|
|
|
|
// List to store the positions of matched templates in the main image
|
|
|
|
|
List<Point> matchPositions;
|
|
|
|
|
|
|
|
|
|
// If the index value is negative, retrieve and use an outdated image from the queue
|
|
|
|
|
if (nextTemplateInfo.Index < 0)
|
|
|
|
|
{
|
|
|
|
|
Bitmap outdatedImage = null;
|
2024-10-14 20:22:07 +00:00
|
|
|
|
|
2024-10-20 19:16:46 +00:00
|
|
|
|
parent.Log($"Finding a previous screen of {nextTemplateInfo.FileName}...");
|
|
|
|
|
|
2024-10-14 20:22:07 +00:00
|
|
|
|
try
|
2024-10-06 18:39:28 +00:00
|
|
|
|
{
|
2024-10-14 20:22:07 +00:00
|
|
|
|
while (outdatedSamples.Count > 0)
|
2024-10-06 18:39:28 +00:00
|
|
|
|
{
|
2024-10-14 20:22:07 +00:00
|
|
|
|
outdatedImage = outdatedSamples.Dequeue();
|
|
|
|
|
if (outdatedImage.Tag != null)
|
|
|
|
|
{
|
|
|
|
|
if (((SampleInfo)outdatedImage.Tag).FileName == nextTemplateInfo.FileName)
|
|
|
|
|
{
|
2024-10-20 19:16:46 +00:00
|
|
|
|
parent.Log($"Found the previous screen of {nextTemplateInfo.FileName}");
|
2024-10-14 20:22:07 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 18:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-14 20:22:07 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-10-20 19:16:46 +00:00
|
|
|
|
parent.Log($"Error finding a previous screen: {ex.Message}");
|
2024-10-14 20:22:07 +00:00
|
|
|
|
}
|
2024-10-06 18:39:28 +00:00
|
|
|
|
|
|
|
|
|
// Find the matching positions of the outdated image in the main image
|
|
|
|
|
if (outdatedImage != null) {
|
2024-10-20 19:16:46 +00:00
|
|
|
|
matchPositions = FindTemplate(out_mainImage, outdatedImage);
|
|
|
|
|
if (matchPositions.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
parent.Log("Match found with the outdated image");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parent.Log("No match found with the outdated image");
|
|
|
|
|
}
|
2024-10-06 18:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-20 19:16:46 +00:00
|
|
|
|
parent.Log("Not found a outdated image");
|
2024-10-06 18:39:28 +00:00
|
|
|
|
matchPositions = new List<Point>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// If the index is not negative, use the current image for template matching
|
2024-10-08 05:59:04 +00:00
|
|
|
|
matchPositions = FindTemplate(out_mainImage, (Bitmap)image.Clone());
|
2024-10-06 18:39:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 18:29:43 +00:00
|
|
|
|
foreach (Point matchPosition in matchPositions)
|
2024-08-02 07:51:49 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
try
|
2024-09-17 13:22:46 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
string text = sampleAny.Contains(templateName) ?
|
|
|
|
|
InspectSample((Bitmap)mainImage.Clone(), matchPosition, templateSize, templateName, sampleSize) : string.Empty;
|
|
|
|
|
|
|
|
|
|
results.Add(new ScreenMatchResult
|
|
|
|
|
{
|
2024-09-18 06:01:05 +00:00
|
|
|
|
FileName = templateName,
|
2024-09-18 05:33:15 +00:00
|
|
|
|
ScreenNumber = i,
|
|
|
|
|
Position = matchPosition,
|
|
|
|
|
Text = text
|
|
|
|
|
});
|
2024-09-18 18:29:43 +00:00
|
|
|
|
|
|
|
|
|
break; // Only one
|
2024-09-17 13:22:46 +00:00
|
|
|
|
}
|
2024-09-18 05:33:15 +00:00
|
|
|
|
catch (Exception ex)
|
2024-09-17 13:22:46 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
parent.Log($"Ignore the match. {ex.Message}");
|
2024-09-17 13:22:46 +00:00
|
|
|
|
}
|
2024-09-18 18:29:43 +00:00
|
|
|
|
}
|
2024-08-26 06:24:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 08:53:21 +00:00
|
|
|
|
if (results.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
parent.Log("Match found");
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-08-26 06:24:38 +00:00
|
|
|
|
{
|
|
|
|
|
parent.Log($"No match found");
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 12:05:17 +00:00
|
|
|
|
templateCurrentIndex = ++templateCurrentIndex % templateImages.Count;
|
2024-08-02 07:51:49 +00:00
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 18:29:43 +00:00
|
|
|
|
public Bitmap CropBitmap(Bitmap bitmap, Point matchPosition, Size templateSize, Size sampleSize, int dx = 0, int dy = 0)
|
2024-09-18 06:51:24 +00:00
|
|
|
|
{
|
|
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
public string InspectSample(Bitmap bitmap, Point matchPosition, Size templateSize, string templateName, Size sampleSize)
|
2024-09-02 05:17:06 +00:00
|
|
|
|
{
|
2024-09-02 06:30:20 +00:00
|
|
|
|
if (bitmap == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(bitmap), "Bitmap cannot be null.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 13:22:46 +00:00
|
|
|
|
if (matchPosition == null || matchPosition == Point.Empty)
|
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
throw new ArgumentException("matchPosition cannot be empty.");
|
2024-09-17 13:22:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 06:51:24 +00:00
|
|
|
|
// initialize the text
|
2024-09-02 05:17:06 +00:00
|
|
|
|
string text = "";
|
|
|
|
|
|
2024-09-02 06:30:20 +00:00
|
|
|
|
// Crop image
|
2024-09-18 18:29:43 +00:00
|
|
|
|
Bitmap croppedBitmap = CropBitmap(bitmap, matchPosition, templateSize, sampleSize, sampleAdjustX, sampleAdjustY);
|
2024-09-04 05:12:28 +00:00
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
// Save to the outdated samples
|
|
|
|
|
if (sampleNodup.Contains(templateName))
|
|
|
|
|
{
|
2024-09-18 18:29:43 +00:00
|
|
|
|
Bitmap croppedNodupBitmap = CropBitmap(bitmap, matchPosition, templateSize, sampleNodupSize);
|
|
|
|
|
uint bitmapCrc32 = ComputeBitmapCrc32(croppedNodupBitmap);
|
2024-10-06 18:39:28 +00:00
|
|
|
|
croppedNodupBitmap.Tag = new SampleInfo(templateName, bitmapCrc32);
|
2024-09-20 00:36:44 +00:00
|
|
|
|
|
2024-10-06 18:39:28 +00:00
|
|
|
|
bool bitmapExists = outdatedSamples.Any(x => ((SampleInfo)x.Tag).Crc32 == bitmapCrc32);
|
2024-09-18 05:33:15 +00:00
|
|
|
|
if (bitmapExists)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"This may be a duplicate request. {templateName}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-10-06 18:39:28 +00:00
|
|
|
|
outdatedSamples.Enqueue(croppedNodupBitmap);
|
2024-09-18 05:33:15 +00:00
|
|
|
|
parent.Log($"Added to the image queue. {templateName}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-04 05:12:28 +00:00
|
|
|
|
// if use Clipboard
|
2024-09-18 18:29:43 +00:00
|
|
|
|
if (sampleClipboard.Contains(templateName))
|
2024-09-04 05:12:28 +00:00
|
|
|
|
{
|
2024-09-20 07:53:50 +00:00
|
|
|
|
parent.Log($"Trying to use the clipboard... {templateName}");
|
2024-09-04 07:18:05 +00:00
|
|
|
|
Thread th = new Thread(new ThreadStart(() =>
|
2024-09-04 05:12:28 +00:00
|
|
|
|
{
|
2024-09-04 07:18:05 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2024-09-04 07:20:48 +00:00
|
|
|
|
Clipboard.SetImage((Bitmap)croppedBitmap.Clone());
|
2024-09-04 07:18:05 +00:00
|
|
|
|
parent.Log($"Copied the image to Clipboard");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
parent.Log($"Failed to copy to the clipboard: {ex.Message}");
|
2024-09-04 07:18:05 +00:00
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
th.SetApartmentState(ApartmentState.STA);
|
|
|
|
|
th.Start();
|
2024-09-04 05:12:28 +00:00
|
|
|
|
}
|
2024-09-02 05:17:06 +00:00
|
|
|
|
|
2024-09-04 05:12:28 +00:00
|
|
|
|
// if use OCR
|
2024-09-18 18:29:43 +00:00
|
|
|
|
if (sampleOcr.Contains(templateName))
|
2024-09-02 05:17:06 +00:00
|
|
|
|
{
|
2024-09-04 05:12:28 +00:00
|
|
|
|
try
|
2024-09-02 05:17:06 +00:00
|
|
|
|
{
|
2024-09-04 05:12:28 +00:00
|
|
|
|
using (var engine = new TesseractEngine(tesseractDataPath, tesseractLanguage, EngineMode.Default))
|
|
|
|
|
{
|
|
|
|
|
using (var page = engine.Process(croppedBitmap))
|
|
|
|
|
{
|
|
|
|
|
text = page.GetText();
|
2024-09-02 05:17:06 +00:00
|
|
|
|
|
2024-09-04 05:12:28 +00:00
|
|
|
|
parent.Log($"Mean confidence: {page.GetMeanConfidence()}");
|
|
|
|
|
parent.Log($"Text (GetText): {text}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
parent.Log($"Failed to OCR: {ex.Message}");
|
2024-09-02 05:17:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-25 16:23:02 +00:00
|
|
|
|
public Bitmap CaptureScreen(Screen screen)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
|
|
|
|
Rectangle screenSize = screen.Bounds;
|
|
|
|
|
|
2024-08-25 13:43:34 +00:00
|
|
|
|
DEVMODE dm = new DEVMODE();
|
|
|
|
|
dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
|
|
|
|
|
EnumDisplaySettings(screen.DeviceName, -1, ref dm);
|
|
|
|
|
|
|
|
|
|
var scalingFactor = Math.Round(Decimal.Divide(dm.dmPelsWidth, screen.Bounds.Width), 2);
|
2024-08-26 06:24:38 +00:00
|
|
|
|
parent.Log($"Resolved the screen scale: {scalingFactor}");
|
|
|
|
|
|
2024-08-25 13:43:34 +00:00
|
|
|
|
int adjustedWidth = (int)(screenSize.Width * scalingFactor);
|
|
|
|
|
int adjustedHeight = (int)(screenSize.Height * scalingFactor);
|
|
|
|
|
|
|
|
|
|
Bitmap bitmap = new Bitmap(adjustedWidth, adjustedHeight);
|
|
|
|
|
using (Graphics bitmapGraphics = Graphics.FromImage(bitmap))
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-25 13:43:34 +00:00
|
|
|
|
bitmapGraphics.CopyFromScreen(screenSize.Left, screenSize.Top, 0, 0, new Size(adjustedWidth, adjustedHeight));
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 06:24:38 +00:00
|
|
|
|
return bitmap;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 윈도우 핸들을 기준으로 찾기
|
|
|
|
|
public List<ScreenMatchResult> CaptureAndMatchAllWindows()
|
|
|
|
|
{
|
|
|
|
|
var results = new List<ScreenMatchResult>();
|
|
|
|
|
|
|
|
|
|
// 모든 윈도우 핸들을 열거
|
|
|
|
|
EnumWindows((hWnd, lParam) =>
|
|
|
|
|
{
|
|
|
|
|
if (IsWindowVisible(hWnd))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string windowTitle = GetWindowTitle(hWnd);
|
2024-08-24 13:24:26 +00:00
|
|
|
|
string processName = GetProcessName(hWnd);
|
|
|
|
|
GetWindowRect(hWnd, out RECT windowRect);
|
2024-09-18 05:33:15 +00:00
|
|
|
|
Point windowPosition = new Point(windowRect.Left, windowRect.Top);
|
2024-07-29 12:43:14 +00:00
|
|
|
|
Bitmap windowImage = CaptureWindow(hWnd);
|
2024-08-24 13:24:26 +00:00
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
if (windowImage != null)
|
|
|
|
|
{
|
2024-08-12 12:05:17 +00:00
|
|
|
|
Bitmap image = templateImages[templateCurrentIndex];
|
2024-09-18 06:01:05 +00:00
|
|
|
|
string templateName = image.Tag as string;
|
|
|
|
|
Size templateSize = new Size
|
|
|
|
|
{
|
|
|
|
|
Width = image.Width,
|
|
|
|
|
Height = image.Height
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
List<Point> matchPositions = FindTemplate(windowImage, image);
|
|
|
|
|
matchPositions.ForEach((matchPosition) =>
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-09-18 06:01:05 +00:00
|
|
|
|
try
|
2024-09-18 05:33:15 +00:00
|
|
|
|
{
|
2024-09-18 06:01:05 +00:00
|
|
|
|
string text = sampleAny.Contains(templateName) ?
|
|
|
|
|
InspectSample((Bitmap)windowImage.Clone(), matchPosition, templateSize, templateName, sampleSize) : string.Empty;
|
|
|
|
|
|
|
|
|
|
results.Add(new ScreenMatchResult
|
|
|
|
|
{
|
|
|
|
|
FileName = templateName,
|
|
|
|
|
WindowHandle = hWnd,
|
|
|
|
|
WindowTitle = windowTitle,
|
|
|
|
|
ProcessName = processName,
|
|
|
|
|
WindowPosition = windowPosition,
|
|
|
|
|
Position = matchPosition,
|
|
|
|
|
Text = text
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
parent.Log($"Ignore the match. {ex.Message}");
|
|
|
|
|
}
|
2024-09-18 05:33:15 +00:00
|
|
|
|
});
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-18 05:33:15 +00:00
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
parent.Log($"Error {ex.Message}");
|
|
|
|
|
}
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}, IntPtr.Zero);
|
|
|
|
|
|
2024-08-12 12:05:17 +00:00
|
|
|
|
templateCurrentIndex = ++templateCurrentIndex % templateImages.Count;
|
2024-08-02 07:51:49 +00:00
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetWindowTitle(IntPtr hWnd)
|
|
|
|
|
{
|
|
|
|
|
int length = GetWindowTextLength(hWnd);
|
|
|
|
|
StringBuilder sb = new StringBuilder(length + 1);
|
|
|
|
|
GetWindowText(hWnd, sb, sb.Capacity);
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-24 13:24:26 +00:00
|
|
|
|
public string GetProcessName(IntPtr hWnd)
|
|
|
|
|
{
|
|
|
|
|
uint processId;
|
|
|
|
|
GetWindowThreadProcessId(hWnd, out processId);
|
|
|
|
|
Process process = Process.GetProcessById((int)processId);
|
|
|
|
|
return process.ProcessName;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
public Bitmap CaptureWindow(IntPtr hWnd)
|
|
|
|
|
{
|
|
|
|
|
GetWindowRect(hWnd, out RECT rect);
|
|
|
|
|
int width = rect.Right - rect.Left;
|
|
|
|
|
int height = rect.Bottom - rect.Top;
|
|
|
|
|
|
|
|
|
|
if (width <= 0 || height <= 0)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Bitmap bitmap = new Bitmap(width, height);
|
|
|
|
|
Graphics graphics = Graphics.FromImage(bitmap);
|
|
|
|
|
IntPtr hDC = graphics.GetHdc();
|
|
|
|
|
IntPtr windowDC = GetDC(hWnd);
|
|
|
|
|
|
|
|
|
|
bool success = BitBlt(hDC, 0, 0, width, height, windowDC, 0, 0, SRCCOPY);
|
|
|
|
|
ReleaseDC(hWnd, windowDC);
|
|
|
|
|
graphics.ReleaseHdc(hDC);
|
|
|
|
|
|
|
|
|
|
return success ? bitmap : null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
public List<Point> FindTemplate(Bitmap mainImage, Bitmap templateImage)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
var matches = new List<Point>();
|
|
|
|
|
|
2024-07-29 12:43:14 +00:00
|
|
|
|
int mainWidth = mainImage.Width;
|
|
|
|
|
int mainHeight = mainImage.Height;
|
|
|
|
|
int templateWidth = templateImage.Width;
|
|
|
|
|
int templateHeight = templateImage.Height;
|
|
|
|
|
|
2024-08-25 16:23:02 +00:00
|
|
|
|
int startX = isSearchFromEnd ? mainWidth - templateWidth : 0;
|
|
|
|
|
int endX = isSearchFromEnd ? -1 : mainWidth - templateWidth + 1;
|
|
|
|
|
int stepX = isSearchFromEnd ? -1 : 1;
|
2024-08-25 08:57:22 +00:00
|
|
|
|
|
2024-08-25 16:23:02 +00:00
|
|
|
|
int startY = isSearchFromEnd ? mainHeight - templateHeight : 0;
|
|
|
|
|
int endY = isSearchFromEnd ? -1 : mainHeight - templateHeight + 1;
|
|
|
|
|
int stepY = isSearchFromEnd ? -1 : 1;
|
2024-08-25 08:57:22 +00:00
|
|
|
|
|
|
|
|
|
for (int x = startX; x != endX; x += stepX)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-25 08:57:22 +00:00
|
|
|
|
for (int y = startY; y != endY; y += stepY)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-12 12:05:17 +00:00
|
|
|
|
if (IsTemplateMatch(mainImage, templateImage, x, y, threshold))
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-09-18 05:33:15 +00:00
|
|
|
|
matches.Add(new Point(x, y));
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 05:33:15 +00:00
|
|
|
|
return matches;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-12 12:05:17 +00:00
|
|
|
|
private bool IsTemplateMatch(Bitmap mainImage, Bitmap templateImage, int offsetX, int offsetY, double threshold)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
|
|
|
|
int templateWidth = templateImage.Width;
|
|
|
|
|
int templateHeight = templateImage.Height;
|
2024-08-02 07:51:49 +00:00
|
|
|
|
int totalPixels = templateWidth * templateHeight;
|
|
|
|
|
int requiredMatches = (int)(totalPixels * threshold);
|
2024-09-04 08:26:31 +00:00
|
|
|
|
|
|
|
|
|
// When the square root of the canvas size of the image to be matched is less than 10, a complete match is applied.
|
|
|
|
|
if (Math.Sqrt(templateWidth * templateHeight) < 10.0)
|
|
|
|
|
{
|
|
|
|
|
for (int y = 0; y < templateHeight; y++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < templateWidth; x++)
|
|
|
|
|
{
|
|
|
|
|
if (mainImage.GetPixel(x + offsetX, y + offsetY) != templateImage.GetPixel(x, y))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Otherwise, randomness is used.
|
2024-08-02 07:51:49 +00:00
|
|
|
|
int matchedCount = 0;
|
|
|
|
|
Random rand = new Random();
|
|
|
|
|
while (matchedCount < requiredMatches)
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-02 07:51:49 +00:00
|
|
|
|
int x = rand.Next(templateWidth);
|
|
|
|
|
int y = rand.Next(templateHeight);
|
|
|
|
|
|
|
|
|
|
if (mainImage.GetPixel(x + offsetX, y + offsetY) != templateImage.GetPixel(x, y))
|
2024-07-29 12:43:14 +00:00
|
|
|
|
{
|
2024-08-02 07:51:49 +00:00
|
|
|
|
return false;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
2024-08-02 07:51:49 +00:00
|
|
|
|
|
|
|
|
|
matchedCount++;
|
2024-07-29 12:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-08-25 16:23:02 +00:00
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
private Bitmap ImageQuantize(Bitmap image, int levels = 4)
|
2024-08-25 16:23:02 +00:00
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
Bitmap quantizedImage = new Bitmap(image.Width, image.Height);
|
2024-08-26 06:24:38 +00:00
|
|
|
|
if (image.Tag != null)
|
|
|
|
|
{
|
2024-10-08 05:59:04 +00:00
|
|
|
|
quantizedImage.Tag = image.Tag;
|
2024-08-26 06:24:38 +00:00
|
|
|
|
}
|
2024-08-25 16:23:02 +00:00
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
int step = 255 / (levels - 1); // step by step..... ooh baby...(?)
|
|
|
|
|
|
2024-08-25 16:23:02 +00:00
|
|
|
|
for (int y = 0; y < image.Height; y++)
|
|
|
|
|
{
|
|
|
|
|
for (int x = 0; x < image.Width; x++)
|
|
|
|
|
{
|
|
|
|
|
// Convert the pixel to grayscale
|
|
|
|
|
Color pixelColor = image.GetPixel(x, y);
|
|
|
|
|
byte grayValue = (byte)((pixelColor.R + pixelColor.G + pixelColor.B) / 3);
|
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
// Convert the grayscale value to the quantize value
|
|
|
|
|
byte quantizedValue = (byte)((grayValue / step) * step);
|
|
|
|
|
|
|
|
|
|
// Renew the colors
|
|
|
|
|
Color quantizedColor = Color.FromArgb(quantizedValue, quantizedValue, quantizedValue);
|
|
|
|
|
quantizedImage.SetPixel(x, y, quantizedColor);
|
2024-08-25 16:23:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-08 05:59:04 +00:00
|
|
|
|
return quantizedImage;
|
2024-08-25 16:23:02 +00:00
|
|
|
|
}
|
2024-09-18 18:29:43 +00:00
|
|
|
|
|
|
|
|
|
private uint ComputeBitmapCrc32(Bitmap bitmap)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
|
|
|
|
|
|
|
|
|
|
byte[] bitmapBytes = ms.ToArray();
|
|
|
|
|
Crc32 crc32 = new Crc32();
|
|
|
|
|
crc32.Append(bitmapBytes);
|
|
|
|
|
|
|
|
|
|
return BitConverter.ToUInt32(crc32.GetCurrentHash(), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-19 14:28:16 +00:00
|
|
|
|
}
|