pub struct Screenshot { /* private fields */ }Expand description
A PNG screenshot of a window together with the screen area it shows.
Coordinates handed to Computer::click and
Computer::drag are pixels in this image; the crate converts
them back to screen points with Screenshot::to_screen_point.
Implementations§
Source§impl Screenshot
impl Screenshot
Sourcepub fn encode(
image: RawImage,
frame: Rect,
max_dimension: Option<u32>,
) -> Result<Self>
pub fn encode( image: RawImage, frame: Rect, max_dimension: Option<u32>, ) -> Result<Self>
Downscales image so neither side exceeds max_dimension, then encodes it as PNG.
frame is the screen area, in points, the image covers.
§Errors
Returns an error if the buffer size does not match the dimensions or encoding fails.
Sourcepub fn png(&self) -> &[u8] ⓘ
pub fn png(&self) -> &[u8] ⓘ
PNG-encoded bytes.
Examples found in repository?
examples/inspect.rs (line 34)
13fn main() -> ExitCode {
14 use computer_use::{CaptureOptions, Computer};
15
16 let mut args = std::env::args().skip(1);
17 let Some(app) = args.next() else {
18 eprintln!("usage: inspect <app name | bundle id | pid:N> [screenshot.png]");
19 return ExitCode::FAILURE;
20 };
21 let output = args.next();
22
23 let run = || -> computer_use::Result<()> {
24 let mut computer = Computer::new()?;
25 let permissions = computer.permissions();
26 if !permissions.all_granted() {
27 eprintln!("warning: missing permissions: {permissions:?}");
28 }
29 let started = std::time::Instant::now();
30 let state = computer.get_app_state(&app, &CaptureOptions::default())?;
31 println!("{state}");
32 eprintln!("\ncaptured {} elements in {:?}", state.elements.len(), started.elapsed());
33 if let (Some(path), Some(shot)) = (output, &state.screenshot) {
34 std::fs::write(&path, shot.png())?;
35 eprintln!("saved {}x{} screenshot to {path}", shot.width(), shot.height());
36 }
37 Ok(())
38 };
39 match run() {
40 Ok(()) => ExitCode::SUCCESS,
41 Err(e) => {
42 eprintln!("error: {e}");
43 ExitCode::FAILURE
44 }
45 }
46}Sourcepub const fn width(&self) -> u32
pub const fn width(&self) -> u32
Width in pixels.
Examples found in repository?
examples/inspect.rs (line 35)
13fn main() -> ExitCode {
14 use computer_use::{CaptureOptions, Computer};
15
16 let mut args = std::env::args().skip(1);
17 let Some(app) = args.next() else {
18 eprintln!("usage: inspect <app name | bundle id | pid:N> [screenshot.png]");
19 return ExitCode::FAILURE;
20 };
21 let output = args.next();
22
23 let run = || -> computer_use::Result<()> {
24 let mut computer = Computer::new()?;
25 let permissions = computer.permissions();
26 if !permissions.all_granted() {
27 eprintln!("warning: missing permissions: {permissions:?}");
28 }
29 let started = std::time::Instant::now();
30 let state = computer.get_app_state(&app, &CaptureOptions::default())?;
31 println!("{state}");
32 eprintln!("\ncaptured {} elements in {:?}", state.elements.len(), started.elapsed());
33 if let (Some(path), Some(shot)) = (output, &state.screenshot) {
34 std::fs::write(&path, shot.png())?;
35 eprintln!("saved {}x{} screenshot to {path}", shot.width(), shot.height());
36 }
37 Ok(())
38 };
39 match run() {
40 Ok(()) => ExitCode::SUCCESS,
41 Err(e) => {
42 eprintln!("error: {e}");
43 ExitCode::FAILURE
44 }
45 }
46}Sourcepub const fn height(&self) -> u32
pub const fn height(&self) -> u32
Height in pixels.
Examples found in repository?
examples/inspect.rs (line 35)
13fn main() -> ExitCode {
14 use computer_use::{CaptureOptions, Computer};
15
16 let mut args = std::env::args().skip(1);
17 let Some(app) = args.next() else {
18 eprintln!("usage: inspect <app name | bundle id | pid:N> [screenshot.png]");
19 return ExitCode::FAILURE;
20 };
21 let output = args.next();
22
23 let run = || -> computer_use::Result<()> {
24 let mut computer = Computer::new()?;
25 let permissions = computer.permissions();
26 if !permissions.all_granted() {
27 eprintln!("warning: missing permissions: {permissions:?}");
28 }
29 let started = std::time::Instant::now();
30 let state = computer.get_app_state(&app, &CaptureOptions::default())?;
31 println!("{state}");
32 eprintln!("\ncaptured {} elements in {:?}", state.elements.len(), started.elapsed());
33 if let (Some(path), Some(shot)) = (output, &state.screenshot) {
34 std::fs::write(&path, shot.png())?;
35 eprintln!("saved {}x{} screenshot to {path}", shot.width(), shot.height());
36 }
37 Ok(())
38 };
39 match run() {
40 Ok(()) => ExitCode::SUCCESS,
41 Err(e) => {
42 eprintln!("error: {e}");
43 ExitCode::FAILURE
44 }
45 }
46}Sourcepub fn to_screen_point(&self, x: f64, y: f64) -> Point
pub fn to_screen_point(&self, x: f64, y: f64) -> Point
Converts a pixel position in this image to a screen point.
Sourcepub fn from_screen_point(&self, point: Point) -> (f64, f64)
pub fn from_screen_point(&self, point: Point) -> (f64, f64)
Converts a screen point to a pixel position in this image.
Sourcepub fn contains_pixel(&self, x: f64, y: f64) -> bool
pub fn contains_pixel(&self, x: f64, y: f64) -> bool
True when (x, y) lies inside the image.
Trait Implementations§
Source§impl Clone for Screenshot
impl Clone for Screenshot
Source§impl Debug for Screenshot
impl Debug for Screenshot
Source§impl PartialEq for Screenshot
impl PartialEq for Screenshot
impl StructuralPartialEq for Screenshot
Auto Trait Implementations§
impl Forget for Screenshot
impl Freeze for Screenshot
impl RefUnwindSafe for Screenshot
impl Send for Screenshot
impl Sync for Screenshot
impl Unpin for Screenshot
impl UnsafeUnpin for Screenshot
impl UnwindSafe for Screenshot
Blanket Implementations§
impl<T> AutoreleaseSafe for Twhere
T: ?Sized,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more