pub struct Barcode { /* private fields */ }Expand description
An encoded barcode, ready to render.
This is the type most callers work with. It pairs an encoded Symbol
with convenience methods for the built-in renderers, while
Barcode::render stays open to any Renderer implementation.
§Examples
use smart_package_tracker::{Barcode, RenderOptions, TrackingId};
let id = TrackingId::generate()?;
let barcode = Barcode::code128(&id)?;
let options = RenderOptions::default();
barcode.to_png_file("label.png", &options)?;
barcode.to_svg_file("label.svg", &options)?;Implementations§
Source§impl Barcode
impl Barcode
Sourcepub fn code128(data: impl AsRef<str>) -> Result<Self>
Available on crate feature code128 only.
pub fn code128(data: impl AsRef<str>) -> Result<Self>
code128 only.Encode data as Code 128.
Accepts anything that borrows as a string, including TrackingId.
§Errors
Returns Error::EmptyPayload or
Error::Unencodable.
Sourcepub fn qr(data: impl AsRef<str>) -> Result<Self>
Available on crate feature qr only.
pub fn qr(data: impl AsRef<str>) -> Result<Self>
qr only.Encode data as a QR Code with default settings: medium error
correction, and the smallest version that fits.
Use Barcode::qr_with to choose the error correction level or pin the
version.
§Errors
Returns Error::EmptyPayload or
Error::Unencodable.
Sourcepub fn qr_with(encoder: Qr, data: impl AsRef<str>) -> Result<Self>
Available on crate feature qr only.
pub fn qr_with(encoder: Qr, data: impl AsRef<str>) -> Result<Self>
qr only.Encode data as a QR Code with an explicitly configured encoder.
§Examples
use smart_package_tracker::{Barcode, symbology::{Ecc, Qr, QrVersion}};
let barcode = Barcode::qr_with(
Qr::new().ecc(Ecc::High).version(QrVersion::Fixed(6)),
"PKG-9ED9285C",
)?;
assert_eq!(barcode.symbol().modules().width(), 41);§Errors
Returns Error::EmptyPayload or
Error::Unencodable.
Sourcepub fn from_symbol(symbol: Symbol) -> Self
pub fn from_symbol(symbol: Symbol) -> Self
Wrap an already-encoded symbol.
Sourcepub fn kind(&self) -> SymbologyKind
pub fn kind(&self) -> SymbologyKind
Which symbology encoded it.
Sourcepub fn render<R: Renderer>(
&self,
renderer: &R,
options: &RenderOptions,
) -> Result<R::Output>
pub fn render<R: Renderer>( &self, renderer: &R, options: &RenderOptions, ) -> Result<R::Output>
Sourcepub fn to_png(&self, options: &RenderOptions) -> Result<Vec<u8>>
Available on crate feature png only.
pub fn to_png(&self, options: &RenderOptions) -> Result<Vec<u8>>
png only.Sourcepub fn to_svg(&self, options: &RenderOptions) -> Result<String>
Available on crate feature svg only.
pub fn to_svg(&self, options: &RenderOptions) -> Result<String>
svg only.Sourcepub fn to_png_file(
&self,
path: impl AsRef<Path>,
options: &RenderOptions,
) -> Result<()>
Available on crate features png and std only.
pub fn to_png_file( &self, path: impl AsRef<Path>, options: &RenderOptions, ) -> Result<()>
png and std only.Sourcepub fn to_svg_file(
&self,
path: impl AsRef<Path>,
options: &RenderOptions,
) -> Result<()>
Available on crate features std and svg only.
pub fn to_svg_file( &self, path: impl AsRef<Path>, options: &RenderOptions, ) -> Result<()>
std and svg only.Sourcepub fn decode(&self) -> Result<String>
Available on crate feature code128 only.
pub fn decode(&self) -> Result<String>
code128 only.Decode the barcode back into its payload from the module grid.
This does not simply return the stored payload: it reconstructs bar and space runs from the rendered module pattern and decodes those. A successful round trip is therefore evidence that the encoded geometry is correct.
§Errors
Returns Error::Decode if this is not a
Code 128 barcode, or if the pattern is not valid Code 128. There is no
QR decoder in this crate; see the scan roadmap item.