electron實現截圖功能(window+mac同時實現),以及快捷鍵應用
阿新 • • 發佈:2020-10-20
本片文章中的截圖方式 :
window:應用qq截圖,截圖方式與qq無異,截完圖之後可編輯操作;
mac: 呼叫系統截圖
qq截圖工具地址:https://github.com/17326953721/qqScreenshot.git
截圖:
1.window截圖:
import{execFile}from"child_process"; const isDevelopment = process.env.NODE_ENV !== "development";constpath=require("path");
let url = path.resolve(__dirname, "./../static/qq/PrintScr.exe");
if (isDevelopment && !process.env.IS_TEST) { // 生產環境 url = path.join(__dirname, "../../extraResources/PrintScr.exe"); } let screen_window = execFile(url); screen_window.on("exit", (code) => { if (code) { //截圖完成,已經在貼上板中 } });
上面的生產環境的路徑需要在package.json中進行配置,不然的話打包完成之後找不到檔案地址,導致截圖失敗,配置如下:
"extraResources": [ { "from": "./static/qq/PrintScr.exe", "to": "./extraResources/PrintScr.exe" }, { "from": "./static/qq/PrScrn.dll", "to": "./extraResources/PrScrn.dll" } ],
2.mac截圖:
const child_process = require("child_process"); child_process.exec(`screencapture -i -c`, (error, stdout, stderr) => { console.log("308", error); if (!error) { //截圖完成,在貼上板中 } });
screencapture 是mac中的命令,可以通過終端輸入進行截圖,字尾命令:
-c 強制截圖儲存到剪貼簿而不是檔案中 -C 截圖時保留游標(只在非互動模式下有效) -d display errors to the user graphically(不知道啥意思) -i 互動模式擷取螢幕。可以是選區或者是視窗。按下空格可切換截圖模式 -m 只擷取主顯示器(-i模式下無效) -M 截圖完畢後,會開啟郵件客戶端,圖片就躺在郵件正文中 -o 在視窗模式下,不擷取視窗的陰影 -P 截圖完畢後,在圖片預覽中開啟 -s 只允許滑鼠選擇模式 -S 視窗模式下,擷取螢幕而不是視窗 -t png 指定圖片格式,模式是png。可選的有pdf, jpg, tiff等 -T 延時擷取,預設為5秒。 -w 只允許視窗擷取模式 -W 開始互動擷取模式,預設為視窗模式(只是預設模式與-i不同) -x 不播放聲效 -a do not include windows attached to selected windows(不懂) -r 不向圖片中加入dpi資訊 -l<windowid> 抓取指定windowid的視窗截圖 -R<x,y,w,h> 抓取指定區域的截圖 -B<bundleid> 截圖輸出會被bundleid指出的程式開啟 -U 開啟截圖操作版 //我這邊使用-U,會報錯
到此截圖已完成,因為截下來的圖片,是二進位制資料,需要轉成圖片格式,從剪貼簿中解析圖片,如下:
import { clipboard } from "electron"; //呼叫electron方法獲取剪貼簿內容 clipboardParsing() { let pngs = clipboard.readImage().toPNG(); //可改變圖片格式,如:toJPEG let imgData = new Buffer(pngs, "beas64"); let imgs = "data:image/png;base64," + btoa(new Uint8Array(imgData).reduce( (data, byte) => data + String.fromCharCode(byte), "") ); let mytextarea = document.getElementById("mytextarea"); let screenshotImg = document.createElement("img"); //imgs 為base64格式 screenshotImg.src = imgs; screenshotImg.style.maxHeight = "70px"; screenshotImg.style.maxWidth = "200px"; mytextarea.appendChild(screenshotImg); },
所有程式碼:
import{execFile}from"child_process"; const isDevelopment = process.env.NODE_ENV !== "development"; constpath=require("path"); const child_process = require("child_process");execFileWay() { if (process.platform == "darwin") { //判斷當前作業系統,"darwin" 是mac系統 "win32" 是window系統 child_process.exec(`screencapture -i -c`,(error, stdout, stderr) => { if (!error) { this.clipboardParsing(); } }); } else { let url = path.resolve(__dirname, "../../../../../static/qq/PrintScr.exe"); if (isDevelopment && !process.env.IS_TEST) { // 生產環境 url = path.join(__dirname, "../../../../extraResources/PrintScr.exe" ); } let screen_window = execFile(url); screen_window.on("exit", (code) => { if (code) { this.clipboardParsing(); } }); } }
快捷鍵(window)
import { app, Menu, BrowserWindow, ipcMain,Tray, BrowserView, globalShortcut } from 'electron' import { execFile } from 'child_process'; const isDevelopment = process.env.NODE_ENV !== "development"; const path = require('path'); //截圖快捷鍵 export const screenshots = () => { return globalShortcut.register('CommandOrControl+Shift+L', () => { let url = path.resolve(__dirname, "../../static/qq/PrintScr.exe"); if (isDevelopment && !process.env.IS_TEST) { // 生產環境 url = path.join(__dirname, '../../../extraResources/PrintScr.exe'); } let screen_window = execFile(url); })
}