Struct qrcodegen::QrCode

source ·
pub struct QrCode { /* private fields */ }
Expand description

A QR Code symbol, which is a type of two-dimension barcode.

Invented by Denso Wave and described in the ISO/IEC 18004 standard.

Instances of this struct represent an immutable square grid of dark and light cells. The impl provides static factory functions to create a QR Code from text or binary data. The struct and impl cover the QR Code Model 2 specification, supporting all versions (sizes) from 1 to 40, all 4 error correction levels, and 4 character encoding modes.

Ways to create a QR Code object:

  • High level: Take the payload data and call QrCode::encode_text() or QrCode::encode_binary().
  • Mid level: Custom-make the list of segments and call QrCode::encode_segments() or QrCode::encode_segments_advanced().
  • Low level: Custom-make the array of data codeword bytes (including segment headers and final padding, excluding error correction codewords), supply the appropriate version number, and call the QrCode::encode_codewords() constructor.

(Note that all ways require supplying the desired error correction level.)

Implementations§

source§

impl QrCode

source

pub fn encode_text(text: &str, ecl: QrCodeEcc) -> Result<Self, DataTooLong>

Returns a QR Code representing the given Unicode text string at the given error correction level.

As a conservative upper bound, this function is guaranteed to succeed for strings that have 738 or fewer Unicode code points (not UTF-8 code units) if the low error correction level is used. The smallest possible QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.

Returns a wrapped QrCode if successful, or Err if the data is too long to fit in any version at the given ECC level.

source

pub fn encode_binary(data: &[u8], ecl: QrCodeEcc) -> Result<Self, DataTooLong>

Returns a QR Code representing the given binary data at the given error correction level.

This function always encodes using the binary segment mode, not any text mode. The maximum number of bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.

Returns a wrapped QrCode if successful, or Err if the data is too long to fit in any version at the given ECC level.

source

pub fn encode_segments( segs: &[QrSegment], ecl: QrCodeEcc ) -> Result<Self, DataTooLong>

Returns a QR Code representing the given segments at the given error correction level.

The smallest possible QR Code version is automatically chosen for the output. The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.

This function allows the user to create a custom sequence of segments that switches between modes (such as alphanumeric and byte) to encode text in less space. This is a mid-level API; the high-level API is encode_text() and encode_binary().

Returns a wrapped QrCode if successful, or Err if the data is too long to fit in any version at the given ECC level.

source

pub fn encode_segments_advanced( segs: &[QrSegment], ecl: QrCodeEcc, minversion: Version, maxversion: Version, mask: Option<Mask>, boostecl: bool ) -> Result<Self, DataTooLong>

Returns a QR Code representing the given segments with the given encoding parameters.

The smallest possible QR Code version within the given range is automatically chosen for the output. Iff boostecl is true, then the ECC level of the result may be higher than the ecl argument if it can be done without increasing the version. The mask number is either between 0 to 7 (inclusive) to force that mask, or None to automatically choose an appropriate mask (which may be slow).

This function allows the user to create a custom sequence of segments that switches between modes (such as alphanumeric and byte) to encode text in less space. This is a mid-level API; the high-level API is encode_text() and encode_binary().

Returns a wrapped QrCode if successful, or Err if the data is too long to fit in any version in the given range at the given ECC level.

source

pub fn encode_codewords( ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], msk: Option<Mask> ) -> Self

Creates a new QR Code with the given version number, error correction level, data codeword bytes, and mask number.

This is a low-level API that most users should not use directly. A mid-level API is the encode_segments() function.

source

pub fn version(&self) -> Version

Returns this QR Code’s version, in the range [1, 40].

source

pub fn size(&self) -> i32

Returns this QR Code’s size, in the range [21, 177].

source

pub fn error_correction_level(&self) -> QrCodeEcc

Returns this QR Code’s error correction level.

source

pub fn mask(&self) -> Mask

Returns this QR Code’s mask, in the range [0, 7].

source

pub fn get_module(&self, x: i32, y: i32) -> bool

Returns the color of the module (pixel) at the given coordinates, which is false for light or true for dark.

The top left corner has the coordinates (x=0, y=0). If the given coordinates are out of bounds, then false (light) is returned.

Trait Implementations§

source§

impl Clone for QrCode

source§

fn clone(&self) -> QrCode

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl PartialEq for QrCode

source§

fn eq(&self, other: &QrCode) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for QrCode

source§

impl StructuralPartialEq for QrCode

Auto Trait Implementations§

§

impl Freeze for QrCode

§

impl RefUnwindSafe for QrCode

§

impl Send for QrCode

§

impl Sync for QrCode

§

impl Unpin for QrCode

§

impl UnwindSafe for QrCode

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.