类: ScreenCapturer
2022/10/22约 726 字大约 2 分钟
media_projection.ScreenCapturer
屏幕截图捕获器,用于获取屏幕截图。通过 requestScreenCapture() 函数创建。
继承关系
EventEmitter↳
ScreenCapturer
ScreenCapturer 继承自 EventEmitter,因此可以使用所有 EventEmitter 的方法来监听事件。
目录
方法
Events
Events
image_available
▸ on(event, listener): this
有新的截图时触发的事件。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
capturer.on("image_available", () => {
console.log("新的截图已可用");
const img = capturer.latestImageOrNull();
if (img) {
console.log("截图尺寸:", img.width, "x", img.height);
}
});
// 保持运行
$autojs.keepRunning();
}
main();参数
| 名称 | 类型 | 描述 |
|---|---|---|
event | "image_available" | 事件名称,固定为 "image_available" |
listener | () => void | 监听器函数,当有新的截图可用时被调用 |
返回值
this
返回 ScreenCapturer 对象本身,支持链式调用。
方法
latestImage
▸ latestImage(): Image
获取当前最新的截图图片对象。如果当前没有截图,则会抛出 ImageUnavailableError。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
// 等待第一张截图
await capturer.awaitForImageAvailable();
// 获取最新截图
const img = capturer.latestImage();
console.log("截图尺寸:", img.width, "x", img.height);
capturer.stop();
}
main();返回值
最新的截图图片对象。
抛出
如果当前没有可用的截图,则抛出 ImageUnavailableError。
latestImageOrNull
▸ latestImageOrNull(): Image | null
获取当前最新的截图图片对象。如果当前没有截图,则会返回 null。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
// 尝试获取最新截图(可能为 null)
const img = capturer.latestImageOrNull();
if (img) {
console.log("截图尺寸:", img.width, "x", img.height);
} else {
console.log("当前没有可用的截图");
}
capturer.stop();
}
main();返回值
Image | null
最新的截图图片对象,如果没有则返回 null。
nextImage
▸ nextImage(): Promise<Image>
等待下一张截图并返回。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
// 等待并获取下一张截图
const img = await capturer.nextImage();
console.log("截图尺寸:", img.width, "x", img.height);
// 获取下一张截图
const img2 = await capturer.nextImage();
console.log("第二张截图尺寸:", img2.width, "x", img2.height);
capturer.stop();
}
main();返回值
Promise<Image>
返回一个 Promise,resolve 时返回下一张截图图片对象。
awaitForImageAvailable
▸ awaitForImageAvailable(): Promise<void>
等待有截图可用。仅在刚申请到截图权限,未有任何截图可用时,会等待有第一张截图到来。在第一张截图到来的任何时刻调用,会立即返回。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
// 等待第一张截图可用
await capturer.awaitForImageAvailable();
console.log("第一张截图已可用");
// 现在可以安全地获取截图
const img = capturer.latestImage();
console.log("截图尺寸:", img.width, "x", img.height);
capturer.stop();
}
main();返回值
Promise<void>
返回一个 Promise,当第一张截图可用时 resolve。
stop
▸ stop(): void
停止截图。
示例
"nodejs";
const { requestScreenCapture } = require("media_projection");
async function main() {
const capturer = await requestScreenCapture();
// 获取几张截图
for (let i = 0; i < 5; i++) {
const img = await capturer.nextImage();
console.log(`第 ${i + 1} 张截图`);
}
// 停止截图
capturer.stop();
console.log("截图已停止");
}
main();返回值
void
