feat: 格式扩展 — 支持 BMP/JPEG/WebP 输出

- png.rs 重命名为 image.rs,新增 OutputFormat 枚举
- QrCode::to_image_bytes 支持 PNG/BMP/JPEG/WebP
- CLI 新增 -f/--format 参数(png/bmp/jpeg/webp)
- Web API fmt 参数扩展至全部 4 种图像格式
- core/Cargo.toml: image crate 新增 bmp feature
This commit is contained in:
2026-06-19 21:34:21 +08:00
parent ef6b092eda
commit b41f6ee7df
6 changed files with 115 additions and 41 deletions
+7 -6
View File
@@ -70,12 +70,13 @@ async fn generate_qr(Query(params): Query<QrParams>) -> impl IntoResponse {
if params.fmt == "svg" {
let svg = qr.to_svg(None);
([(header::CONTENT_TYPE, "image/svg+xml")], svg).into_response()
} else {
match qr.to_png_bytes(params.size, None) {
Ok(b) => ([(header::CONTENT_TYPE, "image/png")], b).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
return ([(header::CONTENT_TYPE, "image/svg+xml")], svg).into_response();
}
let fmt = qr_core::render::image::OutputFormat::from_ext(&params.fmt)
.unwrap_or(qr_core::render::image::OutputFormat::Png);
match qr.to_image_bytes(params.size, None, Some(fmt)) {
Ok(b) => ([(header::CONTENT_TYPE, fmt.mime())], b).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}