简体   繁体   English

我如何获得未更改的 RobotJS 屏幕截图并将其保存到文件中?

[英]How would I get an unaltered RobotJS screenshot and save it to a file?

I have a program that takes a robotjs screen capture and saves it to a file, like a 1 fps live stream.我有一个程序可以获取robotjs屏幕截图并将其保存到文件中,例如 1 fps live stream。

const Jimp=require("jimp");
const robot=require("robotjs");
const fs=require("fs");
setInterval(function(){
    var img=robot.screen.capture();
    new Jimp({
        "data":img.image,
        "width":img.width,
        "height":img.height
    },function(err,image){
        if(err){
            fs.writeFile(__dirname+"/data/screen.png","",function(){});
        }else{
            image.write(__dirname+"/data/screen.png");
        }
    });
},1000);

At the current moment, every second it:目前,每一秒它:

  1. Takes a screen capture进行屏幕截图
  2. Creates a Jimp instance from that从中创建一个 Jimp 实例
  3. Writes it to screen.png将其写入screen.png

I can't think of any obvious reason for why this would cause some colors to be inverted when I take a screen capture.我想不出任何明显的原因来说明为什么这会导致某些 colors 在我进行屏幕截图时被反转。

For example, when I look at the image after visiting Stack Overflow, the Stack Overflow icon looks blue instead of the usual orange, and after waiting a second (so that it can take a screenshot of me looking at the screenshot), in the screenshot in the screenshot, the colors are back to normal - the original orange SO logo.例如,当我在访问 Stack Overflow 后查看图像时,Stack Overflow 图标看起来是蓝色而不是通常的橙色,等了一秒钟后(这样它就可以截取我正在看的屏幕截图),在屏幕截图中在屏幕截图中,colors 恢复正常 - 原来的橙色 SO 标志。 The screenshot in the screenshot in the screenshot is blue again, and so on.截图中的截图又是蓝色的,依此类推。 An important thing to note is that not all colors are inversed - the logo is, but the white background isn't.需要注意的重要一点是,并非所有 colors 都是倒置的 - 徽标是,但白色背景不是。

I run this file ( app.js ) with node app.js .我用node app.js运行这个文件 ( app.js )。 One thing I tried is to create a Jimp instance from the robotjs screenshot, and then create a Jimp instance from the Jimp instance, so that hopefully the colors would get reversed back, but it looks the same.我尝试过的一件事是从 robotjs 屏幕截图创建一个 Jimp 实例,然后从 Jimp 实例创建一个 Jimp 实例,这样 colors 就会倒过来,但它看起来是一样的。 I am guessing that the problem has something to do with the robotjs image data, but I can't be sure.我猜测问题与 robotjs 图像数据有关,但我不能确定。

How would I get an unaltered screenshot and save it to a file?我如何获得未更改的屏幕截图并将其保存到文件中?

Edit:编辑:

Here's an example:这是一个例子:

Normal (not inverted) colors:正常(未反转)colors:

正常(未反转)颜色

Inverted colors:反转colors:

反转颜色

The buffer from robotjs is in the format BGRA (Blue, Green, Red, Alpha/Opacity).来自 robotjs 的缓冲区采用 BGRA(蓝色、绿色、红色、Alpha/不透明度)格式。 Before, it took about 100 to 150 ms to take a screenshot and save, and now it takes 250 to 300 ms after converting to RGBA (which Jimp uses).以前,截屏并保存大约需要 100 到 150 毫秒,现在转换为 RGBA(Jimp 使用的)后需要 250 到 300 毫秒。 It got 150 ms slower, but it works.它慢了 150 毫秒,但它可以工作。 Here is the full code:这是完整的代码:

const Jimp=require("jimp");
const robot=require("robotjs");
const fs=require("fs");
setInterval(function(){
    var img=robot.screen.capture();
    var data=[];
    var bitmap=img.image;
    var i=0,l=bitmap.length;
    for(i=0;i<l;i+=4){
        data.push(bitmap[i+2],bitmap[i+1],bitmap[i],bitmap[i+3]);
    }
    new Jimp({
        "data":new Uint8Array(data),
        "width":img.width,
        "height":img.height
    },function(err,image){
        if(err){
            fs.writeFile(__dirname+"/data/screen.png","",function(){});
        }else{
            image.write(__dirname+"/data/screen.png");
        }
    });
},1000);

Side note: this is optimized code.旁注:这是优化的代码。 Before, I used a piece of code which made a copy of img.image into data and then used a forEach to loop over every element and make necessary changes on the way.之前,我使用一段代码将img.image复制到data中,然后使用forEach遍历每个元素并在途中进行必要的更改。 That was very slow , at around 800 ms to execute.这非常慢,大约需要 800 毫秒才能执行。 If you know of any faster way, please comment, or better, edit the answer.如果您知道任何更快的方法,请发表评论,或者更好地编辑答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM