Skip to content

Commit

Permalink
first full commit✨
Browse files Browse the repository at this point in the history
  • Loading branch information
0x002500 committed Aug 28, 2023
0 parents commit d77b8f6
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
104 changes: 104 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "getpic"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
captrs = "0.3.1"
shuteye = "0.3.3"
winapi = "0.3.9"
90 changes: 90 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use captrs::*;
use shuteye::sleep;
use std::time::Duration;
use winapi::um::wingdi::{BITMAPFILEHEADER, BITMAPINFOHEADER};

pub fn serialize_row<T: Sized>(src: &T) -> &[u8] {
unsafe {
::std::slice::from_raw_parts((src as *const T) as *const u8, ::std::mem::size_of::<T>())
}
}

fn main() {
let mut capturer = Capturer::new(0).unwrap();

let (w, h) = capturer.geometry();

for i in 1..10 {
let ps = capturer.capture_frame();
if ps.is_err() {
println!("{:?}", ps.err());
continue;
}
let ps = ps.unwrap();

let mut buf = vec![];

// 因为图片是倒着的,要水平翻转一下
for y in (0..h).rev() {
for x in 0..w {
let Bgr8 { r, g, b, a } = ps[(y * w + x) as usize];
buf.push(b);
buf.push(g);
buf.push(r);
// buf.push(a);
}
}

save_to_file(
format!("{}.bmp", i).as_str(),
&buf[..],
(w) as i32,
(h) as i32,
);

sleep(Duration::from_millis(1000 / 60));
}
}

fn save_to_file(file: &str, rgba: &[u8], w: i32, h: i32) {
let mut data: Vec<u8> = vec![];

let bif = BITMAPFILEHEADER {
bfType: ('B' as u16) | (('M' as u16) << 8),
bfOffBits: 54,
bfReserved1: 0,
bfReserved2: 0,
bfSize: (w * h * 3 + 54) as u32,
};

for v in serialize_row(&bif) {
data.push(*v);
}
let bii = BITMAPINFOHEADER {
biBitCount: 24,
biSize: 40,
biWidth: w,
biHeight: h,
biPlanes: 1,
biCompression: 0,
biSizeImage: (w * h * 3) as u32,
biClrImportant: 0,
biClrUsed: 0,
biXPelsPerMeter: 0,
biYPelsPerMeter: 0,
};

for v in serialize_row(&bii) {
data.push(*v);
}

for v in rgba {
data.push(*v);
}

use std::fs::File;
use std::io::Write;

let mut file = File::create(file).expect("create failed");
file.write_all(&data[..]).expect("write failed");
}

0 comments on commit d77b8f6

Please sign in to comment.