[][src]Crate qrcode_generator

This crate provides functions to generate QR Code matrices and images in RAW, PNG and SVG formats.

Examples

Encode any data to a QR Code matrix which is Vec<Vec<bool>>.

extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

let result: Vec<Vec<bool>> = qrcode_generator::to_matrix("Hello world!", QrCodeEcc::Low).unwrap();

println!("{:?}", result);

Encode any data to a PNG image stored in a Vec instance.

extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

let result: Vec<u8> = qrcode_generator::to_png_to_vec("Hello world!", QrCodeEcc::Low, 1024).unwrap();

println!("{:?}", result);

Encode any data to a PNG image stored in a file.

This example is not tested
extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

qrcode_generator::to_png_to_file("Hello world!", QrCodeEcc::Low, 1024, "path/to/file.png").unwrap();

Encode any data to a SVG image stored in a String instance.

extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

let result: String = qrcode_generator::to_svg_to_string("Hello world!", QrCodeEcc::Low, 1024, None).unwrap();

println!("{:?}", result);

Encode any data to a SVG image stored in a file.

This example is not tested
extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

qrcode_generator::to_svg_to_file("Hello world!", QrCodeEcc::Low, 1024, None, "path/to/file.svg").unwrap();

Low-level Usage

Raw Image Data

The to_image and to_image_buffer functions can be used, if you want to modify your image.

Segments

Every generate and to function has its own by_segments function. You can concatenate segments by using different encoding methods, such as numeric, alphanumeric or binary to reduce the size (level) of your QR code matrix/image.

extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;
use qrcode_generator::qrcodegen::QrSegment;

let first = "1234567";

let second = "ABCDEFG";

let first_chars: Vec<char> = first.chars().collect();
let second_chars: Vec<char> = second.chars().collect();

let segments = vec![QrSegment::make_numeric(&first_chars), QrSegment::make_alphanumeric(&second_chars)];

let result: Vec<Vec<bool>> = qrcode_generator::to_matrix_by_segments(&segments, QrCodeEcc::Low).unwrap();

println!("{:?}", result);

Optimized URL segments

URL is a common type of data used in QR code. The protocol and the host of a URL is case-insensitive, so they can be converted to a upper-case segment and encoded by alphanumeric instead of binary to reduce the size.

You can use the optimize_url_segments function to create URL segments.

extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;


let url = "https://magiclen.org/path/to/12345";

let matrix_1 = qrcode_generator::to_matrix(url, QrCodeEcc::Low).unwrap();
let matrix_2 = qrcode_generator::to_matrix_by_segments(&qrcode_generator::optimize_url_segments(url), QrCodeEcc::Low).unwrap();

assert!(matrix_2.len() < matrix_1.len());

Validators Support

Validators is a crate which can help you validate user input.

To use with Validators support, you have to enable the validator feature for this crate.

This example is not tested
[dependencies.qrcode-generator]
version = "*"
features = ["validator"]

And the optimize_validated_http_url_segments function is available.

This example is not tested
extern crate qrcode_generator;
extern crate validators;

use qrcode_generator::QrCodeEcc;
use validators::{ValidatorOption, http_url::HttpUrlValidator};

let validator = HttpUrlValidator {
    protocol: ValidatorOption::Allow,
    local: ValidatorOption::Allow,
};

let url = "https://magiclen.org/path/to/12345";

let validated_http_url = validator.parse_str(url).unwrap();

let matrix_1 = qrcode_generator::to_matrix(url, QrCodeEcc::Low).unwrap();
let matrix_2 = qrcode_generator::to_matrix_by_segments(&qrcode_generator::optimize_validated_http_url_segments(&validated_http_url), QrCodeEcc::Low).unwrap();

assert!(matrix_2.len() < matrix_1.len());

Re-exports

pub extern crate qrcodegen;
pub extern crate validators;

Enums

QrCodeEcc

The error correction level in a QR Code symbol.

Functions

optimize_url_segments

Optimize URL text for generating QR code.

optimize_validated_http_url_segments

Optimize URL text for generating QR code.

to_image

Encode data to image data stored in a Vec instance.

to_image_buffer

Encode data to a image buffer.

to_image_buffer_by_segments

Encode data to a image buffer.

to_image_by_segments

Encode data to image data stored in a Vec instance.

to_matrix

Encode data to a QR code matrix.

to_matrix_by_segments

Encode data to a QR code matrix.

to_png

Encode data to a PNG image via any writer.

to_png_by_segments

Encode data to a PNG image via any writer.

to_png_to_file

Encode data to a PNG image via a file path.

to_png_to_file_by_segments

Encode data to a PNG image via a file path.

to_png_to_vec

Encode data to a PNG image in memory.

to_png_to_vec_by_segments

Encode data to a PNG image in memory.

to_svg

Encode data to a SVG image via any writer.

to_svg_by_segments

Encode data to a SVG image via any writer.

to_svg_to_file

Encode data to a SVG image via a file path.

to_svg_to_file_by_segments

Encode data to a SVG image via a file path.

to_svg_to_string

Encode data to a SVG image in memory.

to_svg_to_string_by_segments

Encode data to a SVG image in memory.