Skip to main content

Module qr_code

Module qr_code 

Source
Expand description

二维码生成模块 — 对齐 PHP endroid/qr-code

提供二维码生成功能,支持 PNG/SVG 输出和原始矩阵获取。

§PHP 对齐

§核心 API 映射

PHP 类Rust 结构说明
Endroid\QrCode\QrCodeQrCodeGenerator二维码生成器
Endroid\QrCode\QrCode::setSize()QrCodeConfig::with_size设置尺寸
Endroid\QrCode\QrCode::setMargin()QrCodeConfig::with_margin设置边距
Endroid\QrCode\QrCode::setForegroundColor()QrCodeConfig::with_foreground_color前景色
Endroid\QrCode\QrCode::setBackgroundColor()QrCodeConfig::with_background_color背景色
Endroid\QrCode\QrCode::setErrorCorrectionLevel()QrCodeConfig::with_error_correction_level容错级别
Endroid\QrCode\ErrorCorrectionLevelErrorCorrectionLevel容错级别枚举
Endroid\QrCode\QrCode::writeString() (PNG)QrCodeGenerator::generate_png生成 PNG
Endroid\QrCode\QrCode::writeString() (SVG)QrCodeGenerator::generate_svg生成 SVG

§PHP 行为对齐

  • 容错级别:PHP 支持 Low(7%)/Medium(15%)/Quartile(25%)/High(30%),Rust 通过 ErrorCorrectionLevel 表达。
  • 尺寸与边距:PHP setSize(size) + setMargin(margin),Rust 通过 QrCodeConfigsize/margin 字段表达。
  • 前景/背景色:PHP 使用 RGBA 数组,Rust 简化为 RGB [u8; 3]
  • PNG 输出:先从 qrcode crate 获取矩阵,再用 image crate 渲染为 PNG(像素级边距控制)。
  • SVG 输出:直接使用 qrcode crate 的 SVG 渲染器(qrcode::render::svg::Color)。

§Rust 用法

use sz_rust_core::qr_code::{QrCodeGenerator, QrCodeConfig, ErrorCorrectionLevel};

// 默认配置
let generator = QrCodeGenerator::new();
let png_bytes = generator.generate_png("https://example.com").unwrap();
let svg_string = generator.generate_svg("https://example.com").unwrap();

// 自定义配置
let config = QrCodeConfig::new()
    .with_size(300)
    .with_margin(20)
    .with_foreground_color([0, 0, 255])
    .with_error_correction_level(ErrorCorrectionLevel::High);
let generator = QrCodeGenerator::with_config(config);
let png_bytes = generator.generate_png("Hello").unwrap();

Structs§

QrCodeConfig
二维码配置 — 对齐 PHP Endroid\QrCode\QrCode
QrCodeGenerator
二维码生成器 — 对齐 PHP Endroid\QrCode\QrCode

Enums§

ErrorCorrectionLevel
二维码容错级别 — 对齐 PHP Endroid\QrCode\ErrorCorrectionLevel
QrCodeError
二维码生成错误 — 对齐 PHP endroid\qr-code 异常