Skip to main content

Screenshot

Struct Screenshot 

Source
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

Source

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.

Source

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}
Source

pub fn into_png(self) -> Vec<u8> ⓘ

Consumes the screenshot and returns the PNG bytes.

Source

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}
Source

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}
Source

pub const fn frame(&self) -> Rect

Screen area covered, in points.

Source

pub fn to_screen_point(&self, x: f64, y: f64) -> Point

Converts a pixel position in this image to a screen point.

Source

pub fn from_screen_point(&self, point: Point) -> (f64, f64)

Converts a screen point to a pixel position in this image.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Screenshot

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Screenshot

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Screenshot

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.