首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用robotjs捕获和保存映像

使用robotjs捕获和保存映像
EN

Stack Overflow用户
提问于 2017-01-30 16:47:40
回答 2查看 4.5K关注 0票数 2

我尝试用robotjs (http://robotjs.io/)从屏幕上捕获和保存图像,但是当我打开文件位图时,图像不是有效的格式。这是我的密码:

代码语言:javascript
运行
复制
var robot = require("robotjs");
var fs = require("fs");
var size = 10;
var img = robot.screen.capture(0, 0, size, size);
fs.writeFileSync('img.bmp',img.image);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-10 17:14:08

请注意,来自罗博兹罗博兹缓冲区是带有像素的原始缓冲区,而不是BMP或PNG或任何其他格式。

您应该进行一些数据转换,并可能使用支持写入文件的库来保存它(我在Robotjs中没有看到这种情况)。

请查看这个其他问题,它也使用robot.screen.capture并使用Jimp库将文件保存到PNG文件中。这段代码也回答了你的问题。

票数 4
EN

Stack Overflow用户

发布于 2020-11-25 05:45:57

Jimp支持将原始像素缓冲器转换成PNG,而且速度更快。

代码语言:javascript
运行
复制
let robot = require("robotjs");
let Jimp = require('jimp');

const img = robot.screen.capture(0, 0, width, height).image;
new Jimp({data: img, width, height}, (err, image) => {
       image.write(fileName);
});

图像将以错误的颜色保存。要修复它,可以使用以下代码:

代码语言:javascript
运行
复制
function screenCaptureToFile2(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();
screenCaptureToFile2(pic)
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41941151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档