Skip to content

Commit

Permalink
feat: 支持截取屏幕部分区域
Browse files Browse the repository at this point in the history
0.0.8
  • Loading branch information
nashaofu committed Jul 18, 2022
1 parent b7d6f79 commit 077272d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)`: 同步截图
9 changes: 7 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ export class Screenshots {
y: number
width: number
height: number
scale: number
rotation: number
scaleFactor: number
isPrimary: boolean
static all(): Array<Screenshots>
static fromDisplay(id: number): Screenshots | null
static fromPoint(x: number, y: number): Screenshots | null
captureSync(): Buffer | null
capture(): Promise<Buffer>
captureAreaSync(x: number, y: number, width: number, height: number): Buffer | null
captureArea(x: number, y: number, width: number, height: number): Promise<Buffer>
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-screenshots",
"version": "0.0.7",
"version": "0.0.8",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
Expand Down
31 changes: 27 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -34,9 +36,14 @@ impl Task for AsyncCapture {
type JsValue = Buffer;

fn compute(&mut self) -> Result<Self::Output> {
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()))
});

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -102,6 +110,21 @@ impl Screenshots {
pub fn capture(&self) -> AsyncTask<AsyncCapture> {
AsyncTask::new(AsyncCapture {
screen: self.screen,
area: None,
})
}

#[napi]
pub fn capture_area_sync(&self, x: i32, y: i32, width: u32, height: u32) -> Option<Buffer> {
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<AsyncCapture> {
AsyncTask::new(AsyncCapture {
screen: self.screen,
area: Some((x, y, width, height)),
})
}
}

0 comments on commit 077272d

Please sign in to comment.