capture_mag.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. extern crate repng;
  2. extern crate scrap;
  3. use scrap::Display;
  4. #[cfg(windows)]
  5. use scrap::{CapturerMag, TraitCapturer};
  6. #[cfg(windows)]
  7. use std::fs::File;
  8. fn main() {
  9. let n = Display::all().unwrap().len();
  10. for _i in 0..n {
  11. #[cfg(windows)]
  12. record(_i);
  13. }
  14. }
  15. #[cfg(windows)]
  16. fn get_display(i: usize) -> Display {
  17. Display::all().unwrap().remove(i)
  18. }
  19. #[cfg(windows)]
  20. fn record(i: usize) {
  21. use std::time::Duration;
  22. use scrap::{Frame, TraitPixelBuffer};
  23. for d in Display::all().unwrap() {
  24. println!("{:?} {} {}", d.origin(), d.width(), d.height());
  25. }
  26. let display = get_display(i);
  27. let (w, h) = (display.width(), display.height());
  28. {
  29. let mut capture_mag = CapturerMag::new(display.origin(), display.width(), display.height())
  30. .expect("Couldn't begin capture.");
  31. let wnd_cls = "";
  32. let wnd_name = "RustDeskPrivacyWindow";
  33. if false == capture_mag.exclude(wnd_cls, wnd_name).unwrap() {
  34. println!("No window found for cls {} name {}", wnd_cls, wnd_name);
  35. } else {
  36. println!("Filter window for cls {} name {}", wnd_cls, wnd_name);
  37. }
  38. let frame = capture_mag.frame(Duration::from_millis(0)).unwrap();
  39. let Frame::PixelBuffer(frame) = frame else {
  40. return;
  41. };
  42. let frame = frame.data();
  43. println!("Capture data len: {}, Saving...", frame.len());
  44. let mut bitflipped = Vec::with_capacity(w * h * 4);
  45. let stride = frame.len() / h;
  46. for y in 0..h {
  47. for x in 0..w {
  48. let i = stride * y + 4 * x;
  49. bitflipped.extend_from_slice(&[frame[i + 2], frame[i + 1], frame[i], 255]);
  50. }
  51. }
  52. // Save the image.
  53. let name = format!("capture_mag_{}_1.png", i);
  54. repng::encode(
  55. File::create(name.clone()).unwrap(),
  56. w as u32,
  57. h as u32,
  58. &bitflipped,
  59. )
  60. .unwrap();
  61. println!("Image saved to `{}`.", name);
  62. }
  63. {
  64. let mut capture_mag = CapturerMag::new(display.origin(), display.width(), display.height())
  65. .expect("Couldn't begin capture.");
  66. let wnd_cls = "";
  67. let wnd_title = "RustDeskPrivacyWindow";
  68. if false == capture_mag.exclude(wnd_cls, wnd_title).unwrap() {
  69. println!("No window found for cls {} title {}", wnd_cls, wnd_title);
  70. } else {
  71. println!("Filter window for cls {} title {}", wnd_cls, wnd_title);
  72. }
  73. let frame = capture_mag.frame(Duration::from_millis(0)).unwrap();
  74. let Frame::PixelBuffer(frame) = frame else {
  75. return;
  76. };
  77. println!("Capture data len: {}, Saving...", frame.data().len());
  78. let mut raw = Vec::new();
  79. unsafe {
  80. scrap::ARGBToRAW(
  81. frame.data().as_ptr(),
  82. frame.stride()[0] as _,
  83. (&mut raw).as_mut_ptr(),
  84. (w * 3) as _,
  85. w as _,
  86. h as _,
  87. )
  88. };
  89. let mut bitflipped = Vec::with_capacity(w * h * 4);
  90. let stride = raw.len() / h;
  91. for y in 0..h {
  92. for x in 0..w {
  93. let i = stride * y + 3 * x;
  94. bitflipped.extend_from_slice(&[raw[i], raw[i + 1], raw[i + 2], 255]);
  95. }
  96. }
  97. let name = format!("capture_mag_{}_2.png", i);
  98. repng::encode(
  99. File::create(name.clone()).unwrap(),
  100. w as u32,
  101. h as u32,
  102. &bitflipped,
  103. )
  104. .unwrap();
  105. println!("Image saved to `{}`.", name);
  106. }
  107. }