使用robotjs捕获并保存图像

602 阅读1分钟
const robot = require("@jitsi/robotjs");
const Jimp = require("jimp");

function screenCaptureToFile(robotScreenPic, path) {
	return new Promise((resolve, reject) => {
		try {
			const image = new Jimp(robotScreenPic.width, robotScreenPic.height);
			let pos = 0;
			image.scan(0, 0, image.bitmap.width, image.bitmap.height, (x, y, idx) => {
				image.bitmap.data[idx + 2] = robotScreenPic.image.readUInt8(pos++);
				image.bitmap.data[idx + 1] = robotScreenPic.image.readUInt8(pos++);
				image.bitmap.data[idx + 0] = robotScreenPic.image.readUInt8(pos++);
				image.bitmap.data[idx + 3] = robotScreenPic.image.readUInt8(pos++);
			});
			image.write(path, resolve);
		} catch (e) {
			console.error(e);
			reject(e);
		}
	});
}

var pic = robot.screen.capture();
screenCaptureToFile(pic, "./screen.png");

使用robotjs捕获屏幕并保存图像,robotjs捕获的对象为bitmap,需要通过jimp转为png格式保存;图像将以错误的颜色保存,需要screenCaptureToFile函数转化为正常颜色