MCPcopy Create free account
hub / github.com/Recordscript/recordscript / record

Function record

libs/scrap/examples/screenshot.rs:22–141  ·  view source on GitHub ↗
(i: usize)

Source from the content-addressed store, hash-verified

20}
21
22fn record(i: usize) {
23 let one_second = Duration::new(1, 0);
24 let one_frame = one_second / 60;
25
26 for d in Display::all().unwrap() {
27 println!("{:?} {} {}", d.origin(), d.width(), d.height());
28 }
29
30 let display = get_display(i);
31 let mut capturer = Capturer::new(display).expect("Couldn't begin capture.");
32 let (w, h) = (capturer.width(), capturer.height());
33
34 loop {
35 // Wait until there's a frame.
36
37 let frame = match capturer.frame(Duration::from_millis(0)) {
38 Ok(frame) => frame,
39 Err(error) => {
40 if error.kind() == WouldBlock {
41 // Keep spinning.
42 thread::sleep(one_frame);
43 continue;
44 } else {
45 panic!("Error: {}", error);
46 }
47 }
48 };
49 let Frame::PixelBuffer(frame) = frame else {
50 return;
51 };
52 let buffer = frame.data();
53 println!("Captured data len: {}, Saving...", buffer.len());
54
55 // Flip the BGRA image into a RGBA image.
56
57 let mut bitflipped = Vec::with_capacity(w * h * 4);
58 let stride = buffer.len() / h;
59
60 for y in 0..h {
61 for x in 0..w {
62 let i = stride * y + 4 * x;
63 bitflipped.extend_from_slice(&[buffer[i + 2], buffer[i + 1], buffer[i], 255]);
64 }
65 }
66
67 // Save the image.
68
69 let name = format!("screenshot{}_1.png", i);
70 repng::encode(
71 File::create(name.clone()).unwrap(),
72 w as u32,
73 h as u32,
74 &bitflipped,
75 )
76 .unwrap();
77
78 println!("Image saved to `{}`.", name);
79 break;

Callers 1

mainFunction · 0.70

Calls 8

sleepFunction · 0.85
get_displayFunction · 0.70
widthMethod · 0.45
heightMethod · 0.45
frameMethod · 0.45
dataMethod · 0.45
cloneMethod · 0.45
strideMethod · 0.45

Tested by

no test coverage detected