c++ - 在 OSX 中使用 CGDisplayStream 存储屏幕记录的特定文件路径

标签 c++ macos core-graphics screen-capture screen-recording

我一直在研究用于录制屏幕的 C++ 命令行工具。经过一番搜索后,我想出了以下代码。当我编译和运行代码时,看起来屏幕正在被记录。我正在寻找可以提供要存储屏幕记录的特定文件路径的功能。另外我想附加时间戳和文件名。如果有人对此问题有更好的方法或方法,请在此处提出建议。任何线索表示赞赏。谢谢

#include <ApplicationServices/ApplicationServices.h>

int main(int argc, const char * argv[]) {
    // insert code here...

    CGRect mainMonitor = CGDisplayBounds(CGMainDisplayID());
    CGFloat monitorHeight = CGRectGetHeight(mainMonitor);
    CGFloat monitorWidth = CGRectGetWidth(mainMonitor);
    const void *keys[1] = { kCGDisplayStreamSourceRect };
    const void *values[1] = { CGRectCreateDictionaryRepresentation(CGRectMake(0, 0, 100, 100)) };

    CFDictionaryRef properties = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

    CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), monitorWidth, monitorHeight, '420f' , properties,  ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef){});

    CGDirectDisplayID displayID = CGMainDisplayID();
    CGImageRef image_create = CGDisplayCreateImage(displayID);

    CFRunLoopSourceRef runLoop = CGDisplayStreamGetRunLoopSource(stream);

   // CFRunLoopAddSource(<#CFRunLoopRef rl#>, runLoop, <#CFRunLoopMode mode#>);

    CGError err = CGDisplayStreamStart(stream);
    if (err == CGDisplayNoErr) {
        std::cout<<"WORKING"<<std::endl;
        sleep(5);
    } else {
        std::cout<<"Error: "<<err<<std::endl;
    }

    //std::cout << "Hello, World!\n";
    return 0;
}

最佳答案

您应该在 CGDisplayStreamCreate 中提供的回调中执行此操作。您可以通过 IOSurfaceGetBaseAddress 访问像素(请参阅其他 IOSurface 函数)。如果你不想自己玩弄像素,你可以用 IOSurface 中的 CVPixelBufferCreateWithBytes 创建一个 CVPixelBuffer,然后用 CIImage 创建一个[CIImage imageWithCVImageBuffer] 并将其保存到文件中,如 here 所示。

关于c++ - 在 OSX 中使用 CGDisplayStream 存储屏幕记录的特定文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49810280/

相关文章:

c++ - 从 WIC 图像 C++ 获取 RGB

objective-c - NSTableView 中的鼠标悬停

c++ - 是否有预处理器或其他东西来控制 Visual Studio 包含的标题?

c++ - 实现八叉树

c++ - 带有 unsigned char 的 for 循环给出了意想不到的行为

multithreading - 如何在多核上编译 OpenCL 程序?

macos - 在 Mac OS X 上安装/升级 gradle

ios - 可缩放图形

objective-c - 在 NSView 的 OSX 上使用 Core Graphics 绘制车速表

ios - 如何绘制三角形的UIButton