QR Code Generator

Build Status Build status

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>>.

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

let result: Vec> = qrcodegenerator::tomatrix("Hello world!", QrCodeEcc::Low).unwrap();

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

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

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

let result: Vec = qrcodegenerator::topngtovec("Hello world!", QrCodeEcc::Low, 1024).unwrap();

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

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

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

qrcodegenerator::topngtofile("Hello world!", QrCodeEcc::Low, 1024, "path/to/file.png").unwrap(); ```

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

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

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

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

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

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

qrcodegenerator::tosvgtofile("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.

```rust extern crate qrcode_generator;

use qrcodegenerator::QrCodeEcc; use qrcodegenerator::qrcodegen::QrSegment;

let first = "1234567";

let second = "ABCDEFG";

let firstchars: Vec = first.chars().collect(); let secondchars: Vec = second.chars().collect();

let segments = vec![QrSegment::makenumeric(&firstchars), QrSegment::makealphanumeric(&secondchars)];

let result: Vec> = qrcodegenerator::tomatrixbysegments(&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.

```rust extern crate qrcode_generator;

use qrcode_generator::QrCodeEcc;

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

let matrix1 = qrcodegenerator::tomatrix(url, QrCodeEcc::Low).unwrap(); let matrix2 = qrcodegenerator::tomatrixbysegments(&qrcodegenerator::optimizeurl_segments(url), QrCodeEcc::Low).unwrap();

assert!(matrix2.len() < matrix1.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.

toml [dependencies.qrcode-generator] version = "*" features = ["validator"]

And the optimize_validated_http_url_segments function is available.

```rust extern crate qrcode_generator; extern crate validators;

use qrcodegenerator::QrCodeEcc; use validators::{ValidatorOption, httpurl::HttpUrlValidator};

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

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

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

let matrix1 = qrcodegenerator::tomatrix(url, QrCodeEcc::Low).unwrap(); let matrix2 = qrcodegenerator::tomatrixbysegments(&qrcodegenerator::optimizevalidatedhttpurlsegments(&validatedhttp_url), QrCodeEcc::Low).unwrap();

assert!(matrix2.len() < matrix1.len()); ```

Crates.io

https://crates.io/crates/qrcode-generator

Documentation

https://docs.rs/qrcode-generator

License

MIT