Skip to content
المشاريع

حول

مكتبة لالتقاط الشاشة متعددة المنصات، مكتوبة بلغة Rust. تدعم Linux (X11 وWayland) وmacOS وWindows، وتتيح التقاط لقطات الشاشة وتسجيل الفيديو (قيد التطوير).

التصميم والوسائط

كيف تُقاس هذه الأرقام

1,048 نجمة145 التفرعات +1 خلال 24 ساعة

ما يمكنك فعله

  • Cross-platform: Supports Linux (X11, Wayland), MacOS, and Windows.
  • Supports multiple screenshot modes: Can take screenshots of the screen and windows.
  • Supports video recording: Supports recording of the screen or window (WIP).

النمو المقاس للنجوم

خلال 24 ساعة
+١
خلال 7 أيام
+٣
خلال 15 يومًا
+١٠
خلال شهر
+١٨

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

XCap

English | 简体中文

XCap is a cross-platform screen capture library written in Rust. It supports Linux (X11, Wayland), MacOS, and Windows. XCap supports screenshot and video recording (WIP).

Features

  • Cross-platform: Supports Linux (X11, Wayland), MacOS, and Windows.
  • Supports multiple screenshot modes: Can take screenshots of the screen and windows.
  • Supports video recording: Supports recording of the screen or window (WIP).

Implementation Status

Feature Linux(X11) Linux(Wayland) MacOS Windows(>=Windows 8.1)
Screen Capture ✅ ⛔ ✅ ✅
Window Capture ✅ ⛔ ✅ ✅
Screen Recording ✅ ⛔ ✅ ✅
Window Recording 🛠️ 🛠️ 🛠️ 🛠️
  • ✅: Feature available
  • ⛔: Feature available, but not fully supported in some special scenarios
  • 🛠️: To be developed

Examples

  • Screen Capture
use fs_extra::dir;
use std::time::Instant;
use xcap::Monitor;

fn normalized(filename: String) -> String {
    filename.replace(['|', '\\', ':', '/'], "")
}

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

    dir::create_all("target/monitors", true).unwrap();

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

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

    println!("运行耗时: {:?}", start.elapsed());
}
  • Region Capture
use fs_extra::dir;
use std::time::Instant;
use xcap::Monitor;

fn normalized(filename: String) -> String {
    filename.replace(['|', '\\', ':', '/'], "")
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monitors = Monitor::all()?;
    dir::create_all("target/monitors", true).unwrap();

    let monitor = monitors
        .into_iter()
        .find(|m| m.is_primary().unwrap_or(false))
        .expect("No primary monitor found");

    let monitor_width = monitor.width()?;
    let monitor_height = monitor.height()?;

    let region_width = 400u32;
    let region_height = 300u32;

    let x = ((monitor_width as i32) - (region_width as i32)) / 2;
    let y = ((monitor_height as i32) - (region_height as i32)) / 2;
    let start = Instant::now();

    let image = monitor.capture_region(x, y, region_width, region_height)?;
    println!(
        "Time to record region of size {}x{}: {:?}",
        image.width(),
        image.height(),
        start.elapsed()
    );

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

    Ok(())
}
  • Screen Record
use std::{thread, time::Duration};
use xcap::Monitor;

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

    let (video_recorder, sx) = monitor.video_recorder().unwrap();

    thread::spawn(move || loop {
        match sx.recv() {
            Ok(frame) => {
                println!("frame: {:?}", frame.width);
            }
            _ => continue,
        }
    });

    println!("start");
    video_recorder.start().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("stop");
    video_recorder.stop().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("start");
    video_recorder.start().unwrap();
    thread::sleep(Duration::from_secs(2));
    println!("stop");
    video_recorder.stop().unwrap();
}
  • Screen Record with Options (macOS / Windows)

VideoRecorderOptions caps the frame rate, records a sub-rectangle in monitor units, and controls the cursor and own-window exclusion. Every Frame carries a timestamp measured from when the recorder was created. OutputSize::Logical asks ScreenCaptureKit to deliver frames at the region's point size instead of its Retina pixel size; DXGI ignores it, so keep a CPU downscale as a fallback.

use std::time::{Duration, Instant};
use xcap::{CaptureRegion, Monitor, OutputSize, VideoRecorderOptions};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let monitor = Monitor::from_point(100, 100)?;

    let options = VideoRecorderOptions::default()
        .fps(15)
        .region(CaptureRegion {
            x: 100,
            y: 100,
            width: 640,
            height: 400,
        })
        .captures_cursor(false)
        .excludes_own_windows(true)
        .output_size(OutputSize::Logical);

    let (recorder, frames) = monitor.video_recorder_with_options(options)?;

    recorder.start()?;
    let started = Instant::now();
    while started.elapsed() < Duration::from_secs(3) {
        let frame = frames.recv_timeout(Duration::from_secs(1))?;
        println!(
            "{}x{} at {:?}",
            frame.width, frame.height, frame.timestamp
        );
    }
    recorder.stop()?;

    Ok(())
}

captures_cursor and excludes_own_windows only take effect on macOS. CaptureRegion uses monitor units (points on macOS, physical pixels on Windows); frame dimensions are always physical pixels.

  • Window Capture
use fs_extra::dir;
use std::time::Instant;
use xcap::Window;

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

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

    dir::create_all("target/windows", true).unwrap();

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

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

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

        i += 1;
    }

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

More examples in examples. The recording examples can be run with:

# Region + frame-rate cap, saves target/examples/monitor_record_region.png
cargo run --example monitor_record_region

# Every option from the command line
cargo run --example monitor_record_options -- --fps 10 --width 800 --height 600
cargo run --example monitor_record_options -- --help

Linux System Requirements

On Linux, the following dependencies need to be installed to compile properly.

Debian/Ubuntu:

apt-get install pkg-config libclang-dev libxcb1-dev libxrandr-dev libdbus-1-dev libpipewire-0.3-dev libwayland-dev libegl-dev

Alpine:

apk add pkgconf llvm19-dev clang19-dev libxcb-dev libxrandr-dev dbus-dev pipewire-dev wayland-dev mesa-dev

ArchLinux:

pacman -S base-devel clang libxcb libxrandr dbus libpipewire

License

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

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…