[英]Capture Screen Image in C++ on OSX
Is there a way to programatically take a screenshot (or somehow get access to an image) of the current screen display on a mac? 有没有办法以编程方式获取Mac上当前屏幕显示的屏幕截图(或以某种方式获取图像)?
Preferably, in C++, not Objective-C. 最好在C ++中而不是在Objective-C中。
I have been working on the same problem. 我一直在研究相同的问题。 This is the code I came up with after searching for a while.
这是我搜索了一段时间后想到的代码。
CGImageRef screenShot = CGWindowListCreateImage( CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
//std::string image_name = "/Users/nikhil/Desktop/SC_";
//image_name+=str;
//cout<<str<<endl;
//image_name+=".jpg";
//cout<<image_name<<endl;
CFStringRef file = CFSTR("/Users/nikhil/Desktop/SC.jpg");
CFStringRef type = CFSTR("public.jpeg");
CFURLRef urlRef = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, file, kCFURLPOSIXPathStyle, false );
CGImageDestinationRef image_destination = CGImageDestinationCreateWithURL( urlRef, type, 1, NULL );
CGImageDestinationAddImage( image_destination, screenShot, NULL );
CGImageDestinationFinalize( image_destination );
You have to give a static string to CFSTR(). 您必须给CFSTR()一个静态字符串。 Although I myself am looking for alternative to store the screencapture to a file with timestamp based file name.
尽管我本人正在寻找将屏幕捕获存储到具有基于时间戳的文件名的文件的替代方法。
Use screencapture(1). 使用screencapture(1)。
http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/screencapture.1.html http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/screencapture.1.html
Invoke it with system() or posix_spawn(). 使用system()或posix_spawn()调用它。
我建议使用Quartz Display Services和功能CGDisplayCreateImage()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.