From 077272db8bc55076692e99711a9662e95c3957a6 Mon Sep 17 00:00:00 2001 From: nashaofu Date: Mon, 18 Jul 2022 23:02:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=88=AA=E5=8F=96?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=E9=83=A8=E5=88=86=E5=8C=BA=E5=9F=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 0.0.8 --- Cargo.toml | 2 +- README.md | 7 +++++++ example.js | 9 +++++++-- index.d.ts | 5 ++++- package.json | 2 +- src/lib.rs | 31 +++++++++++++++++++++++++++---- 6 files changed, 47 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39a140a..782d33e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ crate-type = ["cdylib"] # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix napi = { version = "2.5.0", default-features = false, features = ["napi4"] } napi-derive = "2.5.0" -screenshots = "0.3.3" +screenshots = "0.4.0" [build-dependencies] napi-build = "2.0.1" diff --git a/README.md b/README.md index 4bd6534..0707d6a 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,11 @@ all.forEach(capturer => { // 从屏幕id获取截图,id可以通过 `Screenshots.all()` 获取,也可以通过electron的 `screen.getAllDisplays()` 获取 let sc = Screenshots.fromDisplay(71) + +// 截取屏幕的一部分区域 +sc.captureArea(300, 300, 300, 300).then(buffer => { + fs.writeFileSync(`captureArea-${capturer.id}.png`, buffer) +}) ``` ## API @@ -79,3 +84,5 @@ let sc = Screenshots.fromDisplay(71) - `Screenshots.all()`: 获取所有屏幕 - `screenshots.capture()`: 异步截图 - `screenshots.captureSync()`: 同步截图 +- `screenshots.captureArea(x, y, width, height)`: 异步截图 +- `screenshots.captureAreaSync(x, y, width, height)`: 同步截图 diff --git a/example.js b/example.js index 2f7578b..317801b 100644 --- a/example.js +++ b/example.js @@ -41,7 +41,12 @@ all.forEach(capturer => { y: capturer.y, width: capturer.width, height: capturer.height, - scale: capturer.scale, - rotation: capturer.rotation + rotation: capturer.rotation, + scaleFactor: capturer.scaleFactor, + isPrimary: capturer.isPrimary }) }) + +capturer.captureArea(300, 300, 300, 300).then(buffer => { + fs.writeFileSync(`captureArea-${capturer.id}.png`, buffer) +}) diff --git a/index.d.ts b/index.d.ts index f606a5a..760a68d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,11 +9,14 @@ export class Screenshots { y: number width: number height: number - scale: number rotation: number + scaleFactor: number + isPrimary: boolean static all(): Array static fromDisplay(id: number): Screenshots | null static fromPoint(x: number, y: number): Screenshots | null captureSync(): Buffer | null capture(): Promise + captureAreaSync(x: number, y: number, width: number, height: number): Buffer | null + captureArea(x: number, y: number, width: number, height: number): Promise } diff --git a/package.json b/package.json index f208ac7..33b93af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-screenshots", - "version": "0.0.7", + "version": "0.0.8", "main": "index.js", "types": "index.d.ts", "scripts": { diff --git a/src/lib.rs b/src/lib.rs index 4cfbdc3..c0b12d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,12 +20,14 @@ pub struct Screenshots { pub y: i32, pub width: u32, pub height: u32, - pub scale: f64, pub rotation: f64, + pub scale_factor: f64, + pub is_primary: bool, } pub struct AsyncCapture { screen: Screen, + area: Option<(i32, i32, u32, u32)>, } #[napi] @@ -34,9 +36,14 @@ impl Task for AsyncCapture { type JsValue = Buffer; fn compute(&mut self) -> Result { - let screen = self.screen; + let AsyncCapture { screen, area } = *self; let handle = thread::spawn(move || { - let image = screen.capture()?; + let image = if let Some((x, y, width, height)) = area { + screen.capture_area(x, y, width, height)? + } else { + screen.capture()? + }; + Some(Buffer::from(image.buffer().clone())) }); @@ -66,8 +73,9 @@ impl Screenshots { y: screen.y, width: screen.width, height: screen.height, - scale: screen.scale as f64, rotation: screen.rotation as f64, + scale_factor: screen.scale_factor as f64, + is_primary: screen.is_primary, } } #[napi] @@ -102,6 +110,21 @@ impl Screenshots { pub fn capture(&self) -> AsyncTask { AsyncTask::new(AsyncCapture { screen: self.screen, + area: None, + }) + } + + #[napi] + pub fn capture_area_sync(&self, x: i32, y: i32, width: u32, height: u32) -> Option { + let image = self.screen.capture_area(x, y, width, height)?; + Some(Buffer::from(image.buffer().clone())) + } + + #[napi] + pub fn capture_area(&self, x: i32, y: i32, width: u32, height: u32) -> AsyncTask { + AsyncTask::new(AsyncCapture { + screen: self.screen, + area: Some((x, y, width, height)), }) } }