關於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

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