electron 截图
时间: 2023-10-01 19:02:09 浏览: 77
抱歉,作为一个 AI,我没有界面来进行截图。但是,你可以使用 Electron 提供的桌面捕捉 API 来实现截图功能。以下是一个示例代码:
```javascript
const { desktopCapturer } = require('electron');
desktopCapturer.getSources({ types: ['screen'] }).then(async sources => {
const source = sources[0];
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: source.id,
minWidth: 1280,
maxWidth: 1280,
minHeight: 720,
maxHeight: 720
}
}
});
const video = document.createElement('video');
video.srcObject = stream;
video.onloadedmetadata = () => {
const canvas = document.createElement('canvas');
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
const dataURL = canvas.toDataURL();
console.log(dataURL);
};
});
```
这个代码会截取屏幕上的一张 1280x720 的截图,并输出为 base64 编码的数据 URL。你也可以将数据 URL 赋值给图片元素的 src 属性来显示截图。
阅读全文