-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcapture screen.cpp
More file actions
389 lines (321 loc) · 12.6 KB
/
Copy pathcapture screen.cpp
File metadata and controls
389 lines (321 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // MUST be BEFORE OpenCV
#include "capture screen.h"
#include <opencv2/opencv.hpp>
#include <string>
#include <fstream>
#include <iostream>
#include <thread>
#include <chrono>
// Define ADB path here
extern std::string g_AdbPath;
// ----------------------------------------------------------------------------
// HELPER: RUN COMMAND SILENTLY (NO WINDOW POPUP)
// ----------------------------------------------------------------------------
void RunSilentCommand(std::string command) {
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; // Hides the window
ZeroMemory(&pi, sizeof(pi));
// Execute command with CREATE_NO_WINDOW flag
if (CreateProcessA(
NULL, // Application Name
(LPSTR)command.c_str(), // Command Line
NULL, // Process Attributes
NULL, // Thread Attributes
FALSE, // Inherit Handles
CREATE_NO_WINDOW, // Creation Flags (This stops the CMD popup)
NULL, // Environment
NULL, // Current Directory
&si, // Startup Info
&pi) // Process Info
)
{
// Wait for process to finish
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
int g_SleepAfterScreenshot = 400; // 400 by default.
// Helper: Is file valid?
bool IsFileValid(const std::string& path) {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file.is_open()) return false;
return file.tellg() > 1024; // Valid if larger than 1KB
}
// ----------------------------------------------------------------------------
// ENHANCED SCREEN CAPTURE (With Retry Mechanism + Silent Mode)
// ----------------------------------------------------------------------------
cv::Mat CaptureAdbScreen(bool grayscale)
{
// Using Public folder to avoid permission issues
std::string tempFile = "tempfile\\adb_temp_screen.png";
int maxRetries = 3;
cv::Mat img;
for (int i = 0; i < maxRetries; ++i) {
// 1. Delete old file
remove(tempFile.c_str());
// 2. Prepare command (Protected against quote errors)
// We use cmd /c so redirection (>) works
std::string cmd = "cmd /c \"\"" + g_AdbPath + "\" exec-out screencap -p > \"" + tempFile + "\"\"";
// 3. Execute (SILENTLY - NO POPUP)
RunSilentCommand(cmd);
// 4. Small wait for file write
std::this_thread::sleep_for(std::chrono::milliseconds(g_SleepAfterScreenshot));
// 5. File check
if (!IsFileValid(tempFile)) {
continue;
}
// 6. Read image
int flags = grayscale ? cv::IMREAD_GRAYSCALE : cv::IMREAD_COLOR;
img = cv::imread(tempFile, flags);
// 7. Is image valid?
if (!img.empty()) {
return img; // Success
}
}
return cv::Mat();
}
// ----------------------------------------------------------------------------
// 1. SCREEN SCAN (FIELD FINDING) - ROI ADDED
// ----------------------------------------------------------------------------
cv::Mat screenscan(const std::string& templatePath, int& tarlakonumuX, int& tarlakonumuY) {
// Get color image
cv::Mat fullFrame = CaptureAdbScreen(false);
if (fullFrame.empty()) return cv::Mat();
// --- ROI CROP (IGNORE BOTTOM MENU) ---
int roiHeight = (int)(fullFrame.rows * 0.80); // Top 80%
cv::Rect searchRect(0, 0, fullFrame.cols, roiHeight);
cv::Mat croppedFrame = fullFrame(searchRect);
// Load template
cv::Mat templ = cv::imread(templatePath, cv::IMREAD_COLOR);
if (templ.empty()) {
return fullFrame;
}
cv::Mat result;
cv::matchTemplate(croppedFrame, templ, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
if (maxVal >= 0.70) {
// Calculate position (No offset needed since crop starts at 0,0)
tarlakonumuX = maxLoc.x + (templ.cols / 2);
tarlakonumuY = maxLoc.y + (templ.rows / 2);
}
else {
tarlakonumuX = -1;
tarlakonumuY = -1;
}
return fullFrame;
}
// ----------------------------------------------------------------------------
// 2. WHEAT SCAN (WHEAT FINDING) - ROI ADDED
// ----------------------------------------------------------------------------
cv::Mat wheatscan(const std::string& templatePath, int& wheatkonumuX, int& wheatkonumuY) {
cv::Mat fullFrame = CaptureAdbScreen(false);
if (fullFrame.empty()) return cv::Mat();
// --- ROI CROP ---
int roiHeight = (int)(fullFrame.rows * 0.80);
cv::Rect searchRect(0, 0, fullFrame.cols, roiHeight);
cv::Mat croppedFrame = fullFrame(searchRect);
cv::Mat templ = cv::imread(templatePath, cv::IMREAD_COLOR);
if (templ.empty()) {
return fullFrame;
}
cv::Mat result;
cv::matchTemplate(croppedFrame, templ, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
if (maxVal >= 0.70) {
wheatkonumuX = maxLoc.x + (templ.cols / 2);
wheatkonumuY = maxLoc.y + (templ.rows / 2);
}
else {
wheatkonumuX = -1;
wheatkonumuY = -1;
}
return fullFrame;
}
// ----------------------------------------------------------------------------
// 3. SICKLE SCAN (SICKLE FINDING) - ROI ADDED
// ----------------------------------------------------------------------------
cv::Mat sicklescan(const std::string& templatePath, int& sicklekonumuX, int& sicklekonumuY) {
cv::Mat fullFrame = CaptureAdbScreen(false);
if (fullFrame.empty()) return cv::Mat();
// --- ROI CROP ---
int roiHeight = (int)(fullFrame.rows * 0.80);
cv::Rect searchRect(0, 0, fullFrame.cols, roiHeight);
cv::Mat croppedFrame = fullFrame(searchRect);
cv::Mat sicktempl = cv::imread(templatePath, cv::IMREAD_COLOR);
if (sicktempl.empty()) {
return fullFrame;
}
cv::Mat result;
cv::matchTemplate(croppedFrame, sicktempl, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
if (maxVal >= 0.72) {
sicklekonumuX = maxLoc.x + (sicktempl.cols / 2);
sicklekonumuY = maxLoc.y + (sicktempl.rows / 2);
}
else {
sicklekonumuX = -1;
sicklekonumuY = -1;
}
return fullFrame;
}
// ----------------------------------------------------------------------------
// 4. GROWN SCAN (GROWN WHEAT FINDING) - ROI ADDED
// ----------------------------------------------------------------------------
cv::Mat grownscan(const std::string& templatePath, int& grownkonumuX, int& grownkonumuY) {
// 1. Capture screen (Getting color but will convert to grayscale)
cv::Mat fullFrame = CaptureAdbScreen(false);
if (fullFrame.empty()) return cv::Mat();
// 2. ROI Crop
int roiHeight = (int)(fullFrame.rows * 0.80);
cv::Rect searchRect(0, 0, fullFrame.cols, roiHeight);
cv::Mat croppedFrame = fullFrame(searchRect);
// 3. Load template
cv::Mat growntempl = cv::imread(templatePath, cv::IMREAD_COLOR);
if (growntempl.empty()) {
return fullFrame;
}
// --- CRITICAL SETTING: GRAYSCALE CONVERSION ---
// Color matching fails on swaying wheat in the wind.
// Grayscale matching ignores light changes, focuses on shape.
cv::Mat grayFrame, grayTempl;
cv::cvtColor(croppedFrame, grayFrame, cv::COLOR_BGR2GRAY);
cv::cvtColor(growntempl, grayTempl, cv::COLOR_BGR2GRAY);
// 4. Matching (On grayscale images)
cv::Mat result;
cv::matchTemplate(grayFrame, grayTempl, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
// Grayscale matching works perfectly at 0.65 - 0.70 range.
// If still not found, you can set this to 0.60.
if (maxVal >= 0.64) {
grownkonumuX = maxLoc.x + (growntempl.cols / 2);
grownkonumuY = maxLoc.y + (growntempl.rows / 2);
}
else {
grownkonumuX = -1;
grownkonumuY = -1;
}
// Return color version for display
return fullFrame;
}
// ----------------------------------------------------------------------------
// 5. FIND TEMPLATE MATCHES (GENERAL SEARCH) - ROI ADDED
// ----------------------------------------------------------------------------
cv::Mat FindTemplateMatches(
const std::string& templatePath,
float threshold,
std::vector<cv::Point>& matches,
cv::Point& bestMatch,
cv::Size& templSize,
double* outBestScore
) {
// 1. Capture color image
cv::Mat fullFrame = CaptureAdbScreen(false);
if (fullFrame.empty()) return cv::Mat();
// 2. ROI Crop (Remove bottom menu)
int roiHeight = (int)(fullFrame.rows * 0.80);
cv::Rect searchRect(0, 0, fullFrame.cols, roiHeight);
cv::Mat croppedFrame = fullFrame(searchRect);
// 3. Load template
cv::Mat templ = cv::imread(templatePath, cv::IMREAD_COLOR);
if (templ.empty()) {
return fullFrame;
}
templSize = templ.size();
// 4. Matching
cv::Mat result;
cv::matchTemplate(croppedFrame, templ, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
// Save best match
bestMatch = maxLoc;
if (outBestScore) {
*outBestScore = maxVal;
}
// 5. Find multiple matches
matches.clear();
for (int y = 0; y < result.rows; y++)
{
for (int x = 0; x < result.cols; x++)
{
if (result.at<float>(y, x) >= threshold)
{
// Add position found within ROI to the list.
// No offset needed since crop starts at (0,0).
matches.emplace_back(x, y);
}
}
}
return fullFrame; // Return original full image (for GUI display)
}
// ----------------------------------------------------------------------------
// 6. WINDOW CAPTURE (Legacy function)
// ----------------------------------------------------------------------------
cv::Mat ekranYakala(HWND hwnd) {
RECT memuekrani;
GetClientRect(hwnd, &memuekrani);
int genislik = memuekrani.right - memuekrani.left;
int yukseklik = memuekrani.bottom - memuekrani.top;
HDC hdcWindow = GetDC(hwnd);
HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcWindow, genislik, yukseklik);
SelectObject(hdcMemDC, hBitmap);
BitBlt(hdcMemDC, 0, 0, genislik, yukseklik, hdcWindow, 0, 0, SRCCOPY);
BITMAPINFOHEADER bi{};
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = genislik;
bi.biHeight = -yukseklik;
bi.biPlanes = 1;
bi.biBitCount = 24;
bi.biCompression = BI_RGB;
cv::Mat mat(yukseklik, genislik, CV_8UC3);
GetDIBits(hdcWindow, hBitmap, 0, yukseklik, mat.data, reinterpret_cast<BITMAPINFO*>(&bi), DIB_RGB_COLORS);
DeleteObject(hBitmap);
DeleteDC(hdcMemDC);
ReleaseDC(hwnd, hdcWindow);
return mat;
}
// ----------------------------------------------------------------------------
// 7. SHOP SCAN (SHOP ICON FINDING)
// ----------------------------------------------------------------------------
cv::Mat shopscan(const std::string& templatePath, int& shopkonumuX, int& shopkonumuY) {
// 1. Capture screen (Color)
cv::Mat shopFrame = CaptureAdbScreen(false);
if (shopFrame.empty()) return cv::Mat();
// 2. Load template
cv::Mat shoptempl = cv::imread(templatePath, cv::IMREAD_COLOR);
if (shoptempl.empty()) {
return shopFrame;
}
// 3. Matching (On full screen)
cv::Mat result;
cv::matchTemplate(shopFrame, shoptempl, result, cv::TM_CCOEFF_NORMED);
double maxVal;
cv::Point maxLoc;
cv::minMaxLoc(result, nullptr, &maxVal, nullptr, &maxLoc);
// Fixed icon, 0.70 - 0.72 threshold is safe.
if (maxVal >= 0.70) {
shopkonumuX = maxLoc.x + (shoptempl.cols / 2);
shopkonumuY = maxLoc.y + (shoptempl.rows / 2);
}
else {
shopkonumuX = -1;
shopkonumuY = -1;
}
return shopFrame;
}