如何在OSX中访问NSWindow的像素缓冲区?

时间:2011-03-28 11:27:39

标签: cocoa macos core-graphics

我正在寻找一种方法来获取像CamTwist'Desktop +'功能这样的窗口内容。这可以抓住任何窗口,即使它在后台。

Apple的OpenGLScreenCapture示例显示了如何从主屏幕捕获而不是从隐藏表面捕获。

知道CamTwist如何访问NSWindow的像素缓冲区吗?

2 个答案:

答案 0 :(得分:5)

AFAIK,官方API可以在CGWindow.h中找到,作为CoreGraphics的一部分:

/* Create an image containing a composite of the specified set of windows
   contained within a rectangular area. The set of windows is specified
   using options from `CGWindowListOption', along with an optional
   additional window ID.

   The windows list options are:

   --- kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly: Use all
   on-screen windows in this user session to construct the image. The
   parameter `windowID' should be `kCGNullWindowID'.

   --- kCGWindowListOptionOnScreenAboveWindow: Use all on-screen windows in
   this user session above the window specified by `windowID', ordered from
   front to back, to construct the image. To include the window specified by
   `windowID', add the flag `kCGWindowListOptionIncludingWindow'.

   --- kCGWindowListOptionOnScreenBelowWindow: Use all on-screen windows in
   this user session below the window specified by `windowID', ordered from
   front to back, to construct the image. To include the window specified by
   `windowID', add the flag `kCGWindowListOptionIncludingWindow'.

   --- kCGWindowListOptionIncludingWindow: Use only the window specified by
   `windowID' to construct the image.

   The parameter `screenBounds' specifies the rectangle in screen space
   (origin at the upper-left; y-value increasing downward). Setting
   `screenBounds' to `CGRectInfinite' will include all the windows on the
   entire desktop. Setting `screenBounds' to `CGRectNull' will use the
   bounding box of the specified windows as the screen space rectangle.

断裂

   /* The parameter `imageOptions' allows you to specify whether the window
   frame ornamentation, such as a shadow or similar effect, should be
   included or excluded in the bounds calculation when `CGRectNull' is
   specified for the window bounds.

   If no windows meet the specified criteria, or the windows can't be read,
   then a transparent black image will be returned.

   Any on-screen window with sharing type `kCGWindowSharingNone' will not
   be included in the image.

   This function returns NULL if the caller is not running within a Quartz
   GUI session or the window server is disabled. */

CG_EXTERN CGImageRef CGWindowListCreateImage(CGRect screenBounds,
  CGWindowListOption listOption, CGWindowID windowID,
  CGWindowImageOption imageOption)
  CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);

/* Create an image containing a composite of the specified set of windows
   contained within a rectangular area à la `CGWindowListCreateImage'. The
   set of windows is specified by `windowArray', an array of window IDs. */

CG_EXTERN CGImageRef CGWindowListCreateImageFromArray(CGRect screenBounds,
  CFArrayRef windowArray, CGWindowImageOption imageOption)
  CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_NA);

抱歉,找不到Apple网站上文档的链接。但是,它们似乎在Son of Grab中有示例代码。

答案 1 :(得分:4)

AFAIK使用官方API无法做到这一点。当然这是可能的,否则Apple将如何实施Exposé呢?人们通过逆向工程Apple的代码创建的Here you can find a header;使用它需要您自担风险(作为私有API,任何这些调用都可能随着Apple的任何发布而改变而且没有任何通知)并且如果您使用它,不要指望Apple会让您进入他们的App Store :-)如您所见,您可以使用此API获取所有应用程序的所有窗口(包括隐藏窗口)的列表,您甚至可以操作它们;虽然你真正允许做什么可能取决于你的应用程序权限。 And here's code显示了如何使用此私有API,捕获您喜欢的任何窗口的内容。请注意,代码的工作方式有所不同,具体取决于操作系统版本,有一种方法可以在10.5之前完成,以及如何在10.5之后执行此操作,因此如果您的目标是旧系统,请确保同时实现这两种方式。用于真正获取图像的最终API调用不是私有的,顺便说一句,它们可以在官方SDK头文件中找到,只有你获得对不属于当前进程的窗口的引用的方式才是私有的。

更新:从10.5开始,Apple公开了从窗口服务器复制WindowID的重要功能;所以它不再是私有API。但是,已经可以在10.5之前检索那些WindowID,但是当时标题尚未公开,并且仍然没有在反向工程标题中找到的所有函数都已公开。