Skip to content

捕获 API

dcc_mcp_core (capture 模块)

使用平台特定后端的 DCC 应用程序屏幕捕获。

Capturer

高级捕获器包装器,自动选择后端。

构造函数

python
from dcc_mcp_core import Capturer

# 自动选择最佳后端
capturer = Capturer.new_auto()

# 单窗口捕获
capturer = Capturer.new_window_auto()

# 无头环境 / CI 测试
capturer = Capturer.new_mock(width=1920, height=1080)
print(f"Backend: {capturer.backend_name()}")
count, total_bytes, errors = capturer.stats()

静态方法

方法返回描述
new_auto()Capturer使用最佳可用后端创建捕获器(Windows: DXGI, Linux: X11, 其他: Mock)
new_window_auto()Capturer创建配置为单窗口捕获的捕获器(Windows: HWND PrintWindow;其他平台回退到 Mock)
new_mock(width=1920, height=1080)Capturer创建使用 Mock 合成后端的捕获器(用于 CI/无头环境)

实例方法

方法返回描述
capture(format="png", jpeg_quality=85, scale=1.0, timeout_ms=5000, process_id=None, window_title=None)CaptureFrame捕获一帧(全屏,或当设置 process_id/window_title 时捕获匹配窗口)
capture_window(*, process_id=None, window_handle=None, window_title=None, format="png", jpeg_quality=85, scale=1.0, timeout_ms=5000, include_decorations=True)CaptureFrame捕获单个窗口。必须提供 process_id/window_handle/window_title 中的至少一个
backend_name()str当前后端名称(如 "DXGI Desktop Duplication""HWND PrintWindow"
backend_kind()CaptureBackendKind当前后端的枚举形式
stats()tuple[int, int, int]运行统计:(capture_count, total_bytes, error_count)

CaptureFrame

python
frame = capturer.capture(format="png")
print(frame.width, frame.height)  # 帧尺寸
print(frame.format)               # 格式字符串: "png"、"jpeg" 或 "raw_bgra"
print(frame.mime_type)            # MIME 类型,例如 "image/png"
print(frame.byte_len())           # 编码数据的字节长度
print(frame.data)                 # 编码图像字节

CaptureFrame 属性

属性类型描述
widthint帧宽度(像素)
heightint帧高度(像素)
databytes编码图像字节(PNG、JPEG)或原始 BGRA32 数据
formatstr格式字符串:"png""jpeg""raw_bgra"
mime_typestr编码字节的 MIME 类型(例如 "image/png"
timestamp_msint捕获时的 Unix 纪元毫秒时间戳
dpi_scalefloat显示缩放因子(1.0 标准,2.0 HiDPI)

CaptureFrame 方法

方法返回描述
byte_len()int编码图像数据的字节长度

捕获格式

格式描述
pngPNG 图片格式(无损,较大)
jpeg / jpgJPEG 图片格式(有损,较小)
raw_bgra原始 BGRA32 字节(无编码)

捕获参数

参数类型默认值描述
formatstr"png"输出格式
jpeg_qualityint85JPEG 质量 (1-100)
scalefloat1.0缩放因子
timeout_msint5000捕获超时
process_idintNone捕获特定进程
window_titlestrNone捕获特定窗口

后端

后端平台描述
dxgiWindowsDXGI Desktop Duplication API
x11LinuxX11 XShmGetImage
mock所有平台用于测试的合成棋盘格

后端选择通过 new_auto() 自动进行。

错误处理

捕获错误以 RuntimeError 抛出:

python
try:
    frame = capturer.capture(timeout_ms=1000)
except RuntimeError as e:
    print(f"捕获失败: {e}")

平台说明

Windows

DXGI 后端要求:

  • Windows 8 或更高版本
  • DirectX 11 兼容 GPU
  • 桌面复制支持

Linux

X11 后端要求:

  • X11 显示服务器
  • 对 X 服务器的读取权限

macOS

macOS 使用 Mock 后端进行测试。生产捕获需要平台特定实现。

Released under the MIT License.