A native desktop capture library for Node.js that leverages N-API and DirectX libraries to capture desktop on Windows rapidly and efficiently. Ideal for applications requiring real-time screen capturing, such as screenshot taking, streaming, recording, or automated testing tools.
Ensure you have Node.js installed. Then, install the package via npm:
npm install desktop-capture-js
First, require the library in your project:
const { captureFrameAsBuffer, captureFrameAsJpeg } = require('desktop-capture-js');
Capture the current desktop frame as a raw buffer along with its dimensions.
const result = captureFrameAsBuffer();
if (result.status === 1) {
const frameBuffer = result.message;
const width = result.width;
const height = result.height;
// Process the frame buffer as needed
} else {
console.error('Capture failed:', result.message);
}
Capture the current desktop frame and convert it to a JPEG image. You can specify the quality (default is 80).
captureFrameAsJpeg(90)
.then(result => {
if (result.status === 1) {
const jpegBuffer = result.message;
const width = result.width;
const height = result.height;
// Save or process the JPEG buffer as needed
} else {
console.error('No new frame available');
}
})
.catch(error => {
console.error('Capture failed:', error.message);
});
Captures the current desktop frame as a raw buffer.
Returns:
- An object containing:
-
status
(number
):1
for success,0
for failure. -
message
(Buffer
|string
): The frame data buffer on success, or an error message. -
width
(number
): Width of the captured frame. -
height
(number
): Height of the captured frame.
-
Captures the current desktop frame and converts it to a JPEG image.
Parameters:
-
quality
(number
, optional): JPEG quality (1-100). Defaults to80
.
Returns:
- A
Promise
that resolves to an object containing:-
status
(number
):1
for success,0
for failure. -
message
(Buffer
|string
): The JPEG data buffer on success, or an error message. -
width
(number
): Width of the captured frame. -
height
(number
): Height of the captured frame.
-
const fs = require('fs');
const { captureFrameAsJpeg } = require('desktop-capture-js');
captureFrameAsJpeg(85)
.then(result => {
if (result.status === 1) {
fs.writeFileSync('screenshot.jpg', result.message);
console.log(`Screenshot saved: ${result.width}x${result.height}`);
} else {
console.error('No new frame available');
}
})
.catch(error => {
console.error('Error capturing frame:', error.message);
});
const { captureFrameAsBuffer } = require('desktop-capture-js');
setInterval(() => {
const result = captureFrameAsBuffer();
if (result.status === 1) {
// Stream the frame buffer to a server or process it
console.log(`Captured frame: ${result.width}x${result.height}`);
} else {
console.warn(result.message);
}
}, 17); // Capture in 60 fps