我正在使用电子创建一个Windows应用程序,该应用程序创建一个全屏透明覆盖窗口。此覆盖的目的是:

问题描述 投票:0回答:4
在覆盖层上绘制一些东西

我被困在第一步,这是屏幕截图捕获过程。 我尝试了使用
    capturePage()
  1. this.electronService.remote.getCurrentWindow().webContents.capturePage() .then((img: Electron.NativeImage) => { ... }
  2. desktopCapturer

,但这仅捕获我的覆盖窗口(而不捕获桌面屏幕)。这将是一个对我没有用的空白图像。

eption2是使用this.electronService.remote.desktopCapturer.getSources({types: ['screen']}).then(sources => { for (const source of sources) { if (source.name === 'Screen 1') { try { const mediaDevices = navigator.mediaDevices as any; mediaDevices.getUserMedia({ audio: false, video: { // this specification is only available for Chrome -> Electron runs on Chromium browser mandatory: { chromeMediaSource: 'desktop', chromeMediaSourceId: source.id, minWidth: 1280, maxWidth: 1280, minHeight: 720, maxHeight: 720 } } }).then((stream: MediaStream) => { // stream.getVideoTracks()[0] contains the video track I need this.handleStream(stream); }); } catch (e) { } } } });

MediaStream

下一步是对我来说变得模糊的地方。 我该如何处理被收购的

handleStream()

从屏幕截图中获取屏幕截图?我看到了很多示例如何在网页上显示此流,但我希望将其发送到我的后端。 this stackoverflow帖子提到了如何做,但我没有使它正常工作。 这就是我实施import * as MediaStreamRecorder from 'msr'; private handleStream(stream: MediaStream): void { recorder.stop() const recorder = new MediaStreamRecorder(stream); recorder.ondataavailable = (blob: Blob) => { // I immediately get a blob, while the linked SO page got an event and had to get a blob through event.data this.http.post<Result>('http://localhost:5050', blob); }; // make data available event fire every one second recorder.start(1000); }

blob
python服务器未接受
Blob

。 检查

let url = URL.createObjectURL(blob);
window.open(url, '_blank')
的内容后,这是我怀疑的视频。 我用以下代码对此进行了验证: byte[]
在新窗口中打开斑点。它显示了大概半秒的视频,但我想拥有静态图像。那么如何从中获得特定的快照呢?我也不确定是否只是在邮局中仅发送JavaScript Blob格式,以使Python正确解释它。在Java中,它可以通过简单地发送图像来工作,因此我验证了Python服务器实现如预期的。

使用除了使用
desktopCapturer
以外的任何建议也可以。 这个实现也正在捕获我的鼠标,我宁愿没有。 我必须承认,我没想到这个功能很难实施。

在这里,您可以使用桌面屏幕截图:
const { desktopCapturer } = require('electron')

document.getElementById('screenshot-button').addEventListener('click', () => { // The button which takes the screenshot
    desktopCapturer.getSources({ types: ['screen'] })
        .then( sources => {
            document.getElementById('screenshot-image').src = sources[0].thumbnail.toDataURL() // The image to display the screenshot
        })
})

使用``屏幕''将截图整个桌面。
使用“ Windows”将仅对应用程序进行屏幕截图。
也请参考以下文档:

Https://www.electronjs.org/docs/api/desktop-capturer


    

您可以从这样的视频中获取每个帧:

desktopCapturer.getSources({
        types: ['window'], thumbnailSize: {
            height: 768,
            width: 1366
        }
    }).then(sources => {
        for (let s in sources) {
            const content = sources[s].thumbnail.toPNG()
            console.log(content)
            }
})

javascript angular electron
4个回答
12
投票

desktopCapturer
只拍摄视频。因此,您需要从中获得一个帧。您可以为此使用HTML5
canvas

。这是一个示例:

https://ourcodeworld.com/articles/Read/280/creating-screenshots-of-your-app-or-the-screen-in-electron-framework®

或使用NPM上可用的一些第三方屏幕快照库。我发现的一个人需要在Linux上安装ImageMagick,但也许还有更多,或者您不需要支持Linux。您需要在主要电子过程中执行此操作,在该过程中,您可以在节点中执行任何操作。

对于那些可能关注的人,这是您使用IPC进行

3
投票
缩略图方法的方式:

main过程:

2
投票
app.whenReady().then(() => { ipcMain.handle('captureScreenshot', async (e, width, height) => { const sources = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width, height } }) return sources[0].thumbnail.toDataURL() }) // ... }

PRELOAD.JS:

contextBridge.exposeInMainWorld('nativeBits', {
  captureScreenshot: (width, height) => ipcRenderer.invoke('captureScreenshot', width, height)
})

renderer:

document.getElementById('screenshot-button').addEventListener('click', async () => { const dataURL = await nativeBits.captureScreenshot(640, 360) document.getElementById('screenshot-image').src = dataURL })

或以博客文章的形式(

0
投票
)如果您想进一步的故障,而不是舒适地回复的内容。 关键要点:以这种方式进行全尺寸屏幕截图意味着在每个屏幕上进行屏幕截图,因此,如果您想经常拍摄屏幕截图,则应该更喜欢实际的屏幕截图API。


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.