我想知道如何解决这个问题。我希望每X秒检查一次屏幕是否包含图像(例如,一个红点),如果是,则返回True。我非常熟悉Python,有一些简单的解决方案。但我还没有找到类似的解决方案。
我主要想做的是:
在screenshot
上拍摄screenshot
我研究了OpenCV,可能会以这种方式解决它,但可能有点扩展过度。我在考虑使用getPixel来循环遍历屏幕上的所有像素。但它的速度非常慢。
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
HWND runelite = GetForegroundWindow();
HMONITOR monitor = MonitorFromWindow(runelite, MONITOR_DEFAULTTONEAREST);
MONITORINFO info;
info.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &info);
int monitor_width = info.rcMonitor.right - info.rcMonitor.left;
int monitor_height = info.rcMonitor.bottom - info.rcMonitor.top;
int r, g, b;
HDC screenshot = GetDC(NULL);
for (int i = 0; i < monitor_height; i++) {
for (int j = 0; j < monitor_width; j++) {
DWORD color = GetPixel(screenshot, j, i);
cout << "Scanning -> X: " << j << " Y: " << i << endl;
r = GetRValue(color);
g = GetGValue(color);
b = GetBValue(color);
if (r == 0 && g == 0 && b == 0) {
cout << "Button found by color!" << endl;
goto end;
}
}
}
end:
ReleaseDC(NULL, screenshot);
return 0;
}
发布于 2019-04-29 04:28:32
如果您将HDC
的内容复制到另一个位图,并获得指向图像数据的指针并在其上循环,则可以极大地提高速度。
创建内存位图
HDC memDC = CreateCompatibleDC ( hDC );
HBITMAP memBM = CreateCompatibleBitmap ( hDC, nWidth, nHeight );
SelectObject ( memDC, memBM );
然后通过BitBlt
将屏幕数据比特转换为该位图,并使用GetDIBits
获取位图数据。
还请注意,GetDC(NULL)
不会截图,但会让你访问windows live main HDC。直接在桌面上绘制。这就是为什么它上的每一个GetPixel
都需要相当长的时间。
https://stackoverflow.com/questions/55893011
复制相似问题