gpt4 book ai didi

node.js - 使用 Electron 选择桌面屏幕的区域

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:21 29 4
gpt4 key购买 nike

我正在尝试编写一个应用程序,允许用户选择屏幕区域(例如选择拍摄屏幕截图)。

folders in home

这可能吗?

最佳答案

要专门拍摄全屏截图,请使用以下代码(从 Electron 演示应用程序中提取的示例)。您可以在此示例的基础上进行构建,并使用 screen , desktopCapturerrectangle Electron API 中的模块可自定义代码以获得特定的屏幕/显示,或选择特定的边界框(x/y 坐标和像素区域)。

const electron = require('electron')
const desktopCapturer = electron.desktopCapturer
const electronScreen = electron.screen
const shell = electron.shell

const fs = require('fs')
const os = require('os')
const path = require('path')

const screenshot = document.getElementById('screen-shot')
const screenshotMsg = document.getElementById('screenshot-path')

screenshot.addEventListener('click', function (event) {
screenshotMsg.textContent = 'Gathering screens...'
const thumbSize = determineScreenShotSize()
let options = { types: ['screen'], thumbnailSize: thumbSize }

desktopCapturer.getSources(options, function (error, sources) {
if (error) return console.log(error)

sources.forEach(function (source) {
if (source.name === 'Entire screen' || source.name === 'Screen 1') {
const screenshotPath = path.join(os.tmpdir(), 'screenshot.png')

fs.writeFile(screenshotPath, source.thumbnail.toPng(), function (error) {
if (error) return console.log(error)
shell.openExternal('file://' + screenshotPath)
const message = `Saved screenshot to: ${screenshotPath}`
screenshotMsg.textContent = message
})
}
})
})
})

function determineScreenShotSize () {
const screenSize = electronScreen.getPrimaryDisplay().workAreaSize
const maxDimension = Math.max(screenSize.width, screenSize.height)
return {
width: maxDimension * window.devicePixelRatio,
height: maxDimension * window.devicePixelRatio
}
}

您可以采取的其他方法是:

  1. 使用object.getClientRects()在 DOM 中指定要捕获的特定元素,尽管这需要预先了解它们是什么。
  2. 在 View 中添加事件监听器,以通过 mouseClick、mouseMove 等“绘制”您想要的形状。此 stack overflow question有可以根据您想做的事情进行调整的答案。

关于node.js - 使用 Electron 选择桌面屏幕的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42519933/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com