Skip to content

Commit

Permalink
refactor: 重构项目,支持获取window截图 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
nashaofu authored Jan 21, 2024
1 parent 7817a7a commit 8180b4f
Show file tree
Hide file tree
Showing 33 changed files with 1,946 additions and 631 deletions.
34 changes: 18 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
[package]
name = "screenshots"
version = "0.8.7"
name = "xcap"
version = "0.0.1"
edition = "2021"
description = "A cross-platform screen capturer library"
description = "A cross-platform screen capture library"
license = "Apache-2.0"
documentation = "https://docs.rs/screenshots"
homepage = "https://github.com/nashaofu/screenshots-rs"
repository = "https://github.com/nashaofu/screenshots-rs.git"
keywords = ["screenshots", "screenshot", "screen", "capture"]
documentation = "https://docs.rs/xcap"
homepage = "https://github.com/nashaofu/xcap"
repository = "https://github.com/nashaofu/xcap.git"
keywords = ["screen", "monitor", "window", "capture", "image"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0"
display-info = "0.4"
image = "0.24"
percent-encoding = "2.3"
thiserror = "1.0"

[target.'cfg(target_os = "macos")'.dependencies]
core-graphics = "0.22"
core-foundation = "0.9"
core-graphics = "0.23"

[target.'cfg(target_os = "windows")'.dependencies]
fxhash = "0.2"
widestring = "1.0"
windows = { version = "0.51", features = [
windows = { version = "0.52", features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_Graphics_Dwm",
"Win32_UI_WindowsAndMessaging",
"Win32_Storage_Xps",
] }

[target.'cfg(target_os="linux")'.dependencies]
percent-encoding = "2.3"
xcb = { version = "1.3", features = ["randr"] }
dbus = { version = "0.9", features = ["vendored"] }
libwayshot = "0.2"
xcb = "1.2"
87 changes: 70 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,90 @@
# 📷 Screenshots
# 📷 XCap

Screenshots is a cross-platform screenshots library for MacOS, Windows, Linux (X11, Wayland) written in Rust. It provides a simple API for capturing screenshots of a screen or a specific area of a screen.
XCap is a cross-platform screen capture library for MacOS, Windows, Linux (X11, Wayland) written in Rust. It provides a simple API for capturing screen capture of a screen or a specific area of a screen.

## Features

- Cross-platform support: Windows Mac and Linux.
- Multiple capture modes: screen window.
- Video capture、audio capture soon.

## Example

The following example shows how to capture screenshots of all screens and a specific area of a screen.
- Monitor capture

```rust
use screenshots::Screen;
use xcap::Monitor;
use std::time::Instant;

fn normalized(filename: &str) -> String {
filename
.replace("|", "")
.replace("\\", "")
.replace(":", "")
.replace("/", "")
}

fn main() {
let start = Instant::now();
let screens = Screen::all().unwrap();
let monitors = Monitor::all().unwrap();

for monitor in monitors {
let image = monitor.capture_image().unwrap();

for screen in screens {
println!("capturer {screen:?}");
let mut image = screen.capture().unwrap();
image
.save(format!("target/{}.png", screen.display_info.id))
.save(format!("target/monitor-{}.png", normalized(monitor.name())))
.unwrap();
}

println!("运行耗时: {:?}", start.elapsed());
}
```

- Window capture

```rust
use xcap::Window;
use std::time::Instant;

fn normalized(filename: &str) -> String {
filename
.replace("|", "")
.replace("\\", "")
.replace(":", "")
.replace("/", "")
}

fn main() {
let start = Instant::now();
let windows = Window::all().unwrap();

let mut i = 0;

for window in windows {
// 最小化的窗口不能截屏
if window.is_minimized() {
continue;
}

image = screen.capture_area(300, 300, 300, 300).unwrap();
println!(
"Window: {:?} {:?} {:?}",
window.title(),
(window.x(), window.y(), window.width(), window.height()),
(window.is_minimized(), window.is_maximized())
);

let image = window.capture_image().unwrap();
image
.save(format!("target/{}-2.png", screen.display_info.id))
.save(format!(
"target/window-{}-{}.png",
i,
normalized(window.title())
))
.unwrap();
}

let screen = Screen::from_point(100, 100).unwrap();
println!("capturer {screen:?}");
i += 1;
}

let image = screen.capture_area(300, 300, 300, 300).unwrap();
image.save("target/capture_display_with_point.png").unwrap();
println!("运行耗时: {:?}", start.elapsed());
}
```
Expand Down Expand Up @@ -60,4 +113,4 @@ pacman -S libxcb libxrandr dbus

## License

This project is licensed under the Apache License. See the [LICENSE](LICENSE) file for details.
This project is licensed under the Apache License. See the [LICENSE](../LICENSE) file for details.
33 changes: 33 additions & 0 deletions examples/monitor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::time::Instant;
use xcap::Monitor;

fn main() {
let start = Instant::now();
let monitors = Monitor::all().unwrap();
println!("Monitor::all() 运行耗时: {:?}", start.elapsed());

for monitor in monitors {
println!(
"Monitor: {} {} {:?} {:?}",
monitor.id(),
monitor.name(),
(monitor.x(), monitor.y(), monitor.width(), monitor.height()),
(
monitor.rotation(),
monitor.scale_factor(),
monitor.frequency(),
monitor.is_primary()
)
);
}

let monitor = Monitor::from_point(100, 100).unwrap();

println!("Monitor::from_point(): {}", monitor.name());
println!(
"Monitor::from_point(100, 100) 运行耗时: {:?}",
start.elapsed()
);

println!("运行耗时: {:?}", start.elapsed());
}
25 changes: 25 additions & 0 deletions examples/monitor_capture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::time::Instant;
use xcap::Monitor;

fn normalized(filename: &str) -> String {
filename
.replace("|", "")
.replace("\\", "")
.replace(":", "")
.replace("/", "")
}

fn main() {
let start = Instant::now();
let monitors = Monitor::all().unwrap();

for monitor in monitors {
let image = monitor.capture_image().unwrap();

image
.save(format!("target/monitor-{}.png", normalized(monitor.name())))
.unwrap();
}

println!("运行耗时: {:?}", start.elapsed());
}
27 changes: 0 additions & 27 deletions examples/screenshots.rs

This file was deleted.

22 changes: 22 additions & 0 deletions examples/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::time::Instant;
use xcap::Window;

fn main() {
let start = Instant::now();
let windows = Window::all().unwrap();
println!("Window::all() 运行耗时: {:?}", start.elapsed());

for window in windows {
println!(
"Window: {} {} {} {:?} {:?} {:?}",
window.id(),
window.title(),
window.app_name(),
window.current_monitor().name(),
(window.x(), window.y(), window.width(), window.height()),
(window.is_minimized(), window.is_maximized())
);
}

println!("运行耗时: {:?}", start.elapsed());
}
44 changes: 44 additions & 0 deletions examples/window_capture.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::time::Instant;
use xcap::Window;

fn normalized(filename: &str) -> String {
filename
.replace("|", "")
.replace("\\", "")
.replace(":", "")
.replace("/", "")
}

fn main() {
let start = Instant::now();
let windows = Window::all().unwrap();

let mut i = 0;

for window in windows {
// 最小化的窗口不能截屏
if window.is_minimized() {
continue;
}

println!(
"Window: {:?} {:?} {:?}",
window.title(),
(window.x(), window.y(), window.width(), window.height()),
(window.is_minimized(), window.is_maximized())
);

let image = window.capture_image().unwrap();
image
.save(format!(
"target/window-{}-{}.png",
i,
normalized(window.title())
))
.unwrap();

i += 1;
}

println!("运行耗时: {:?}", start.elapsed());
}
56 changes: 0 additions & 56 deletions src/darwin.rs

This file was deleted.

Loading

0 comments on commit 8180b4f

Please sign in to comment.