pub struct QrCodeGenerator { /* private fields */ }Expand description
二维码生成器 — 对齐 PHP Endroid\QrCode\QrCode
持有 QrCodeConfig 配置,提供 PNG/SVG/矩阵三种输出方式。
§PHP 对齐
// PHP endroid/qr-code
$qr = new QrCode('Hello');
$qr->setSize(200);
$qr->setMargin(10);
$qr->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::MEDIUM));
$png = $qr->writeString(); // 默认 PNG§Rust 用法
ⓘ
use sz_rust_core::qr_code::{QrCodeGenerator, QrCodeConfig, ErrorCorrectionLevel};
let config = QrCodeConfig::new()
.with_size(200)
.with_margin(10)
.with_error_correction_level(ErrorCorrectionLevel::Medium);
let generator = QrCodeGenerator::with_config(config);
let png = generator.generate_png("Hello").unwrap();Implementations§
Source§impl QrCodeGenerator
impl QrCodeGenerator
Sourcepub fn with_config(config: QrCodeConfig) -> Self
pub fn with_config(config: QrCodeConfig) -> Self
使用指定配置创建生成器
Sourcepub fn config(&self) -> &QrCodeConfig
pub fn config(&self) -> &QrCodeConfig
获取配置引用
Sourcepub fn generate_matrix(&self, data: &str) -> Result<Vec<Vec<bool>>, QrCodeError>
pub fn generate_matrix(&self, data: &str) -> Result<Vec<Vec<bool>>, QrCodeError>
生成原始矩阵 — 返回 bool 二维数组(true = 深色模块)
矩阵尺寸为 N × N(正方形),不包含边距。
§参数
data: 待编码的数据(不能为空)
§返回
成功返回 Vec<Vec<bool>>,失败返回 QrCodeError。
Sourcepub fn generate_png(&self, data: &str) -> Result<Vec<u8>, QrCodeError>
pub fn generate_png(&self, data: &str) -> Result<Vec<u8>, QrCodeError>
生成 PNG 二进制
先生成原始矩阵,再用 image crate 渲染为 PNG。
像素级边距控制:size 为总尺寸(含边距),margin 为四周白边宽度。
§参数
data: 待编码的数据(不能为空)
§返回
成功返回 PNG 字节流 Vec<u8>,失败返回 QrCodeError。
Sourcepub fn generate_svg(&self, data: &str) -> Result<String, QrCodeError>
pub fn generate_svg(&self, data: &str) -> Result<String, QrCodeError>
生成 SVG 字符串
使用 qrcode crate 内置的 SVG 渲染器(qrcode::render::svg::Color)。
边距通过 quiet zone 控制(margin > 0 时启用)。
§参数
data: 待编码的数据(不能为空)
§返回
成功返回 SVG 字符串,失败返回 QrCodeError。
Trait Implementations§
Source§impl Clone for QrCodeGenerator
impl Clone for QrCodeGenerator
Source§fn clone(&self) -> QrCodeGenerator
fn clone(&self) -> QrCodeGenerator
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for QrCodeGenerator
impl Debug for QrCodeGenerator
Auto Trait Implementations§
impl Freeze for QrCodeGenerator
impl RefUnwindSafe for QrCodeGenerator
impl Send for QrCodeGenerator
impl Sync for QrCodeGenerator
impl Unpin for QrCodeGenerator
impl UnsafeUnpin for QrCodeGenerator
impl UnwindSafe for QrCodeGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.