我在研究中找到了一种使用Node.js截取窗口截图的方法,我尝试使用node-ffi进行此操作,但我不知道怎么......我一直困在这里:
var ffi = require('ffi');
var user32 = new ffi.Library("user32", {
FindWindowA: [ 'uint32' , [ 'string', 'string' ]]
, PrintWindow: [ 'int32' , [ 'int32', 'string', 'int32' ]]
});
var IMG;
var windowHandle = user32.FindWindowA(null, "Calculator");
var printWin = user32.PrintWindow(windowHandle, IMG, 0);
console.log(printWin);
console.log(IMG);
结果:
$ node get-print.js
1
undefined
EDITED
我在C ++中找到了以下工作代码
Bitmap bm = new Bitmap(1024, 768);
Graphics g = Graphics.FromImage(bm);
IntPtr hdc = g.GetHdc();
Form1.PrintWindow(this.Handle, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
g.Dispose();
this.pictureBox1.Image = bm;
现在我需要在NodeJ上执行此操作,
任何人都可以帮助我?
答案 0 :(得分:4)
您可以使用名为" desktop-screenshot"的NPM包。它使用起来非常简单。
关于NPM的例子:
var screenshot = require('desktop-screenshot');
screenshot("screenshot.png", function(error, complete) {
if(error)
console.log("Screenshot failed", error);
else
console.log("Screenshot succeeded");
});
答案 1 :(得分:2)
尽管我没有完整的代码,但是从理论上讲,如果您能够在C ++中运行,则只需使用node-gyp将C ++文件编译为.node文件,然后将其包含在你是nodeJS文件。
所以有一些伪代码示例,首先在一个新目录中创建一个binding.gyp文件,并在其中放置一些代码,如下所示:
{
"targets": [
{
"target_name": "addon",
"sources": [
"hi.cc"
]
}
]
}
然后在同一目录(现在)中创建另一个名为hi.cc
的文件,并将您的C ++代码放入其中,以及其他一些文件来制作节点模块。因此,根据上述文档,您可以执行以下操作(未经测试):
/*don't know what includes you're using to git the Bitmap
and Graphics functions, but include them here */
/*then to make a node module*/
#include <node.h>
using namespace v8;
void GetImage(const FunctionCallbackInfi<Value>& args) {
Bitmap bm = new Bitmap(1024, 768);
Graphics g = Graphics.FromImage(bm);
IntPtr hdc = g.GetHdc();
Form1.PrintWindow(this.Handle, hdc, 0);
g.ReleaseHdc(hdc);
g.Flush();
/*
this is the key part, although I'm not
100% sure it will work since I don't
know exactly what type Graphics returns,
but basically just convert it somehow into
base64, or a plain old void* value
(as in this following example), then make a new
Local variable of it and set the return type
(or make a function callback). So first get the
Graphics variable into a void* of the data, then
convert it to an ArrayBuffer to use in NodeJS, based on this
answer. Anyway:
*/
Local<
ArrayBuffer
>
v =
ArrayBuffer::New(i, /*some void* value*/ temp, 5000/*or some length*/);
a.GetReturnValue().Set(v);
}
void Initialize(Local<Object> exports) {
NODE_SET_METHOD(exports, "hello", GetImage);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)
然后确保您确实安装了node-gyp和正确的构建工具(请参阅上面的文档,但实际上是npm i -g node-gyp
),然后转到build-> Release-> addon.node并将其复制到您的主要nodeJS目录,然后创建一个新的nodeJS文件或在现有文件中包含以下文件:
let addon = require("./addon"),
pictureData = Buffer.from(addon.hello()/* if you choose to return a base64 string instead, then insert: ,"base64"*/);
答案 2 :(得分:0)
还有一个目前仍在开发中的替代 Node.js 包(最后一次提交是 15 天前;与上面提到的包相比,它最后一次提交是在 2015 年或 2016 年)。它允许选择它捕获的屏幕,而另一个似乎没有这样做。
https://github.com/bencevans/screenshot-desktop
const screenshot = require('screenshot-desktop');
screenshot.listDisplays().then((displays) => {
// displays: [{ id, name }, { id, name }]
screenshot({ screen: displays[displays.length - 1].id })
.then((img) => {
// img: Buffer of screenshot of the last display
});
})