Skip to content

Commit

Permalink
moved capture screen into module
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeClimberNT committed Sep 26, 2024
1 parent 0762293 commit e8560bf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ target/
# Added by cargo

/target

*.png
49 changes: 49 additions & 0 deletions src/capture_screen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use scrap::{Capturer, Display};
use std::thread;
use std::time::Duration;
use image::{ImageBuffer, Rgba};

pub fn get_monitors() -> Vec<Display> {
let monitors: Vec<Display> = Display::all().expect("Couldn't find any display.");
monitors
}

pub fn set_monitor(mut monitors: Vec<Display>, index: usize) -> Display {
monitors.remove(index)
}

pub fn capture_screen() {

// configurazione recupera monitor del sistema
let monitors = get_monitors();
for i in 0..monitors.len() {
println!("Monitor {}", i);
}

let monitor = set_monitor(monitors, 0);

let mut capturer: Capturer = Capturer::new(monitor).expect("Couldn't begin capture.");
let (width, height) = (capturer.width(), capturer.height());

loop {
match capturer.frame() {
Ok(frame) => {
let mut buffer: Vec<u8> = frame.iter().cloned().collect();
// Convert BGRA to RGBA
for chunk in buffer.chunks_exact_mut(4) {
chunk.swap(0, 2); // Swap B and R
}
let image =
ImageBuffer::<Rgba<u8>, _>::from_raw(width as u32, height as u32, buffer)
.unwrap();
image.save("screenshot.png").expect("Failed to save image");
println!("Captured frame with dimensions {}x{}", width, height);
break; // Exit after capturing one frame
}
Err(_) => {
// Capture failed, retry
thread::sleep(Duration::from_millis(100));
}
}
}
}
51 changes: 2 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,7 @@
// extern crate scrap;
use capture_screen::{capture_screen};

use scrap::{Capturer, Display};
use std::thread;
use std::time::Duration;
use image::{ImageBuffer, Rgba};
mod capture_screen;

fn get_monitors() -> Vec<Display> {
let monitors: Vec<Display> = Display::all().expect("Couldn't find any display.");
monitors
}

fn set_monitor(mut monitors: Vec<Display>, index: usize) -> Display {
monitors.remove(index)
}

fn capture_screen() {

// configurazione recupera monitor del sistema
let monitors = get_monitors();
for i in 0..monitors.len() {
println!("Monitor {}", i);
}

let monitor = set_monitor(monitors, 0);

let mut capturer: Capturer = Capturer::new(monitor).expect("Couldn't begin capture.");
let (width, height) = (capturer.width(), capturer.height());

loop {
match capturer.frame() {
Ok(frame) => {
let mut buffer: Vec<u8> = frame.iter().cloned().collect();
// Convert BGRA to RGBA
for chunk in buffer.chunks_exact_mut(4) {
chunk.swap(0, 2); // Swap B and R
}
let image =
ImageBuffer::<Rgba<u8>, _>::from_raw(width as u32, height as u32, buffer)
.unwrap();
image.save("screenshot.png").expect("Failed to save image");
println!("Captured frame with dimensions {}x{}", width, height);
break; // Exit after capturing one frame
}
Err(_) => {
// Capture failed, retry
thread::sleep(Duration::from_millis(100));
}
}
}
}

fn main() {
capture_screen();
Expand Down

0 comments on commit e8560bf

Please sign in to comment.