-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0762293
commit e8560bf
Showing
3 changed files
with
53 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,5 @@ target/ | |
# Added by cargo | ||
|
||
/target | ||
|
||
*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters