我一直在研究用于记录屏幕的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函数)。如果你不想做自己的像素,你可以用CVPixelBuffer
创建一个CVPixelBufferCreateWithBytes IOSurface
然后用CIImage
创建一个[CIImage imageWithCVImageBuffer]并将其保存到文件中,如here所示。