8

Is there such a way to capture desktop with node.js not a browser tab?

I have searched a lot but I didn't find any.

What I want is to use node.js to build desktop application.

Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57
user12cdhbw7
  • 115
  • 1
  • 1
  • 7
  • 1
    I know that node specialized for web but also it could be used to build console applications – user12cdhbw7 Dec 14 '13 at 22:47
  • Yes but console applications have nothing to with the GUI, you wont be able to create proper GUI desktop applications with a serviceside programming language. – Roger Far Dec 14 '13 at 22:48
  • 1
    I'm not looking for a GUI,Desktop applications can have a GUI or can't, so console is enough – user12cdhbw7 Dec 14 '13 at 22:51
  • If you're on Windows, you could use this: https://stackoverflow.com/a/69063244/2441655 – Venryx Sep 05 '21 at 12:05

5 Answers5

8

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.

vodolaz095
  • 6,680
  • 4
  • 27
  • 42
  • Please show how to do real-time fast approach and make it available for VLC or Google chrome to re-capture. –  Apr 13 '16 at 10:44
  • 2
    it can be quite untrivial - it will require ffmpeg for generating real time feed and icecast server for broadcasting - better to ask it in separate question – vodolaz095 Apr 13 '16 at 16:04
2

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
});
Pedro L.
  • 7,376
  • 3
  • 25
  • 27
  • how to save screenshot.png – user12cdhbw7 Dec 14 '13 at 23:32
  • Do you use linux? Do you have imagemagick installed? If not, change "import" by your favourite program to take screenshots. – Pedro L. Dec 14 '13 at 23:40
  • 1
    You can install imagemagick for windows. http://www.imagemagick.org/script/binary-releases.php#windows Then you can use the import command. Maybe the command parameters are different so read the documentation. – Pedro L. Dec 14 '13 at 23:48
  • I have monitor 1, monitor 2 connected. when i use to take screenshot how can i tell take screenshot of monitor 1 and 2 or take from monitor 2 only or monitor 1 only? –  Sep 26 '16 at 04:56
2

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");
});
0

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

jayadevkv
  • 384
  • 3
  • 16
-1

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).

Community
  • 1
  • 1
vcc2gnd
  • 137
  • 1
  • 5