screenshot.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. extern crate repng;
  2. extern crate scrap;
  3. use std::fs::File;
  4. use std::io::ErrorKind::WouldBlock;
  5. use std::thread;
  6. use std::time::Duration;
  7. use scrap::{Capturer, Display, Frame, TraitCapturer, TraitPixelBuffer};
  8. fn main() {
  9. let n = Display::all().unwrap().len();
  10. for i in 0..n {
  11. record(i);
  12. }
  13. }
  14. fn get_display(i: usize) -> Display {
  15. Display::all().unwrap().remove(i)
  16. }
  17. fn record(i: usize) {
  18. let one_second = Duration::new(1, 0);
  19. let one_frame = one_second / 60;
  20. for d in Display::all().unwrap() {
  21. println!("{:?} {} {}", d.origin(), d.width(), d.height());
  22. }
  23. let display = get_display(i);
  24. let mut capturer = Capturer::new(display).expect("Couldn't begin capture.");
  25. let (w, h) = (capturer.width(), capturer.height());
  26. loop {
  27. // Wait until there's a frame.
  28. let frame = match capturer.frame(Duration::from_millis(0)) {
  29. Ok(frame) => frame,
  30. Err(error) => {
  31. if error.kind() == WouldBlock {
  32. // Keep spinning.
  33. thread::sleep(one_frame);
  34. continue;
  35. } else {
  36. panic!("Error: {}", error);
  37. }
  38. }
  39. };
  40. let Frame::PixelBuffer(frame) = frame else {
  41. return;
  42. };
  43. let buffer = frame.data();
  44. println!("Captured data len: {}, Saving...", buffer.len());
  45. // Flip the BGRA image into a RGBA image.
  46. let mut bitflipped = Vec::with_capacity(w * h * 4);
  47. let stride = buffer.len() / h;
  48. for y in 0..h {
  49. for x in 0..w {
  50. let i = stride * y + 4 * x;
  51. bitflipped.extend_from_slice(&[buffer[i + 2], buffer[i + 1], buffer[i], 255]);
  52. }
  53. }
  54. // Save the image.
  55. let name = format!("screenshot{}_1.png", i);
  56. repng::encode(
  57. File::create(name.clone()).unwrap(),
  58. w as u32,
  59. h as u32,
  60. &bitflipped,
  61. )
  62. .unwrap();
  63. println!("Image saved to `{}`.", name);
  64. break;
  65. }
  66. drop(capturer);
  67. let display = get_display(i);
  68. let mut capturer = Capturer::new(display).expect("Couldn't begin capture.");
  69. let (w, h) = (capturer.width(), capturer.height());
  70. loop {
  71. // Wait until there's a frame.
  72. let frame = match capturer.frame(Duration::from_millis(0)) {
  73. Ok(frame) => frame,
  74. Err(error) => {
  75. if error.kind() == WouldBlock {
  76. // Keep spinning.
  77. thread::sleep(one_frame);
  78. continue;
  79. } else {
  80. panic!("Error: {}", error);
  81. }
  82. }
  83. };
  84. let Frame::PixelBuffer(frame) = frame else {
  85. return;
  86. };
  87. let buffer = frame.data();
  88. println!("Captured data len: {}, Saving...", buffer.len());
  89. let mut raw = Vec::new();
  90. unsafe {
  91. scrap::ARGBToRAW(
  92. buffer.as_ptr(),
  93. frame.stride()[0] as _,
  94. (&mut raw).as_mut_ptr(),
  95. (w * 3) as _,
  96. w as _,
  97. h as _,
  98. )
  99. };
  100. let mut bitflipped = Vec::with_capacity(w * h * 4);
  101. let stride = raw.len() / h;
  102. for y in 0..h {
  103. for x in 0..w {
  104. let i = stride * y + 3 * x;
  105. bitflipped.extend_from_slice(&[raw[i], raw[i + 1], raw[i + 2], 255]);
  106. }
  107. }
  108. let name = format!("screenshot{}_2.png", i);
  109. repng::encode(
  110. File::create(name.clone()).unwrap(),
  111. w as u32,
  112. h as u32,
  113. &bitflipped,
  114. )
  115. .unwrap();
  116. println!("Image saved to `{}`.", name);
  117. break;
  118. }
  119. }