Winapi 如何快速获取和处理实时屏幕输出

Winapi 如何快速获取和处理实时屏幕输出,winapi,screen-scraping,real-time,Winapi,Screen Scraping,Real Time,我正试图写一个程序来玩一个全屏PC游戏(作为计算机视觉和人工智能的一个实验) 对于这个实验,我假设游戏没有AI玩家的基础API(也没有可用的源),所以我打算处理游戏在屏幕上呈现的视觉信息 游戏在win32系统(我想是direct-X)上以全屏模式运行 目前我正在使用win32函数 #include <windows.h> #include <cvaux.h> class Screen { public: HWND windowHandle;

我正试图写一个程序来玩一个全屏PC游戏(作为计算机视觉和人工智能的一个实验)

对于这个实验,我假设游戏没有AI玩家的基础API(也没有可用的源),所以我打算处理游戏在屏幕上呈现的视觉信息

游戏在win32系统(我想是direct-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;
    }
}
#包括
#包括
类屏幕{
公众:
窗柄;
HDC窗口上下文;
HBITMAP缓冲区;
HDC缓冲上下文;
尺寸;
uchar*字节;
int通道;
屏幕(){
windowHandle=GetDesktopWindow();
windowContext=GetWindowDC(windowHandle);
size=cvSize(GetDeviceCaps(windowContext,HORZRES),GetDeviceCaps(windowContext,VERTRES));
缓冲区=CreateCompatibleBitmap(windowContext,size.width,size.height);
bufferContext=CreateCompatibleDC(windowContext);
选择对象(bufferContext,buffer);
通道=4;
字节=新uchar[size.width*size.height*通道];
}
~Screen(){
ReleaseDC(windowHandle,windowContext);
DeleteDC(bufferContext);
删除对象(缓冲区);
删除[]字节;
}
无效捕获屏幕(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(缓冲区,n*通道,字节);
uchar*src=字节;
uchar*dest=(uchar*)img->imageData;
uchar*end=dest+n*img通道;
while(dest

使用此方法处理帧的速度太慢。是否有更好的方法获取屏幕帧?

作为一种常规方法,我将挂接缓冲区翻转函数调用,以便在每一帧上捕获帧缓冲区


作为一种通用方法,我将钩住缓冲区翻转函数调用,以便在每一帧上捕获帧缓冲区


可能想在元标记中添加3d或游戏我认为潜在问题比我的特定应用程序(碰巧是等距RTS游戏)更普遍可能想在元标记中添加3d或游戏我认为潜在问题比我的特定应用程序(碰巧是等距RTS游戏)更普遍