【发布时间】:2022-01-01 00:55:55
【问题描述】:
我在一个用 Rust 编写的项目工作,该项目基本上模拟了一台打印机。我得到打印机输入,并尝试将其转换为人类可读的数据(字符串、图像)。因此,为了重现 QR 码,我将打印机输入转换为位图切片(我创建了一个实现 GenericImageView 的结构)。
BitmapImageSlice {
height,
width,
buffer: data
}
impl GenericImageView for BitmapImageSlice {
type Pixel = Rgb<u8>;
type InnerImageView = BitmapImageSlice;
fn dimensions(&self) -> (u32, u32) {
(self.width as u32, self.height as u32)
}
fn bounds(&self) -> (u32, u32, u32, u32) {
( 0, 0, self.width as u32, self.height as u32)
}
fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
let byte = self.buffer[x as usize];
let bit_position = 7-y;
let bit = 1 << bit_position;
if (byte & bit as u8) > 0{
Rgb([0, 0, 0])
}
else {
Rgb([255, 255, 255])
}
}
fn inner(&self) -> &Self::InnerImageView {
self
}
}
我的问题是,如何将BitMapImageSlice 值转换为以 PNG 编码的图像?
【问题讨论】:
-
@E_net4thevoter:这适用于 1bpp 图像和 24bpp 图像吗?我猜想一些与
image::RGBA(8)不同的参数,大概很容易在文档中查找image板条箱记录在哪里?还是会使用这个定义的get_pixel访问器函数? -
docs.rs/image/0.23.14/image/codecs/png/struct.PngEncoder.html 或许你可以看看这个函数的实现?
标签: rust rust-cargo low-level rust-crates