Javascript web应用程序中的桌面屏幕捕获
我需要在我的web应用程序(核心php和javascript)中捕获deskop屏幕。我想知道是否有可能用javascript或任何javascript框架捕获桌面屏幕。如果可能,请向我推荐一些代码示例、api或演示。 我在谷歌上搜索了这么多,但没有找到任何可行的解决方案Javascript web应用程序中的桌面屏幕捕获,javascript,php,electron,Javascript,Php,Electron,我需要在我的web应用程序(核心php和javascript)中捕获deskop屏幕。我想知道是否有可能用javascript或任何javascript框架捕获桌面屏幕。如果可能,请向我推荐一些代码示例、api或演示。 我在谷歌上搜索了这么多,但没有找到任何可行的解决方案 提前感谢这根本不可能,因为PHP是服务器端,与客户端桌面无关,而且绝对JavaScript运行在浏览器或electron环境中,两者都与OS环境分离,JavaScript代码是在封闭环境中运行的,因此不可能运行。根本不可能,因
提前感谢这根本不可能,因为
PHP
是服务器端,与客户端桌面无关,而且绝对JavaScript
运行在浏览器或electron
环境中,两者都与OS
环境分离,JavaScript
代码是在封闭环境中运行的,因此不可能运行。根本不可能,因为PHP
是服务器端,与客户端桌面无关,而且绝对JavaScript
运行在浏览器或electron
环境中,两者都与OS
环境分离,JavaScript
代码运行在封闭环境中,因此这是不可能的。JavaScript完全可以访问文档对象模型,因此至少在理论上,它可以捕获自己网页上的内容(但不能捕获浏览器窗口之外的任何内容),而且有一个库可以这样做:(我没有尝试过。)JavaScript可以完全访问文档对象模型,因此至少在理论上,它可以捕获自己网页上的内容(但不能捕获浏览器窗口之外的任何内容)还有一个库可以做到这一点:(我还没有尝试过。)应用程序可以利用API拍摄屏幕截图
应用程序中提供了演示(带代码)。您可以为您的操作系统安装软件,也可以自己构建
渲染器进程:
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
}
}
应用程序可以利用API拍摄屏幕截图
应用程序中提供了演示(带代码)。您可以为您的操作系统安装软件,也可以自己构建
渲染器进程:
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
}
}
你是说浏览器窗口吗?!你有没有试过在谷歌上搜索“js截图”?@Luca,我试了越来越多,
js截图
只是从浏览器或电子人脸上截图,而不是从桌面上截图。可能是重复的可能会有所帮助。你是说浏览器窗口吗?!你有没有试过在谷歌上搜索“js截图”?@Luca,我试了越来越多,js截图
只是从浏览器或电子人脸上截图,而不是从桌面上截图。可能是重复的可能会有所帮助。