如何使用Node JS或JavaScript拍摄子文件夹内容的屏幕截图

时间:2019-04-17 10:42:37

标签: javascript node.js screenshot filestream

我有一个文件夹,并且其中有子文件夹(或目录)。现在,这些目录包含一些文件或内容。我想要的是我想以编程方式对所有子文件夹重复使用node.js或JavaScript文件系统api来获取所有子文件夹内容的屏幕快照。那么我该如何实现呢?

下面是我尝试过但无法达到预期结果的代码。我只得到当前桌面屏幕截图,该屏幕处于活动状态(我目前正在该屏幕上工作),这是不希望的。因此,需要您的建议或解决方案。预先感谢您的帮助。

//specify the interval time in seconds 
var interval = 1;

//specify the path to which screenshots are to be stored
var imgPath = 'screenshots';

//specify any prefix for the image if needed, preferably with a trailing underscore.Else leave it blank
var imgPrefix = '';

//specify interval in seconds to clear the screenshots folder.Leave it blank if no need for reset
var clearInterval = '';

var fs  = require('fs');

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

const scriptsPath = './folders';
const process = require('process');
const path = require('path');

// Returns Top Level Folders List within a given folder
function getFolders(dir) {
  return fs.readdirSync(dir)
    .filter(function(file) {
      return fs.statSync(path.join(dir, file)).isDirectory();
    });
}

function getFolderListWithScreenshot() {

  var count = 0;

  // Get folders list with names
  var folders = getFolders(scriptsPath);
  if (folders.length === 0) return done(); // nothing to do!

  return folders.map(function(folder) {


    const directoryPath = path.join(__dirname + "/" + scriptsPath , folder, './');

    const realFolderPath = path.dirname(directoryPath).split(path.sep).pop();

    console.log(directoryPath);

    console.log(realFolderPath);

    process.chdir(directoryPath);

    // Printing Current Working Directory
    console.log('Current Working directory: ' + process.cwd());

    console.log(process.cwd()+": done");

    count++;

    screenshot(realFolderPath+" ("+count+").jpg", {width: 1440, height: 900, quality: 60}, function(error, complete) {
        if(error) {
            console.log("Screenshot failed", error);
        }
        else {
            console.log("Screenshot succeeded");
        }
    });

  });
}

getFolderListWithScreenshot();

0 个答案:

没有答案