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()
orQrCode::encode_binary()
. - Mid level: Custom-make the list of segments and call
QrCode::encode_segments()
orQrCode::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
impl QrCode
sourcepub fn encode_text(text: &str, ecl: QrCodeEcc) -> Result<Self, DataTooLong>
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.
sourcepub fn encode_binary(data: &[u8], ecl: QrCodeEcc) -> Result<Self, DataTooLong>
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.
sourcepub fn encode_segments(
segs: &[QrSegment],
ecl: QrCodeEcc
) -> Result<Self, DataTooLong>
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.
sourcepub fn encode_segments_advanced(
segs: &[QrSegment],
ecl: QrCodeEcc,
minversion: Version,
maxversion: Version,
mask: Option<Mask>,
boostecl: bool
) -> Result<Self, DataTooLong>
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.
sourcepub fn encode_codewords(
ver: Version,
ecl: QrCodeEcc,
datacodewords: &[u8],
msk: Option<Mask>
) -> Self
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.
sourcepub fn error_correction_level(&self) -> QrCodeEcc
pub fn error_correction_level(&self) -> QrCodeEcc
Returns this QR Code’s error correction level.
sourcepub fn get_module(&self, x: i32, y: i32) -> bool
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.