qrcode-rust/src/lib.rs

374 lines
12 KiB
Rust
Raw Normal View History

//! QRCode encoder
//!
//! This crate provides a QR code and Micro QR code encoder for binary data.
//!
2017-11-07 02:23:50 -06:00
#![cfg_attr(feature = "image", doc = " ```rust")]
#![cfg_attr(not(feature = "image"), doc = "```ignore")]
2016-06-07 11:31:11 -05:00
//! extern crate qrcode;
//! extern crate image;
//!
2016-06-07 11:31:11 -05:00
//! use qrcode::QrCode;
//! use image::Luma;
2016-06-07 11:31:11 -05:00
//!
//! fn main() {
//! // Encode some data into bits.
//! let code = QrCode::new(b"01234567").unwrap();
2016-06-07 11:31:11 -05:00
//!
//! // Render the bits into an image.
//! let image = code.render::<Luma<u8>>().build();
2016-06-07 11:31:11 -05:00
//!
//! // Save the image.
//! image.save("/tmp/qrcode.png").unwrap();
//!
//! // You can also render it into a string.
//! let string = code.render()
//! .light_color(' ')
//! .dark_color('#')
//! .build();
//! println!("{}", string);
//! }
2016-06-07 11:31:11 -05:00
//! ```
#![cfg_attr(feature = "bench", feature(test, external_doc))] // Unstable libraries
2017-11-07 02:23:50 -06:00
#![cfg_attr(feature = "cargo-clippy", deny(warnings, clippy_pedantic))]
2018-07-30 04:31:40 -05:00
#![cfg_attr(
feature = "cargo-clippy",
allow(
indexing_slicing,
write_literal, // see https://github.com/rust-lang-nursery/rust-clippy/issues/2976
)
)]
#![cfg_attr(feature = "bench", doc(include = "../README.md"))]
// ^ make sure we can test our README.md.
2018-07-30 04:31:40 -05:00
#![cfg_attr(feature = "cargo-clippy", allow())]
2017-11-07 02:23:50 -06:00
extern crate checked_int_cast;
#[cfg(feature = "image")]
extern crate image;
#[cfg(feature = "bench")]
extern crate test;
use std::ops::Index;
pub mod bits;
pub mod canvas;
2017-11-07 02:23:50 -06:00
mod cast;
2018-07-30 03:59:03 -05:00
pub mod ec;
pub mod optimize;
pub mod render;
pub mod types;
2016-06-07 11:31:11 -05:00
2017-11-07 02:23:50 -06:00
pub use types::{Color, EcLevel, QrResult, Version};
2016-06-07 11:31:11 -05:00
2017-11-07 02:23:50 -06:00
use cast::As;
2018-07-30 03:59:03 -05:00
use checked_int_cast::CheckedIntCast;
use render::{Pixel, Renderer};
/// The encoded QR code symbol.
#[derive(Clone)]
pub struct QrCode {
content: Vec<Color>,
version: Version,
ec_level: EcLevel,
width: usize,
2014-08-18 14:25:40 -05:00
}
impl QrCode {
/// 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::QrCode;
///
/// let code = QrCode::new(b"Some data").unwrap();
///
2017-11-07 02:23:50 -06:00
pub fn new<D: AsRef<[u8]>>(data: D) -> QrResult<Self> {
Self::with_error_correction_level(data, EcLevel::M)
}
/// 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::{QrCode, EcLevel};
///
/// let code = QrCode::with_error_correction_level(b"Some data", EcLevel::H).unwrap();
///
2018-07-30 03:59:03 -05:00
pub fn with_error_correction_level<D: AsRef<[u8]>>(data: D, ec_level: EcLevel) -> QrResult<Self> {
2017-11-07 02:23:50 -06:00
let bits = bits::encode_auto(data.as_ref(), ec_level)?;
Self::with_bits(bits, ec_level)
}
/// Constructs a new QR code for the given version and error correction
/// level.
///
/// use qrcode::{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::{QrCode, Version, EcLevel};
///
/// let micro_code = QrCode::with_version(b"123", Version::Micro(1), EcLevel::L).unwrap();
///
2018-07-30 03:59:03 -05:00
pub fn with_version<D: AsRef<[u8]>>(data: D, version: Version, ec_level: EcLevel) -> QrResult<Self> {
let mut bits = bits::Bits::new(version);
2017-11-07 02:23:50 -06:00
bits.push_optimal_data(data.as_ref())?;
bits.push_terminator(ec_level)?;
Self::with_bits(bits, ec_level)
}
/// 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::{QrCode, Version, EcLevel};
/// use qrcode::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);
///
2017-11-07 02:23:50 -06:00
pub fn with_bits(bits: bits::Bits, ec_level: EcLevel) -> QrResult<Self> {
let version = bits.version();
let data = bits.into_bytes();
2017-11-07 02:23:50 -06:00
let (encoded_data, ec_data) = ec::construct_codewords(&*data, version, ec_level)?;
let mut canvas = canvas::Canvas::new(version, ec_level);
canvas.draw_all_functional_patterns();
canvas.draw_data(&*encoded_data, &*ec_data);
let canvas = canvas.apply_best_mask();
2018-07-30 04:31:40 -05:00
Ok(Self { content: canvas.into_colors(), version, ec_level, width: version.width().as_usize() })
}
/// Gets the version of this QR code.
pub fn version(&self) -> Version {
self.version
}
/// Gets the error correction level of this QR code.
pub fn error_correction_level(&self) -> EcLevel {
self.ec_level
}
/// 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.
pub fn width(&self) -> usize {
self.width
}
/// 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.
pub fn max_allowed_errors(&self) -> usize {
2017-11-07 02:23:50 -06:00
ec::max_allowed_errors(self.version, self.ec_level).expect("invalid version or ec_level")
}
2014-12-29 14:46:22 -06:00
/// Checks whether a module at coordinate (x, y) is a functional module or
/// not.
pub fn is_functional(&self, x: usize, y: usize) -> bool {
2018-07-30 03:59:03 -05:00
let x = x.as_i16_checked().expect("coordinate is too large for QR code");
let y = y.as_i16_checked().expect("coordinate is too large for QR code");
2017-11-07 02:23:50 -06:00
canvas::is_functional(self.version, self.version.width(), x, y)
2014-12-29 14:46:22 -06:00
}
/// Converts the QR code into a human-readable string. This is mainly for
/// debugging only.
pub fn to_debug_str(&self, on_char: char, off_char: char) -> String {
2018-07-30 03:59:03 -05:00
self.render().quiet_zone(false).dark_color(on_char).light_color(off_char).build()
}
/// Converts the QR code to a vector of booleans. Each entry represents the
/// color of the module, with "true" means dark and "false" means light.
2017-11-07 02:23:50 -06:00
#[deprecated(since = "0.4.0", note = "use `to_colors()` instead")]
pub fn to_vec(&self) -> Vec<bool> {
self.content.iter().map(|c| *c != Color::Light).collect()
}
/// Converts the QR code to a vector of booleans. Each entry represents the
/// color of the module, with "true" means dark and "false" means light.
2017-11-07 02:23:50 -06:00
#[deprecated(since = "0.4.0", note = "use `into_colors()` instead")]
pub fn into_vec(self) -> Vec<bool> {
2018-07-30 03:59:03 -05:00
self.content.into_iter().map(|c| c != Color::Light).collect()
}
/// Converts the QR code to a vector of colors.
pub fn to_colors(&self) -> Vec<Color> {
self.content.clone()
}
/// Converts the QR code to a vector of colors.
pub fn into_colors(self) -> Vec<Color> {
2016-05-16 08:42:23 -05:00
self.content
}
2016-06-07 11:31:11 -05:00
2016-06-07 12:03:16 -05:00
/// 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.
///
/// # Examples
///
2017-11-07 02:23:50 -06:00
#[cfg_attr(feature = "image", doc = " ```rust")]
#[cfg_attr(not(feature = "image"), doc = " ```ignore")]
2016-06-07 12:03:16 -05:00
/// # extern crate image;
/// # extern crate qrcode;
/// # use qrcode::QrCode;
/// # use image::Rgb;
/// # fn main() {
///
/// let image = QrCode::new(b"hello").unwrap()
/// .render()
/// .dark_color(Rgb { data: [0, 0, 128] })
/// .light_color(Rgb { data: [224, 224, 224] }) // adjust colors
/// .quiet_zone(false) // disable quiet zone (white border)
/// .min_dimensions(300, 300) // sets minimum image size
/// .build();
2016-06-07 12:03:16 -05:00
///
/// # }
/// ```
///
/// Note: the `image` crate itself also provides method to rotate the image,
/// or overlay a logo on top of the QR code.
pub fn render<P: Pixel>(&self) -> Renderer<P> {
2016-06-07 11:31:11 -05:00
let quiet_zone = if self.version.is_micro() { 2 } else { 4 };
Renderer::new(&self.content, self.width, quiet_zone)
}
}
impl Index<(usize, usize)> for QrCode {
type Output = Color;
fn index(&self, (x, y): (usize, usize)) -> &Color {
let index = y * self.width + x;
&self.content[index]
}
}
#[cfg(test)]
mod tests {
2017-11-07 02:23:50 -06:00
use {EcLevel, QrCode, Version};
#[test]
fn test_annex_i_qr() {
// This uses the ISO Annex I as test vector.
let code = QrCode::with_version(b"01234567", Version::Normal(1), EcLevel::M).unwrap();
2017-11-07 02:23:50 -06:00
assert_eq!(
&*code.to_debug_str('#', '.'),
"\
#######..#.##.#######\n\
#.....#..####.#.....#\n\
#.###.#.#.....#.###.#\n\
#.###.#.##....#.###.#\n\
#.###.#.#.###.#.###.#\n\
#.....#.#...#.#.....#\n\
#######.#.#.#.#######\n\
........#..##........\n\
#.#####..#..#.#####..\n\
...#.#.##.#.#..#.##..\n\
..#...##.#.#.#..#####\n\
....#....#.....####..\n\
...######..#.#..#....\n\
........#.#####..##..\n\
#######..##.#.##.....\n\
#.....#.#.#####...#.#\n\
#.###.#.#...#..#.##..\n\
#.###.#.##..#..#.....\n\
#.###.#.#.##.#..#.#..\n\
#.....#........##.##.\n\
#######.####.#..#.#.."
);
}
#[test]
fn test_annex_i_micro_qr() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
2017-11-07 02:23:50 -06:00
assert_eq!(
&*code.to_debug_str('#', '.'),
"\
#######.#.#.#\n\
#.....#.###.#\n\
#.###.#..##.#\n\
#.###.#..####\n\
#.###.#.###..\n\
#.....#.#...#\n\
#######..####\n\
.........##..\n\
##.#....#...#\n\
.##.#.#.#.#.#\n\
###..#######.\n\
...#.#....##.\n\
###.#..##.###"
);
}
}
2017-11-07 02:23:50 -06:00
#[cfg(all(test, feature = "image"))]
2016-06-07 11:31:11 -05:00
mod image_tests {
2017-11-07 02:23:50 -06:00
use image::{load_from_memory, Luma, Rgb};
use {EcLevel, QrCode, Version};
2016-06-07 11:31:11 -05:00
#[test]
fn test_annex_i_qr_as_image() {
let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<Luma<u8>>().build();
2018-07-30 03:59:03 -05:00
let expected = load_from_memory(include_bytes!("test_annex_i_qr_as_image.png")).unwrap().to_luma();
2016-06-07 11:31:11 -05:00
assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw());
}
#[test]
fn test_annex_i_micro_qr_as_image() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
2018-07-30 03:59:03 -05:00
let image = code
.render()
.min_dimensions(200, 200)
.dark_color(Rgb { data: [128, 0, 0] })
.light_color(Rgb { data: [255, 255, 128] })
.build();
2018-07-30 03:59:03 -05:00
let expected = load_from_memory(include_bytes!("test_annex_i_micro_qr_as_image.png")).unwrap().to_rgb();
2016-06-07 11:31:11 -05:00
assert_eq!(image.dimensions(), expected.dimensions());
assert_eq!(image.into_raw(), expected.into_raw());
}
}
2014-11-28 15:57:11 -06:00
2017-11-07 02:23:50 -06:00
#[cfg(all(test, feature = "svg"))]
2017-05-22 16:49:51 -05:00
mod svg_tests {
use render::svg::Color as SvgColor;
2017-11-07 02:23:50 -06:00
use {EcLevel, QrCode, Version};
2017-05-22 16:49:51 -05:00
#[test]
fn test_annex_i_qr_as_svg() {
let code = QrCode::new(b"01234567").unwrap();
let image = code.render::<SvgColor>().build();
let expected = include_str!("test_annex_i_qr_as_svg.svg");
assert_eq!(&image, expected);
}
#[test]
fn test_annex_i_micro_qr_as_svg() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
2018-07-30 03:59:03 -05:00
let image = code
.render()
2017-05-22 16:49:51 -05:00
.min_dimensions(200, 200)
.dark_color(SvgColor("#800000"))
.light_color(SvgColor("#ffff80"))
.build();
let expected = include_str!("test_annex_i_micro_qr_as_svg.svg");
assert_eq!(&image, expected);
}
}