pub struct ImageEncoder { /* private fields */ }展开描述
将原始图像缓冲区编码为常见格式的编码字节。
在输入颜色格式兼容时,支持保存为 PNG、JPEG、GIF、TIFF、BMP 和 JPEG XR。
§Example
use windows_capture::encoder::{ImageEncoder, ImageEncoderPixelFormat, ImageFormat};
let width = 320u32;
let height = 240u32;
// BGRA8 缓冲区(例如来自一帧)
let bgra = vec![0u8; (width * height * 4) as usize];
let png_bytes = ImageEncoder::new(ImageFormat::Png, ImageEncoderPixelFormat::Bgra8)
.unwrap()
.encode(&bgra, width, height)
.unwrap();
std::fs::write("example.png", png_bytes).unwrap();实现§
源代码§impl ImageEncoder
impl ImageEncoder
源代码pub fn new(
format: ImageFormat,
pixel_format: ImageEncoderPixelFormat,
) -> Result<Self, ImageEncoderError>
pub fn new( format: ImageFormat, pixel_format: ImageEncoderPixelFormat, ) -> Result<Self, ImageEncoderError>
构造一个新的 ImageEncoder。
源代码pub fn encode(
&self,
image_buffer: &[u8],
width: u32,
height: u32,
) -> Result<Vec<u8>, ImageEncoderError>
pub fn encode( &self, image_buffer: &[u8], width: u32, height: u32, ) -> Result<Vec<u8>, ImageEncoderError>
将所提供的像素缓冲区编码为配置好的输出 ImageFormat。
输入缓冲区必须与所指定的源 crate::settings::ColorFormat
和尺寸相匹配。对于打包的 8 位格式(例如 crate::settings::ColorFormat::Bgra8),
缓冲区长度应为 width * height * 4。
§Errors
- 当源格式不支持用于图像编码时
(例如
crate::settings::ColorFormat::Rgba16F),返回ImageEncoderError::UnsupportedFormat - 当 Windows Imaging API 调用失败时,返回
ImageEncoderError::WindowsError - 整数转换失败时,返回
ImageEncoderError::IntConversionError
自动 Trait 实现§
impl Freeze for ImageEncoder
impl RefUnwindSafe for ImageEncoder
impl Send for ImageEncoder
impl Sync for ImageEncoder
impl Unpin for ImageEncoder
impl UnsafeUnpin for ImageEncoder
impl UnwindSafe for ImageEncoder
通用实现§
源代码§impl<T> IntoEither for T
impl<T> IntoEither for T
源代码§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
将
self 转换为 Left 变体,如果 Either<Self, Self>
当 into_left is true.
Converts self into a Right 变体,如果 Either<Self, Self>
否则。 阅读更多源代码§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
将
self 转换为 Left 变体,如果 Either<Self, Self>
当 into_left(&self) returns true.
Converts self into a Right 变体,如果 Either<Self, Self>
否则。 阅读更多