This crate provides functions to generate QR Code matrices and images in RAW, PNG and SVG formats.
Vec<Vec<bool>>
.```rust extern crate qrcode_generator;
use qrcode_generator::QrCodeEcc;
let result: Vec
println!("{:?}", result); ```
```rust extern crate qrcode_generator;
use qrcode_generator::QrCodeEcc;
let result: Vec
println!("{:?}", result); ```
```rust extern crate qrcode_generator;
use qrcode_generator::QrCodeEcc;
qrcodegenerator::topngtofile("Hello world!", QrCodeEcc::Low, 1024, "path/to/file.png").unwrap(); ```
```rust extern crate qrcode_generator;
use qrcode_generator::QrCodeEcc;
let result: String = qrcodegenerator::tosvgtostring("Hello world!", QrCodeEcc::Low, 1024, None).unwrap();
println!("{:?}", result); ```
```rust extern crate qrcode_generator;
use qrcode_generator::QrCodeEcc;
qrcodegenerator::tosvgtofile("Hello world!", QrCodeEcc::Low, 1024, None, "path/to/file.svg").unwrap(); ```
The to_image
and to_image_buffer
functions can be used, if you want to modify your image.
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
let segments = vec![QrSegment::makenumeric(&firstchars), QrSegment::makealphanumeric(&secondchars)];
let result: Vec
println!("{:?}", result); ```
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
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()); ```
https://crates.io/crates/qrcode-generator
https://docs.rs/qrcode-generator