关于electron的2种截图方式详解

 

第一种方式:

1、采用第三方现有截图工具,添加dll和exe文件

 

2、引入需要的模块

// 快捷键模块

const { globalShortcut } = require('electron');

// 加载子进程

const { execFile } = require('child_process');

const { ipcRenderer } = require('electron');

const { ipcRenderer, remote } = require('electron');

 3、实现方法

     let url = path.join(packagejson.appConfig.pdfPath,'PrScrn.exe');
     console.log(url);
     execFile(url);

第二种方式:

1、采用前端框架kscreenshot,安装

npm: npm install kscreenshot --save

2、映入模块:import kscreenshot from 'kscreenshot' ,实现代码

  new kscreenshot(  {
        key: 65 ,
        copyPath:function(base64){
        base64=base64.replace('data:image/png;base64,', '');
        var imgData = new Buffer(base64, 'base64');//Buffer编码
        fs.writeFile(path.resolve(packagejson.appConfig.pdfPath + 'sreen.png'), imgData, function (err) {

        if (err)
        SmartFile.log(err);

        });
        return  path.resolve(packagejson.appConfig.pdfPath + 'sreen.png');
        }
        
    }).startScreenShot();

原理就是插件截取返回的是base64编码,我们需将编码转成流写下来,将文件路径返回就是粘贴的图片。

5、git插件源码下载地址:https://github.com/kejiacheng/kscreenshot

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章