feat: 更换用于前端显示的图片格式。

This commit is contained in:
2023-01-02 20:59:32 +08:00
parent 366b137258
commit 9030d48e21
8 changed files with 498 additions and 421 deletions

521
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -33,6 +33,7 @@ time = { version = "0.3.17", features = ["formatting"] }
color_space = "0.5.3"
futures = "0.3.25"
either = "1.8.0"
image = "0.24.5"
[features]
# by default Tauri runs in production mode

View File

@ -46,7 +46,6 @@ async fn get_screenshot_by_config(config: DisplayConfig) -> Result<ScreenshotDto
info!("截图耗时 {} s", start.elapsed().as_seconds_f32());
match screenshot_dto {
Ok(screenshot_dto) => {
info!("截图耗时 {} s", start.elapsed().as_seconds_f32());
Ok(screenshot_dto)
}
Err(error) => {

View File

@ -1,13 +1,10 @@
use image::ImageBuffer;
use image::{ImageOutputFormat, Rgb};
use paris::error;
use serde::{Deserialize, Serialize};
use std::iter;
use color_space::{Hsv, Rgb};
use either::Either;
use serde::{Deserialize, Serialize};
use super::{
config::{DisplayConfig, LedStripConfig},
led_color::LedColor,
};
use super::{config::DisplayConfig, led_color::LedColor};
type Point = (usize, usize);
type LedSamplePoints = Vec<Point>;
@ -233,24 +230,38 @@ impl Screenshot {
pub async fn to_webp_base64(&self) -> String {
let bitmap = &self.bitmap;
let mut bitflipped =
Vec::with_capacity(self.config.display_width * self.config.display_height * 3);
let stride = bitmap.len() / self.config.display_height;
for y in 0..self.config.display_height {
for x in 0..self.config.display_width {
let i = stride * y + 4 * x;
bitflipped.extend_from_slice(&[bitmap[i + 2], bitmap[i + 1], bitmap[i]]);
let mut image_buffer = ImageBuffer::new(
self.config.display_width as u32 / 3,
self.config.display_height as u32 / 3,
);
for y in 0..self.config.display_height / 3 {
for x in 0..self.config.display_width / 3 {
let i = stride * y * 3 + 4 * x * 3;
image_buffer.put_pixel(
x as u32,
y as u32,
Rgb::<u8>([bitmap[i + 2], bitmap[i + 1], bitmap[i]]),
);
}
}
let webp_memory = webp::Encoder::from_rgb(
bitflipped.as_slice(),
self.config.display_width as u32,
self.config.display_height as u32,
)
.encode(100.0);
return base64::encode(&*webp_memory);
// let webp_memory =
// webp::Encoder::from_rgb(bitflipped.as_slice(), size_x, size_y).encode(50.0);
// return base64::encode(&*webp_memory);
let mut cursor = std::io::Cursor::new(vec![]);
match image_buffer.write_to(&mut cursor, ImageOutputFormat::Tiff) {
Ok(_) => {
return base64::encode(cursor.into_inner());
}
Err(err) => {
error!("can not encode image. {:?}", err);
return String::from("");
}
}
}
pub async fn to_dto(&self) -> ScreenshotDto {