image/
image.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#[cfg(target_os = "linux")]
use clipboard_rs::ClipboardContextX11Options;
use clipboard_rs::{common::RustImage, Clipboard, ClipboardContext};

#[cfg(target_os = "macos")]
const TMP_PATH: &str = "/tmp/";
#[cfg(target_os = "windows")]
const TMP_PATH: &str = "C:\\Windows\\Temp\\";
#[cfg(all(
	unix,
	not(any(
		target_os = "macos",
		target_os = "ios",
		target_os = "android",
		target_os = "emscripten"
	))
))]
const TMP_PATH: &str = "/tmp/";

#[cfg(target_os = "linux")]
fn setup_clipboard() -> ClipboardContext {
	ClipboardContext::new_with_options(ClipboardContextX11Options { read_timeout: None }).unwrap()
}

#[cfg(not(target_os = "linux"))]
fn setup_clipboard() -> ClipboardContext {
	ClipboardContext::new().unwrap()
}

fn main() {
	let ctx = setup_clipboard();

	let types = ctx.available_formats().unwrap();
	println!("{:?}", types);

	let img = ctx.get_image();

	match img {
		Ok(img) => {
			let _ = img
				.save_to_path(format!("{}test.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test.png err={}", e));

			let resize_img = img
				.thumbnail(300, 300)
				.map_err(|e| println!("thumbnail err={}", e))
				.unwrap();

			let _ = resize_img
				.save_to_path(format!("{}test_thumbnail.png", TMP_PATH).as_str())
				.map_err(|e| println!("save test_thumbnail.png err={}", e));
		}
		Err(err) => {
			println!("err={}", err);
		}
	}
}