node-screenshots 是一个基于XCap的原生的 node.js 截图库,支持 Mac、Windows 和 Linux 系统,且无需任何依赖。 支持截图与视频录制(待实现)。
English | 简体中文
| 操作系统 | node16 | node18 | node20 |
|---|---|---|---|
| Operating System | node16 | node18 | node20 |
| ----------------- | ------ | ------ | ------ |
| Windows x64 | ✓ | ✓ | ✓ |
| Windows x32 | ✓ | ✓ | ✓ |
| Windows arm64 | ✓ | ✓ | ✓ |
| macOS x64 | ✓ | ✓ | ✓ |
| macOS arm64 | ✓ | ✓ | ✓ |
| Linux x64 gnu | ✓ | ✓ | ✓ |
| Linux x64 musl | ✓ | ✓ | ✓ |
| Linux arm64 gnu | ✓ | ✓ | ✓ |
| Linux loongarch64 | ✓ | ✓ | ✓ |
const fs = require('fs')
const { Monitor } = require('node-screenshots')
let monitor = Monitor.fromPoint(100, 100)
console.log(monitor, monitor.id())
let image = monitor.captureImageSync()
fs.writeFileSync(`${monitor.id()}-sync.png`, image.toPngSync())
monitor.captureImage().then((data) => {
console.log(data)
fs.writeFileSync(`${monitor.id()}.jpeg`, data.toJpegSync())
})
const monitors = Monitor.all()
monitors.forEach((item) => {
console.log(
'Monitor:',
item.id(),
item.name(),
[item.x(), item.y(), item.width(), item.height()],
item.rotation(),
item.scaleFactor(),
item.frequency(),
item.isPrimary(),
)
})const fs = require('fs')
const { Window } = require('node-screenshots')
let windows = Window.all()
windows.forEach((item) => {
console.log({
id: item.id(),
x: item.x(),
y: item.y(),
y: item.z(),
width: item.width(),
height: item.height(),
rotation: item.rotation(),
scaleFactor: item.scaleFactor(),
isPrimary: item.isPrimary(),
})
let image = item.captureImageSync()
fs.writeFileSync(`${item.id()}-sync.bmp`, image.toBmpSync())
item.captureImage().then(async (data) => {
console.log(data)
let newImage = await data.crop(10, 10, 10, 10)
fs.writeFileSync(`${item.id()}.png`, await newImage.toPng())
})
})完整的 TypeScript 类型定义:index.d.ts
static all(): Array<Monitor>:获取所有监视器static fromPoint(x: number, y: number): Monitor | null:根据指定的坐标获取监视器captureImageSync(): Image:同步捕获图像captureImage(): Promise<Image>:异步捕获图像
static all(): Array<Window>:获取所有窗口captureImageSync(): Image:同步捕获图像captureImage(): Promise<Image>:异步捕获图像
cropSync(x: number, y: number, width: number, height: number): Image:同步裁剪图像crop(x: number, y: number, width: number, height: number): Promise<Image>:异步裁剪图像toPngSync(copyOutputData?: boolean | undefined | null): Buffer:同步转换为 PNG 格式toPng(copyOutputData?: boolean | undefined | null): Promise<Buffer>:异步转换为 PNG 格式toJpegSync(copyOutputData?: boolean | undefined | null): Buffer:同步转换为 JPEG 格式toJpeg(copyOutputData?: boolean | undefined | null): Promise<Buffer>:异步转换为 JPEG 格式toBmpSync(copyOutputData?: boolean | undefined | null): Buffer:同步转换为 BMP 格式toBmp(copyOutputData?: boolean | undefined | null): Promise<Buffer>:异步转换为 BMP 格式toRawSync(copyOutputData?: boolean | undefined | null): Buffer:同步转换为原始格式(RGBA 数据)toRaw(copyOutputData?: boolean | undefined | null): Promise<Buffer>:异步转换为原始格式(RGBA 数据)
copyOutputData: electron 中传递传递相关参数,否则 electron 会崩溃,nodejs 不传或者传递 false,性能会更好,详细信息参考 napi-rs/napi-rs#1346
在 Linux 上,你需要安装 libxcb、libxrandr 和 dbus。
Debian / Ubuntu:
apt-get install libxcb1 libxrandr2 libdbus-1-3Alpine:
apk add libxcb libxrandr dbus- xcap - XCap 是一个使用 Rust 编写的跨平台的屏幕捕获库