2013-04-19 23 views
6

Lavorare con le bitmap è molto nuovo per me quindi sono stato davvero alle prese con le esercitazioni online e le strategie che ho letto. Fondamentalmente il mio obiettivo è scansionare lo schermo per un particolare valore RGB. Credo che la procedura per farlo sia quella di acquisire lo schermo in una hBitmap e quindi produrre una serie di valori RGB da cui posso eseguire la scansione.C++ Ottenere RGB da hBitmap

Originariamente ho iniziato con GetPixel ma è molto lento. La soluzione era usare GetDIBits che produce l'array di valori RGB. Il problema è che restituisce invece valori RGB strani e forse casuali.

Sto utilizzando il seguente codice che ho trovato da un altro tutorial:

/* Globals */ 
int ScreenX = GetDeviceCaps(GetDC(0), HORZRES); 
int ScreenY = GetDeviceCaps(GetDC(0), VERTRES); 
BYTE* ScreenData = new BYTE[3*ScreenX*ScreenY]; 

void ScreenCap() { 
    HDC hdc = GetDC(GetDesktopWindow()); 
    HDC hdcMem = CreateCompatibleDC (hdc); 
    HBITMAP hBitmap = CreateCompatibleBitmap(hdc, ScreenX, ScreenY); 
    BITMAPINFOHEADER bmi = {0}; 
    bmi.biSize = sizeof(BITMAPINFOHEADER); 
    bmi.biPlanes = 1; 
    bmi.biBitCount = 24; 
    bmi.biWidth = ScreenX; 
    bmi.biHeight = -ScreenY; 
    bmi.biCompression = BI_RGB; 
    bmi.biSizeImage = ScreenX * ScreenY; 
    SelectObject(hdcMem, hBitmap); 
    BitBlt(hdcMem, 0, 0, ScreenX, ScreenY, hdc, 0, 0, SRCCOPY); 
    GetDIBits(hdc, hBitmap, 0, ScreenY, ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); 

    DeleteDC(hdcMem); 
    ReleaseDC(NULL, hdc); 
} 

inline int PosR(int x, int y) { 
    return ScreenData[3*((y*ScreenX)+x)+2]; 
} 

inline int PosG(int x, int y) { 
    return ScreenData[3*((y*ScreenX)+x)+1]; 
} 

inline int PosB(int x, int y) { 
    return ScreenData[3*((y*ScreenX)+x)]; 
} 

ho testare questo con il seguente codice. Ho premuto Maiusc per chiamare ScreenCap e quindi sposto il cursore nella posizione desiderata e premo Spazio per vedere quale sia il valore RGB in quella posizione. Sono completamente pazzo?

int main() { 

while (true) { 

    if (GetAsyncKeyState(VK_SPACE)){ 

     // Print out current cursor position 
     GetCursorPos(&p); 
     printf("X:%d Y:%d \n",p.x,p.y); 
     // Print out RGB value at that position 
     int r = PosR(p.x, p.y); 
     int g = PosG(p.x, p.y); 
     int b = PosB(p.x, p.y); 
     printf("r:%d g:%d b:%d \n",r,g,b); 

    } else if (GetAsyncKeyState(VK_ESCAPE)){ 
     printf("Quit\n"); 
     break; 
    } else if (GetAsyncKeyState(VK_SHIFT)){ 
     ScreenCap(); 
     printf("Captured\n"); 
    } 
} 

system("PAUSE"); 
return 0; 
} 
+0

Stai richiedendo RGB, ma il codice sembra essere il trattamento dei dati come BGR . –

+0

Da quello che ho letto, credo che la natura di GetDIBits li restituisca in questo ordine. Ma suppongo che dovrei sottolineare che anche se ho provato su uno schermo completamente nero/bianco in cui tutti r = g = b, i valori di rgb sembrano ancora casuali. Quindi segnalerebbe il nero quando è effettivamente bianco, e il bianco quando a volte è nero. – Mike

+0

