diff --git a/web/src/main.rs b/web/src/main.rs index 6331355..57ad21b 100644 --- a/web/src/main.rs +++ b/web/src/main.rs @@ -19,6 +19,9 @@ struct QrParams { margin: u8, #[serde(default = "default_size")] size: u8, + /// fmt=svg 返回 SVG,否则返回 PNG + #[serde(default)] + fmt: String, } fn default_level() -> String { "M".into() } @@ -40,7 +43,7 @@ async fn index() -> Html<&'static str> { Html(include_str!("templates/index.html")) } -/// QR 码生成 API → PNG 图片 +/// QR 码生成 API → PNG 或 SVG async fn generate_qr(Query(params): Query) -> impl IntoResponse { let level = match parse_level(¶ms.level) { Ok(l) => l, @@ -58,12 +61,15 @@ async fn generate_qr(Query(params): Query) -> impl IntoResponse { Err(e) => return (StatusCode::BAD_REQUEST, e).into_response(), }; - let png = match qr.to_png_bytes(params.size) { - Ok(b) => b, - Err(e) => return (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), - }; - - ([(header::CONTENT_TYPE, "image/png")], png).into_response() + if params.fmt == "svg" { + let svg = qr.to_svg(); + ([(header::CONTENT_TYPE, "image/svg+xml")], svg).into_response() + } else { + match qr.to_png_bytes(params.size) { + Ok(b) => ([(header::CONTENT_TYPE, "image/png")], b).into_response(), + Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(), + } + } } #[tokio::main] diff --git a/web/src/templates/index.html b/web/src/templates/index.html index a002824..4684a35 100644 --- a/web/src/templates/index.html +++ b/web/src/templates/index.html @@ -6,117 +6,262 @@ QRGen Web -
-

QRGen Web

- -
- - - - + + +
QRGen
+ + +
+ + -
- - - 8px + + +
+
+ 输入内容生成 QR 码 + + +
+
-
- 输入内容生成 QR 码 -
-
-
- - + + +
+ +
+