所以我正在开发一个从屏幕上进行颜色检测的程序,我有这个功能,可以通过搜索屏幕找到我传递的值的颜色。
bool findColor(float hue, float sat, float vib, float tol, RECT reg, POINT& loc) {
{
HWND target = FindWindow(NULL, WINDOWNAME);
if (!target)
{
printf("error, no window\n");
}
RECT rc;
GetWindowRect(target, &rc);
int regLocx, regLocy;
int x = rc.left + reg.left;
int y = rc.top + reg.top;
int w = reg.right - reg.left;
int h = reg.bottom - reg. top;
int screen_w = GetSystemMetrics(SM_CXFULLSCREEN);
int screen_h = GetSystemMetrics(SM_CYFULLSCREEN);
HDC hdc = GetDC(HWND_DESKTOP);
HBITMAP hbitmap = CreateCompatibleBitmap(hdc, screen_w, screen_h);
HDC memdc = CreateCompatibleDC(hdc);
HGDIOBJ oldbmp = SelectObject(memdc, hbitmap);
BitBlt(memdc, 0, 0, w, h, hdc, x, y, CAPTUREBLT | SRCCOPY);
SelectObject(memdc, oldbmp);
BITMAPINFOHEADER infohdr = { sizeof(infohdr), w, h, 1, 32 };
int size = w * h * 4;
std::vector<BYTE> bits(size);
int res = GetDIBits(hdc, hbitmap, 0, h, &bits[0],
(BITMAPINFO*)&infohdr, DIB_RGB_COLORS);
if (res != h)
{
std::cout << "error\n";
}
BYTE *ptr = bits.data();
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)
{
int col = x;
int row = h - y - 1;
int index = (row * w + col) * 4;
BYTE blu = bits[index + 0];
BYTE grn = bits[index + 1];
BYTE red = bits[index + 2];
//Seting it up
HSV value = RGBToHSV(RGB1(red, grn, blu));
if ((value.H > hue - tol) && (value.H < hue + tol) && (value.S > sat - maxSat) && (value.S < sat + maxSat) && (value.V > vib - maxVib) && (value.V < vib + maxVib)) {
loc.x = x;
loc.y = y;
SelectObject(memdc, oldbmp);
DeleteObject(hbitmap);
ReleaseDC(HWND_DESKTOP, hdc);
DeleteDC(memdc);
return true;
}
}
}
SelectObject(memdc, oldbmp);
DeleteObject(hbitmap);
ReleaseDC(HWND_DESKTOP, hdc);
DeleteDC(memdc);
return false;
}}
我的下一个任务是开发可以搜索从.bmp加载的文件的相同工具。到目前为止我有这方面的进展:
bool LoadBMPIntoMEMDC(HDC hDC, LPCTSTR bmpfile){
HBITMAP hBMP = (HBITMAP)LoadImage(NULL, "yellow.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hdc = GetDC(HWND_DESKTOP);
//SaveHBITMAPToFile("yolo.bmp", hBMP);
HDC memdc = CreateCompatibleDC(hdc);
HGDIOBJ oldbmp = SelectObject(memdc, hBMP);
//BitBlt(hdc, 0, 0, 800, 800, memdc, 0, 0, CAPTUREBLT | SRCCOPY);
//SelectObject(memdc, oldbmp);
BITMAPINFOHEADER infohdr = { sizeof(infohdr), 800, 800, 1, 24 };
int size = 800 * 800 * 3;
std::vector<BYTE> bits(size);
int res = GetDIBits(memdc, hBMP, 0, 800, &bits[0],
(BITMAPINFO*)&infohdr, DIB_RGB_COLORS);
BYTE *ptr = bits.data();
for (int y = 0; y < 800; y++)
{
for (int x = 0; x < 800; x++)
{
int col = x;
int row = 800 - y - 1;
int index = (row * 800 + col) * 3;
BYTE blu = bits[index + 0];
BYTE grn = bits[index + 1];
BYTE red = bits[index + 2];
//Seting it up
HSV value = RGBToHSV(RGB1(red, grn, blu));
std::cout << value.H << ", " << value.S << ", " << value.V << "\n";
}
}
return true;
}
我让它搜索一个24位的蓝色100x100 bmp,但是我从未获得过的值。 240为色调或每次迭代为0。
答案 0 :(得分:0)
bool LoadBMPIntoMEMDC(LPCTSTR bmpfile, int startx, int starty, int width, int height)
{
HBITMAP hBMP = (HBITMAP)LoadImage(NULL, bmpfile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
HDC hdc = GetDC(HWND_DESKTOP);
HDC memdc = CreateCompatibleDC(hdc);
HGDIOBJ oldbmp = SelectObject(memdc, hBMP);
BitBlt(memdc, 0, 0, width, height, memdc, startx, starty, CAPTUREBLT | SRCCOPY);
BITMAPINFOHEADER infohdr = { sizeof(infohdr), width, height, 1, 24 };
int size = width * height * 3;
std::vector<BYTE> bits(size);
int res = GetDIBits(memdc, hBMP, 0, height, &bits[0],
(BITMAPINFO*)&infohdr, DIB_RGB_COLORS);
BYTE *ptr = bits.data();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int col = x;
int row = height - y - 1;
int index = (row * width + col) * 3;
BYTE blu = bits[index + 0];
BYTE grn = bits[index + 1];
BYTE red = bits[index + 2];
}
}
return true;
}