我在一个用Rust编写的项目中工作,这个项目基本上模拟了一台打印机。我获得打印机输入,并尝试将其转换为人类可读的数据(字符串、图像)。因此,为了再现二维码,我将打印机输入转换为BitMap切片(我创建了一个实现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
}
}我的问题是,如何将PNG值转换为BitMapImageSlice编码的图像?
发布于 2021-11-22 16:20:40
当试图通过image板条箱实现自己的通用图像时,有一个关键问题:不支持每像素1位的颜色类型,即使使用适配层,它也将与可用的API不兼容。
write_to方法只存在于DynamicImage,它被定义为内置实现的枚举。不能将其扩展为考虑孤立implementations.save_buffer_with_format (建议的here)根据支持的颜色类型需要像素样本的缓冲区。ImageEncoder中声明的用于写入编码内容的裸图像编码器签名也需要遵循支持的颜色类型的像素的缓冲区。因此,在这里使用受支持的图像类型更为简单。将位图转换为L8颜色类型,并使用其中的现有函数。
impl BitmapImageSlice {
pub fn to_image(&self) -> ImageBuffer<Luma<u8>, Vec<u8>> {
// NOTE: this depends on the BitmapImageSlice data layout,
// adjust vector construction accordingly
let data: Vec<u8> = self.buffer.iter()
.flat_map(|b| [
b >> 7,
(b >> 6) & 1,
(b >> 5) & 1,
(b >> 4) & 1,
(b >> 3) & 1,
(b >> 2) & 1,
(b >> 1) & 1,
b & 1,
])
.map(|p| p * 0xFF)
.collect();
ImageBuffer::from_vec(self.width, self.height, data).unwrap()
}
}
fn save(bitmap: &BitmapImageSlice) -> image::error::ImageResult<()> {
let img = bitmap.to_image();
image::save_buffer_with_format(
"out.png",
img.as_raw(),
bitmap.width,
bitmap.height,
ColorType::L8,
ImageFormat::Png,
)?;
Ok(())
}https://stackoverflow.com/questions/70065519
复制相似问题