pub struct QrCode { /* private fields */ }Expand description
The encoded QR code symbol.
Implementations§
Source§impl QrCode
impl QrCode
Sourcepub fn new<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
pub fn new<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
Constructs a new QR code which automatically encodes the given data.
This method uses the “medium” error correction level and automatically chooses the smallest QR code.
use qrcode_rs::QrCode;
let code = QrCode::new(b"Some data").unwrap();§Errors
Returns error if the QR code cannot be constructed, e.g. when the data is too long.
Sourcepub fn with_error_correction_level<D: AsRef<[u8]>>(
data: D,
ec_level: EcLevel,
) -> QrResult<Self>
pub fn with_error_correction_level<D: AsRef<[u8]>>( data: D, ec_level: EcLevel, ) -> QrResult<Self>
Constructs a new QR code which automatically encodes the given data at a specific error correction level.
This method automatically chooses the smallest QR code.
use qrcode_rs::{QrCode, EcLevel};
let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap();§Errors
Returns error if the QR code cannot be constructed, e.g. when the data is too long.
Sourcepub fn new_micro<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
pub fn new_micro<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
Constructs a new Micro QR code which automatically encodes the given data.
This method uses the “medium” error correction level and automatically chooses the smallest Micro QR code.
use qrcode_rs::QrCode;
let code = QrCode::new_micro(b"123").unwrap();§Errors
Returns error if the data cannot be encoded as a Micro QR code, e.g. when the data is too long.
Sourcepub fn micro_with_error_correction_level<D: AsRef<[u8]>>(
data: D,
ec_level: EcLevel,
) -> QrResult<Self>
pub fn micro_with_error_correction_level<D: AsRef<[u8]>>( data: D, ec_level: EcLevel, ) -> QrResult<Self>
Constructs a new Micro QR code which automatically encodes the given data at a specific error correction level.
This method automatically chooses the smallest Micro QR code.
use qrcode_rs::{QrCode, EcLevel};
let code = QrCode::micro_with_error_correction_level(b"123", EcLevel::L).unwrap();§Errors
Returns error if the data cannot be encoded as a Micro QR code, e.g. when the data is too long, or when the error correction level is not supported by any Micro QR version.
Sourcepub fn with_version<D: AsRef<[u8]>>(
data: D,
version: Version,
ec_level: EcLevel,
) -> QrResult<Self>
pub fn with_version<D: AsRef<[u8]>>( data: D, version: Version, ec_level: EcLevel, ) -> QrResult<Self>
Constructs a new QR code for the given version and error correction level.
use qrcode_rs::{QrCode, Version, EcLevel};
let code = QrCode::with_version(b"Some data", Version::Normal(5), EcLevel::M).unwrap();This method can also be used to generate Micro QR code.
use qrcode_rs::{QrCode, Version, EcLevel};
let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap();§Errors
Returns error if the QR code cannot be constructed, e.g. when the data is too long, or when the version and error correction level are incompatible.
Sourcepub fn with_bits(bits: Bits, ec_level: EcLevel) -> QrResult<Self>
pub fn with_bits(bits: Bits, ec_level: EcLevel) -> QrResult<Self>
Constructs a new QR code with encoded bits.
Use this method only if there are very special need to manipulate the raw bits before encoding. Some examples are:
- Encode data using specific character set with ECI
- Use the FNC1 modes
- Avoid the optimal segmentation algorithm
See the Bits structure for detail.
#![allow(unused_must_use)]
use qrcode_rs::{QrCode, Version, EcLevel};
use qrcode_rs::bits::Bits;
let mut bits = Bits::new(Version::Normal(1));
bits.push_eci_designator(9);
bits.push_byte_data(b"\xca\xfe\xe4\xe9\xea\xe1\xf2 QR");
bits.push_terminator(EcLevel::L);
let qrcode = QrCode::with_bits(bits, EcLevel::L);§Errors
Returns error if the QR code cannot be constructed, e.g. when the bits are too long, or when the version and error correction level are incompatible.
Sourcepub const fn error_correction_level(&self) -> EcLevel
pub const fn error_correction_level(&self) -> EcLevel
Gets the error correction level of this QR code.
Sourcepub const fn width(&self) -> usize
pub const fn width(&self) -> usize
Gets the number of modules per side, i.e. the width of this QR code.
The width here does not contain the quiet zone paddings.
Sourcepub fn max_allowed_errors(&self) -> usize
pub fn max_allowed_errors(&self) -> usize
Gets the maximum number of allowed erratic modules can be introduced before the data becomes corrupted. Note that errors should not be introduced to functional modules.
Sourcepub fn info(&self) -> Info
pub fn info(&self) -> Info
Returns metadata about this QR code (version, error-correction level, dimensions, module count, error tolerance, and data capacity).
§Examples
use qrcode_rs::QrCode;
let code = QrCode::new(b"hello").unwrap();
let info = code.info();
assert_eq!(info.width(), code.width());
assert_eq!(info.module_count(), code.width() * code.width());
assert!(info.data_capacity_bytes() > 0);Sourcepub fn is_functional(&self, x: usize, y: usize) -> bool
pub fn is_functional(&self, x: usize, y: usize) -> bool
Checks whether a module at coordinate (x, y) is a functional module or not.
Sourcepub fn to_debug_str(&self, on_char: char, off_char: char) -> String
pub fn to_debug_str(&self, on_char: char, off_char: char) -> String
Converts the QR code into a human-readable string. This is mainly for debugging only.
Sourcepub fn into_colors(self) -> Vec<Color>
pub fn into_colors(self) -> Vec<Color>
Converts the QR code to a vector of colors.
Sourcepub fn render<P: Pixel>(&self) -> Renderer<'_, P>
pub fn render<P: Pixel>(&self) -> Renderer<'_, P>
Renders the QR code into an image. The result is an image builder, which
you may do some additional configuration before copying it into a
concrete image.
Note: theimage crate itself also provides method to rotate the image,
or overlay a logo on top of the QR code.
§Examples
let image = QrCode::new(b"hello").unwrap()
.render()
.dark_color(Rgb([0, 0, 128]))
.light_color(Rgb([224, 224, 224])) // adjust colors
.quiet_zone(false) // disable quiet zone (white border)
.min_dimensions(300, 300) // sets minimum image size
.build();Source§impl QrCode
impl QrCode
Sourcepub fn builder<D: AsRef<[u8]>>(data: D) -> QrCodeBuilder<D>
pub fn builder<D: AsRef<[u8]>>(data: D) -> QrCodeBuilder<D>
Creates a QrCodeBuilder for configuring and constructing a QR code.
This is an ergonomic alternative to the with_* constructors. The
builder uses the same encoding paths, so its output is identical to the
equivalent constructor.
§Examples
use qrcode_rs::{QrCode, EcLevel};
let code = QrCode::builder(b"https://example.com")
.ec_level(EcLevel::H)
.build()
.unwrap();Sourcepub fn dark_modules(&self) -> DarkModules<'_> ⓘ
pub fn dark_modules(&self) -> DarkModules<'_> ⓘ
Returns an iterator over the (x, y) coordinates of every dark module,
convenient for custom rendering. The quiet zone is not included.
§Examples
use qrcode_rs::QrCode;
let code = QrCode::new(b"hi").unwrap();
let dark_count = code.dark_modules().count();Sourcepub fn for_url<D: AsRef<[u8]>>(url: D) -> QrResult<Self>
pub fn for_url<D: AsRef<[u8]>>(url: D) -> QrResult<Self>
Encodes a URL, using high error correction (robust to print damage).
§Errors
Returns an error only if the URL is too long to encode.
Sourcepub fn for_text<D: AsRef<[u8]>>(text: D) -> QrResult<Self>
pub fn for_text<D: AsRef<[u8]>>(text: D) -> QrResult<Self>
Encodes plain text at the default (medium) error correction level.
§Errors
Returns an error only if the text is too long to encode.
Sourcepub fn for_wifi(ssid: &str, password: &str, auth: &str) -> QrResult<Self>
pub fn for_wifi(ssid: &str, password: &str, auth: &str) -> QrResult<Self>
Encodes a WiFi configuration that most phone cameras will offer to join.
auth is one of WPA, WEP or nopass. Special characters in the
SSID/password are backslash-escaped per the WiFi QR specification.
§Errors
Returns an error if the resulting payload is too long to encode.
§Examples
use qrcode_rs::QrCode;
let code = QrCode::for_wifi("MyNetwork", "p\\a;ss", "WPA").unwrap();Sourcepub fn for_gs1<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
pub fn for_gs1<D: AsRef<[u8]>>(data: D) -> QrResult<Self>
Encodes a GS1 data carrier (FNC1 in first position), e.g. a GTIN /
application-identifier payload such as
"010491234512345915970331301234561842". Uses medium error correction
and the smallest fitting version.
§Errors
Returns an error if the data is too long to encode.
§Examples
use qrcode_rs::QrCode;
let code = QrCode::for_gs1("010491234512345915970331301234561842").unwrap();Sourcepub fn alt_text<D: AsRef<[u8]>>(data: D) -> String
pub fn alt_text<D: AsRef<[u8]>>(data: D) -> String
Generates accessible alt text describing a QR code that encodes data.
URLs are described as “linking to …”; other payloads as “containing: …”.
Use the result as the alt of an <img> or the aria-label of an inline
SVG so assistive technology can describe the code without decoding it.
This is an associated function (it does not require a constructed
QrCode), so the input data does not need to be retained on the code.
§Examples
use qrcode_rs::QrCode;
assert_eq!(QrCode::alt_text("https://example.com"), "QR code linking to https://example.com");
assert_eq!(QrCode::alt_text("hello"), "QR code containing: hello");Sourcepub fn alt_text_custom<D: AsRef<[u8]>, F: FnOnce(&[u8]) -> String>(
data: D,
f: F,
) -> String
pub fn alt_text_custom<D: AsRef<[u8]>, F: FnOnce(&[u8]) -> String>( data: D, f: F, ) -> String
Generates alt text with a custom formatter that receives the raw bytes.
§Examples
use qrcode_rs::QrCode;
let alt = QrCode::alt_text_custom("hello", |data| {
format!("A QR code with {} bytes", data.len())
});
assert_eq!(alt, "A QR code with 5 bytes");