【问题标题】:How to quickly acquire and process real time screen output如何快速获取和处理实时屏幕输出
【发布时间】:2010-06-07 00:38:59
【问题描述】:

我正在尝试编写一个程序来玩全屏 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;
    }
}

使用这种方法处理帧的速度太慢了。有没有更好的获取屏幕帧的方法?

【问题讨论】:

  • 可能想要将 3d 或游戏添加到您的元标记中
  • 我认为根本问题比我的特定应用程序更普遍(恰好是等距 RTS 游戏)

标签: winapi screen-scraping real-time


【解决方案1】:

作为一种通用方法,我会挂钩缓冲区翻转函数调用,以便捕获每一帧的帧缓冲区。

http://easyhook.codeplex.com/

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 2016-06-30
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2011-02-24
    相关资源
    最近更新 更多