2010-06-07 57 views
2

我想编写一个程序来播放全屏幕PC游戏的乐趣(如计算机视觉和人工智能的实验)。如何快速采集和处理实时画面输出

在这个实验中,我假定游戏对电脑玩家没有底层API(也可用源),所以我打算处理由游戏在屏幕上呈现的视觉信息。

游戏运行在Win32系统上全屏模式(直接-X我假设)。

目前我使用的是Win32函数

#include <windows.h> 
#include <cvaux.h> 
class Screen { 
    public: 
    HWND windowHandle; 
    HDC  windowContext; 
    HBITMAP buffer; 
    HDC  bufferContext; 
    CvSize size; 
    uchar* bytes; 
    int channels; 
Screen() { 
    windowHandle = GetDesktopWindow(); 
    windowContext = GetWindowDC (windowHandle); 
    size = cvSize (GetDeviceCaps (windowContext, HORZRES), GetDeviceCaps (windowContext, VERTRES)); 
    buffer = CreateCompatibleBitmap (windowContext, size.width, size.height); 
    bufferContext = CreateCompatibleDC (windowContext); 
    SelectObject (bufferContext, buffer); 
    channels = 4; 
    bytes = new uchar[size.width * size.height * channels]; 
} 

~Screen() { 
    ReleaseDC(windowHandle, windowContext); 
    DeleteDC(bufferContext); 
    DeleteObject(buffer); 
    delete[] bytes; 
} 

void CaptureScreen (IplImage* img) { 
    BitBlt(bufferContext, 0, 0, size.width, size.height, windowContext, 0, 0, SRCCOPY); 
    int n = size.width * size.height; 
    int imgChannels = img->nChannels; 

    GetBitmapBits (buffer, n * channels, bytes); 

    uchar* src = bytes; 
    uchar* dest = (uchar*) img->imageData; 
    uchar* end = dest + n * imgChannels; 

    while (dest < end) { 
     dest[0] = src[0]; 
     dest[1] = src[1]; 
     dest[2] = src[2]; 

     dest += imgChannels; 
     src += channels; 
    } 
} 

在这我可以处理使用这种方法实在太慢帧的速率。有没有更好的方法来获取屏幕框架?

+0

可能要到3D或游戏添加到您的meta标签 – 2010-06-07 00:41:26

+0

我认为根本问题比我的具体应用更普遍的(这恰好是一个等距RTS游戏) – Akusete 2010-06-07 00:45:32

回答

0

作为一般的方法,我会钩的缓冲器翻转的函数调用,所以我可以捕获每个帧上的帧缓冲区。

http://easyhook.codeplex.com/