Code Monkey home page Code Monkey logo

screenshots-rs's Introduction

📷 Screenshots

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.

Example

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

use screenshots::Screen;
use std::{fs, time::Instant};

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

  for screen in screens {
    println!("capturer {screen:?}");
    let mut image = screen.capture().unwrap();
    let mut buffer = image.buffer();
    fs::write(format!("target/{}.png", screen.display_info.id), buffer).unwrap();

    image = screen.capture_area(300, 300, 300, 300).unwrap();
    buffer = image.buffer();
    fs::write(format!("target/{}-2.png", screen.display_info.id), buffer).unwrap();
  }

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

  let image = screen.capture_area(300, 300, 300, 300).unwrap();
  let buffer = image.buffer();
  fs::write("target/capture_display_with_point.png", buffer).unwrap();

  println!("Elapsed time: {:?}", start.elapsed());
}

API

Screen

The Screen struct represents a screen capturer and provides the following methods:

  • Screen::new(display_info): Get a screen from the display info, returns a Screen.
  • Screen::all(): Get all screens, returns Result<Vec<Screen>>.
  • Screen::from_point(x, y): Get a screen from a point, returns Result<Screen>.
  • screen.capture(): Capture a screenshot of the screen, returns a image.
  • screen.capture_area(x, y, width, height): Capture a screenshot of the designated area of the screen, returns a Result<Image>.

Image

The Image struct represents a screen screenshot image and provides the following methods:

  • Image::new(width, height, buffer): Get an image from the width, height, and RGBA buffer, returns an Image.
  • Image::from_bgra(width, height, buffer): Get an image from the width, height, and BGRA buffer, returns Result<Image, EncodingError>.
  • image.width(): Get the image width, returns u32.
  • image.height(): Get the image height, returns u32.
  • image.buffer(): Get the image buffer, returns Vec<u8>.

Linux Requirements

On Linux, you need to install libxcb, libxrandr, and dbus.

Debian/Ubuntu:

apt-get install libxcb1 libxrandr2 libdbus-1-3

Alpine:

apk add libxcb libxrandr dbus

License

This project is licensed under the Apache License. See the LICENSE file for details.

screenshots-rs's People

Contributors

joshterrill avatar lwizchz avatar mdacach avatar mokurin000 avatar nashaofu avatar olssdani avatar radoncoding avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.