How to capture desktop screen of computer host where it's running on using node.js

泄露秘密 提交于 2019-12-04 03:11:05

You can use

http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

and

https://en.wikipedia.org/wiki/Scrot

to make screenshot of screen of current user running nodejs application. Something like this (it is complete expressJS example):

var express = require('express'),
  childProcess = require('child_process'),
  app = express();

app.get('/screenshot.png', function(request,response){
  childProcess.exec('scrot screenshot.png', function(err){
    if(err1) {
      response.send(503,'Error creating image!');
    } else {
       response.sendfile('screenshot.png')
    }
  });
});
app.listen(3000);

But this is quite slow approach.

Why don't you just call an external program?

For example, you could call import:

$ import -window root screenshot.png

The code:

var exec = require('child_process').exec;
exec('import -window root screenshot.png', function (error, stdout, stderr){
    // now you have the screenshot
});

Full Resolution

var screenshot = require('desktop-screenshot');

screenshot("screenshot.png", function(error, complete) {
    if(error)
        console.log("Screenshot failed", error);
    else
        console.log("Screenshot succeeded");
});

Resize to 400px wide, maintain aspect ratio

var screenshot = require('desktop-screenshot');

screenshot("screenshot.png", {width: 400}, function(error, complete) {
    if(error)
        console.log("Screenshot failed", error);
    else
        console.log("Screenshot succeeded");
});

Resize to 400x300, set JPG quality to 60%

var screenshot = require('desktop-screenshot');

screenshot("screenshot.jpg", {width: 400, height: 300, quality: 60}, function(error, complete) {
    if(error)
        console.log("Screenshot failed", error);
    else
        console.log("Screenshot succeeded");
});
vcc2gnd

Perhaps you should rephrase your question as follow:

How to capture desktop screen of computer host where it's running on http client's request and send back the image as response

If that's what you actually mean, you can write your own add-on module using native compiler. For reference, see this post).

For the newcomers ,

There is a npm package called desktop-screenshot for exactly the same purpose.

Here it is

You can find an addon where the screenshot is taken and saved to any user defined path.User can also specify a time interval for the screenshot capture. Here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!