desktop-capture-js
v1.0.1
Published
A Native desktop capture library for Windows that uses NAPI and DirectX libaries to capture desktop real fast
Readme
Desktop-capture-js
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.
Table of Contents
Installation
Ensure you have Node.js installed. Then, install the package via npm:
npm install desktop-capture-jsUsage
First, require the library in your project:
const { captureFrameAsBuffer, captureFrameAsJpeg } = require('desktop-capture-js');Capturing a Frame as Buffer
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);
}Capturing a Frame as JPEG
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);
});API Reference
captureFrameAsBuffer()
Captures the current desktop frame as a raw buffer.
Returns:
- An object containing:
status(number):1for success,0for 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.
captureFrameAsJpeg(quality)
Captures the current desktop frame and converts it to a JPEG image.
Parameters:
quality(number, optional): JPEG quality (1-100). Defaults to80.
Returns:
- A
Promisethat resolves to an object containing:status(number):1for success,0for 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.
Examples
Save Captured Frame as JPEG
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);
});Stream Desktop Frames
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