Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/macos/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,15 @@ pub fn capture(
RgbaImage::from_raw(width as u32, height as u32, buffer)
.ok_or_else(|| XCapError::new("RgbaImage::from_raw failed"))
}

pub fn capture_bytes(
cg_rect: CGRect,
list_option: CGWindowListOption,
window_id: CGWindowID,
) -> XCapResult<Vec<u8>> {
let cg_image = create_image(cg_rect, list_option, window_id, kCGWindowImageDefault)
.ok_or_else(|| XCapError::new(format!("Capture failed {} {:?}", window_id, cg_rect)))?;

let bytes = Vec::from(cg_image.data().bytes());
Ok(bytes)
}
10 changes: 9 additions & 1 deletion src/macos/impl_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use image::RgbaImage;

use crate::error::{XCapError, XCapResult};

use super::capture::capture;
use super::capture::{capture, capture_bytes};

#[derive(Debug, Clone)]
pub(crate) struct ImplMonitor {
Expand Down Expand Up @@ -132,4 +132,12 @@ impl ImplMonitor {
kCGNullWindowID,
)
}

pub fn capture_bytes(&self) -> XCapResult<Vec<u8>> {
capture_bytes(
self.cg_display.bounds(),
kCGWindowListOptionAll,
kCGNullWindowID,
)
}
}
4 changes: 4 additions & 0 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ impl Monitor {
pub fn capture_image(&self) -> XCapResult<RgbaImage> {
self.impl_monitor.capture_image()
}

pub fn capture_bytes(&self) -> XCapResult<Vec<u8>> {
self.impl_monitor.capture_bytes()
}
}