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

标签 cocoa macos core-graphics

我正在寻找一种获取窗口内容的方法,例如 CamTwist“桌面+”功能。这可以抓取任何窗口,即使它位于后台。

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

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

最佳答案

据我所知,官方 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中有示例代码。 .

关于cocoa - 如何在 OSX 中访问 NSWindow 的像素缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5458308/

相关文章:

cocoa - 将 NSProgressIndicators 添加到 NSOutlineView 子类

c - 为什么我在 netstat/ifconfig 中看不到服务器正在运行?

ios - 使用 Core Graphics 绘制圆弧时绘制的额外线条

cocoa - cocoa 中的命名管道

iphone - 分析Cocoa程序的程序布局和存储建议

macos - 无法获取 GoLang oracle 包

objective-c - 检查是否启用语音命令?

swift - updateLayer() 上下文

ios - 混合模式 kCGBlendModeMultiply 没有正确地将图像与颜色相乘

macos - 如何在 Cocoa 中将 2 个按钮居中?