代码参考:https : //github.com/aredfox/screencapturer
问题描述:这是一个带有“ MainWindow”的电子应用程序,其中包含一个“开始捕获”按钮。一旦单击它,就会向主流程触发一个事件,然后主流程会启动一个新的单独的“ BrowserWindow”对象,称为“ captureWindow”,并将其自己的capture.html和capture.js关联起来。在capture.js中,每三秒钟制作一次屏幕截图,并将其保存到c:\ temp \ screencap(这是一个演示应用程序,用于说明问题,因此,我暂时没有对此进行配置和对路径进行硬编码)。每次在“ craptureWindow”中进行捕获时,它都会冻结,这是我期望的。但是,“ mainWindow”对象也冻结了,这没想到。我应该如何处理,以便在另一个“ BrowserWindow”对象中运行进程时,mainWindow不会冻结?我假设电子BrowserWindows(或“选项卡”)有单独的线程?
编辑20/12/2016,可能的罪魁祸首是desktopCapturer.getSources()。
附录:发现问题一定在的代码块内getMainSource
,因为当我缓存该“源”结果时,它不会冻结整个电子。因此,一定是过滤方法或屏幕本身导致了冻结问题。
function getMainSource(desktopCapturer, screen, done) {
const options = {
types: ['screen'], thumbnailSize: screen.getPrimaryDisplay().workAreaSize
}
desktopCapturer.getSources(options, (err, sources) => {
if (err) return console.log('Cannot capture screen: ', err)
const isMainSource = source => source.name === 'Entire screen' || source.name === 'Screen 1'
done(sources.filter(isMainSource)[0])
})
}
但是,该解决方案并未缓存getMainSource(也称为“源”)的结果,因为每次都会产生相同的图像数据。我通过将文件写入为png的方式进行了验证,即使在桌面上进行了足够的更改,实际上每个屏幕截图都完全相同。TODO:可能的选项是设置视频流并保存该流中的图像?
如果您想跨平台捕获屏幕截图,我建议您使用下面的方法,而不要依赖于内置的electronic-api。并不是说它们不好,但是例如,它们不适合每三秒钟拍摄一次屏幕截图。
对我来说,解决方案是NPM-模块桌面截图-并称为NPM包的危险,因为这需要在Windows和ASAR执行。
我最终实现的代码是这个-它可能是您的问题的灵感/示例的来源。
/* ******************************************************************** */
/* MODULE IMPORTS */
import { remote, nativeImage } from 'electron';
import path from 'path';
import os from 'os';
import { exec } from 'child_process';
import moment from 'moment';
import screenshot from 'desktop-screenshot';
/* */
/*/********************************************************************///
/* ******************************************************************** */
/* CLASS */
export default class ScreenshotTaker {
constructor() {
this.name = "ScreenshotTaker";
}
start(cb) {
const fileName = `cap_${moment().format('YYYYMMDD_HHmmss')}.png`;
const destFolder = global.config.app('capture.screenshots');
const outputPath = path.join(destFolder, fileName);
const platform = os.platform();
if(platform === 'win32') {
this.performWindowsCapture(cb, outputPath);
}
if(platform === 'darwin') {
this.performMacOSCapture(cb, outputPath);
}
if(platform === 'linux') {
this.performLinuxCapture(cb, outputPath);
}
}
performLinuxCapture(cb, outputPath) {
// debian
exec(`import -window root "${outputPath}"`, (error, stdout, stderr) => {
if(error) {
cb(error, null, outputPath);
} else {
cb(null, stdout, outputPath);
}
});
}
performMacOSCapture(cb, outputPath) {
this.performWindowsCapture(cb, outputPath);
}
performWindowsCapture(cb, outputPath) {
require('hazardous');
screenshot(outputPath, (err, complete) => {
if(err) {
cb(err, null, outputPath);
} else {
cb(null, complete, outputPath);
}
});
}
}
/*/********************************************************************///
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句