possibile duplicato di [GetDIBits e loop through pixel usando X, Y] (http://stackoverflow.com/questions/3688409/getdibits-and-loop-through-pixels-using-xy) – sashoalm

risposta

7

Il problema è che lo schermo è in realtà 32bit profondo non 24. Il codice qui sotto vi darà il risultato è necessario:

/* Globals */ 
int ScreenX = 0; 
int ScreenY = 0; 
BYTE* ScreenData = 0; 

void ScreenCap() 
{ 
    HDC hScreen = GetDC(GetDesktopWindow()); 
    ScreenX = GetDeviceCaps(hScreen, HORZRES); 
    ScreenY = GetDeviceCaps(hScreen, VERTRES); 

    HDC hdcMem = CreateCompatibleDC (hScreen); 
    HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, ScreenX, ScreenY); 
    HGDIOBJ hOld = SelectObject(hdcMem, hBitmap); 
    BitBlt(hdcMem, 0, 0, ScreenX, ScreenY, hScreen, 0, 0, SRCCOPY); 
    SelectObject(hdcMem, hOld); 

    BITMAPINFOHEADER bmi = {0}; 
    bmi.biSize = sizeof(BITMAPINFOHEADER); 
    bmi.biPlanes = 1; 
    bmi.biBitCount = 32; 
    bmi.biWidth = ScreenX; 
    bmi.biHeight = -ScreenY; 
    bmi.biCompression = BI_RGB; 
    bmi.biSizeImage = 0;// 3 * ScreenX * ScreenY; 

    if(ScreenData) 
     free(ScreenData); 
    ScreenData = (BYTE*)malloc(4 * ScreenX * ScreenY); 

    GetDIBits(hdcMem, hBitmap, 0, ScreenY, ScreenData, (BITMAPINFO*)&bmi, DIB_RGB_COLORS); 

    ReleaseDC(GetDesktopWindow(),hScreen); 
    DeleteDC(hdcMem); 
    DeleteObject(hBitmap); 
} 

inline int PosB(int x, int y) 
{ 
    return ScreenData[4*((y*ScreenX)+x)]; 
} 

inline int PosG(int x, int y) 
{ 
    return ScreenData[4*((y*ScreenX)+x)+1]; 
} 

inline int PosR(int x, int y) 
{ 
    return ScreenData[4*((y*ScreenX)+x)+2]; 
} 

bool ButtonPress(int Key) 
{ 
    bool button_pressed = false; 

    while(GetAsyncKeyState(Key)) 
     button_pressed = true; 

    return button_pressed; 
} 

int main() 
{ 
    while (true) 
    { 
     if (ButtonPress(VK_SPACE)) 
     { 

      // Print out current cursor position 
      POINT p; 
      GetCursorPos(&p); 
      printf("X:%d Y:%d \n",p.x,p.y); 
      // Print out RGB value at that position 
      std::cout << "Bitmap: r: " << PosR(p.x, p.y) << " g: " << PosG(p.x, p.y) << " b: " << PosB(p.x, p.y) << "\n"; 

     } else if (ButtonPress(VK_ESCAPE)) 
     { 
      printf("Quit\n"); 
      break; 
     } else if (ButtonPress(VK_SHIFT)) 
     { 
      ScreenCap(); 
      printf("Captured\n"); 
     } 
    } 

    system("PAUSE"); 
    return 0; 
} 
+1

[Apparentemente c'era una perdita di memoria bug in questa soluzione] (http://www.reddit.com/r/programming/comments/24vitp/question_quality_is_dropping_on_stack_overflow/chbcw7i), [ma è stato risolto ora] (http://stackoverflow.com/revisions/16115730/2) . –

0

La dimensione dell'immagine è specificato in pixel, dovrebbe essere specificato in byte

**bmi.biSizeImage = ScreenX * ScreenY;** 
**bmi.biBitCount = 24;** 
bmi.biWidth = ScreenX; 
bmi.biHeight = -ScreenY; 
**bmi.biCompression = BI_RGB;** 

biSizeImage sue unità definite sono byte e si specifica RGB 3 byte per pixel.

http://msdn.microsoft.com/en-us/library/windows/desktop/dd183376(v=vs.85).aspx

biSizeImage La dimensione, in byte , dell'immagine. Questo può essere impostato su zero per bitmap BI_RGB.