pub struct Renderer<'a, P: Pixel> { /* private fields */ }Expand description
A QR code renderer. This is a builder type which converts a bool-vector into an image.
Implementations§
Source§impl<'a, P: Pixel> Renderer<'a, P>
impl<'a, P: Pixel> Renderer<'a, P>
Sourcepub fn from_source<C>(source: &'a C, quiet_zone: u32) -> Renderer<'a, P>where
C: ModuleSource + ?Sized,
pub fn from_source<C>(source: &'a C, quiet_zone: u32) -> Renderer<'a, P>where
C: ModuleSource + ?Sized,
Creates a new renderer from a module-grid source.
This is the read-only-source counterpart to Renderer::new. It is
useful when rendering a borrowed view that implements
qrcode_core::ModuleSource but does not expose facade-specific QR code
methods.
§Panics
Panics if source is not square or if its row-major module slice length
does not match width() * height().
Sourcepub fn from_symbol<S>(symbol: &'a S) -> Renderer<'a, P>
pub fn from_symbol<S>(symbol: &'a S) -> Renderer<'a, P>
Creates a new renderer from a QR symbol.
This is the metadata-aware counterpart to Renderer::from_source.
The quiet zone is inferred from qrcode_core::QrSymbol::quiet_zone.
§Panics
Panics if symbol exposes an invalid module grid.
Sourcepub fn try_from_source<C>(
source: &'a C,
quiet_zone: u32,
) -> Result<Renderer<'a, P>, RenderError>where
C: ModuleSource + ?Sized,
pub fn try_from_source<C>(
source: &'a C,
quiet_zone: u32,
) -> Result<Renderer<'a, P>, RenderError>where
C: ModuleSource + ?Sized,
Tries to create a new renderer from a module-grid source.
Unlike Renderer::from_source, this constructor reports malformed
sources as RenderError instead of panicking.
§Errors
Returns RenderError::InvalidModuleSource when source is empty,
non-square, or its row-major module slice length does not match
width() * height(). Returns RenderError::ModuleSourceTooWide when
the width cannot be represented by this renderer.
Sourcepub fn try_from_symbol<S>(symbol: &'a S) -> Result<Renderer<'a, P>, RenderError>
pub fn try_from_symbol<S>(symbol: &'a S) -> Result<Renderer<'a, P>, RenderError>
Tries to create a new renderer from a QR symbol.
This is the fallible, metadata-aware counterpart to
Renderer::try_from_source. The quiet zone is inferred from
qrcode_core::QrSymbol::quiet_zone.
§Errors
Returns the same errors as Renderer::try_from_source when the symbol
exposes an invalid module grid.
Sourcepub fn dark_color(&mut self, color: P) -> &mut Self
pub fn dark_color(&mut self, color: P) -> &mut Self
Sets color of a dark module. Default is opaque black.
Sourcepub fn light_color(&mut self, color: P) -> &mut Self
pub fn light_color(&mut self, color: P) -> &mut Self
Sets color of a light module. Default is opaque white.
Sourcepub fn quiet_zone(&mut self, has_quiet_zone: bool) -> &mut Self
pub fn quiet_zone(&mut self, has_quiet_zone: bool) -> &mut Self
Whether to include the quiet zone in the generated image.
Sourcepub fn module_dimensions(&mut self, width: u32, height: u32) -> &mut Self
pub fn module_dimensions(&mut self, width: u32, height: u32) -> &mut Self
Sets the size of each module in pixels. Default is 8×8.
Sourcepub fn min_dimensions(&mut self, width: u32, height: u32) -> &mut Self
pub fn min_dimensions(&mut self, width: u32, height: u32) -> &mut Self
Sets the minimum total image size in pixels, including the quiet zone if applicable. The renderer will try to find the dimension as small as possible, such that each module in the QR code has uniform size (no distortion).
For instance, a version 1 QR code has 19 modules across including the quiet zone. If we request an image of size ≥200×200, we get that each module’s size should be 11×11, so the actual image size will be 209×209.
Sourcepub fn max_dimensions(&mut self, width: u32, height: u32) -> &mut Self
pub fn max_dimensions(&mut self, width: u32, height: u32) -> &mut Self
Sets the maximum total image size in pixels, including the quiet zone if applicable. The renderer will try to find the dimension as large as possible, such that each module in the QR code has uniform size (no distortion).
For instance, a version 1 QR code has 19 modules across including the quiet zone. If we request an image of size ≤200×200, we get that each module’s size should be 10×10, so the actual image size will be 190×190.
The module size is at least 1×1, so if the restriction is too small, the final image can be larger than the input.
Sourcepub fn for_web(&mut self) -> &mut Self
pub fn for_web(&mut self) -> &mut Self
Sets dimensions suitable for web display (200×200 pixels minimum).
This is a convenience preset for embedding QR codes in web pages. The actual size may be slightly larger to maintain uniform module sizing.
Sourcepub fn for_print(&mut self, dpi: u32) -> &mut Self
pub fn for_print(&mut self, dpi: u32) -> &mut Self
Sets dimensions suitable for printing at the specified DPI.
Targets a 1-inch × 1-inch physical size. For example, at 300 DPI the image will be at least 300×300 pixels.
§Arguments
dpi- Dots per inch (common values: 150 for draft, 300 for standard, 600 for high quality)
Sets dimensions suitable for social media platform sharing.
Targets platform-recommended sizes:
| Platform | Size (px) |
|---|---|
"twitter" | 400×400 |
"facebook" | 600×600 |
"instagram" | 1080×1080 |
"wechat" | 600×600 |
| Any other | 400×400 |
Source§impl<'a, P: StyledPixel> Renderer<'a, P>
impl<'a, P: StyledPixel> Renderer<'a, P>
Sourcepub fn template<T: RenderTemplate>(self, tmpl: &T) -> Self
pub fn template<T: RenderTemplate>(self, tmpl: &T) -> Self
Applies a render template: dark/light colors (via
StyledPixel::from_hex), optional module size, and the quiet-zone
setting.