我一直在研究用于录制屏幕的 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/