qrcode-rust/README.md

148 lines
4.4 KiB
Markdown
Raw Permalink Normal View History

2014-08-18 21:27:17 +00:00
qrcode-rust
===========
[![Build status](https://travis-ci.org/kennytm/qrcode-rust.svg?branch=master)](https://travis-ci.org/kennytm/qrcode-rust)
2016-05-14 15:43:38 +00:00
[![Coverage Status](https://coveralls.io/repos/github/kennytm/qrcode-rust/badge.svg?branch=coveralls)](https://coveralls.io/github/kennytm/qrcode-rust?branch=coveralls)
[![crates.io](https://img.shields.io/crates/v/qrcode.svg)](https://crates.io/crates/qrcode)
2019-12-22 08:59:06 +00:00
[![MIT OR Apache 2.0](https://img.shields.io/badge/license-MIT%20%2f%20Apache%202.0-blue.svg)](./LICENSE-APACHE.txt)
2014-08-18 21:27:17 +00:00
QR code and Micro QR code encoder in Rust. [Documentation](https://docs.rs/qrcode).
2016-06-07 16:31:11 +00:00
Cargo.toml
----------
```toml
[dependencies]
2019-12-22 08:59:06 +00:00
qrcode = "0.11"
2016-06-07 16:31:11 +00:00
```
The default settings will depend on the `image` crate. If you don't need image generation capability, disable the `default-features`:
2014-08-18 21:27:17 +00:00
```toml
2016-05-14 15:43:38 +00:00
[dependencies]
2019-12-22 08:59:06 +00:00
qrcode = { version = "0.11", default-features = false }
2014-08-18 21:27:17 +00:00
```
2016-06-07 16:31:11 +00:00
Example
-------
2017-05-22 22:25:32 +00:00
## Image generation
2016-06-07 16:31:11 +00:00
```rust
use qrcode::QrCode;
2017-05-22 22:25:32 +00:00
use image::Luma;
2016-06-07 16:31:11 +00:00
fn main() {
// Encode some data into bits.
let code = QrCode::new(b"01234567").unwrap();
// Render the bits into an image.
2017-05-22 22:25:32 +00:00
let image = code.render::<Luma<u8>>().build();
2016-06-07 16:31:11 +00:00
// Save the image.
image.save("/tmp/qrcode.png").unwrap();
}
```
Generates this image:
![Output](src/test_annex_i_qr_as_image.png)
2014-08-18 21:27:17 +00:00
2017-05-22 22:25:32 +00:00
## String generation
```rust
use qrcode::QrCode;
fn main() {
let code = QrCode::new(b"Hello").unwrap();
let string = code.render::<char>()
.quiet_zone(false)
.module_dimensions(2, 1)
.build();
println!("{}", string);
}
```
Generates this output:
```none
############## ######## ##############
## ## ## ## ##
## ###### ## ## ## ## ## ###### ##
## ###### ## ## ## ## ###### ##
## ###### ## #### ## ## ###### ##
## ## #### ## ## ##
############## ## ## ## ##############
## ##
## ########## ## ## ##########
## ## ######## #### ##
########## #### ## #### ######
## ## #### ########## ####
###### ########## ## ## ##
## ## ## ##
############## ## ## ## ## ####
## ## ## ## ##########
## ###### ## ## ## ## ## ##
## ###### ## #### ########## ##
## ###### ## #### ## #### ##
## ## ## ######## ######
############## #### ## ## ##
```
## SVG generation
```rust
use qrcode::{QrCode, Version, EcLevel};
use qrcode::render::svg;
fn main() {
let code = QrCode::with_version(b"01234567", Version::Micro(2), EcLevel::L).unwrap();
let image = code.render()
.min_dimensions(200, 200)
.dark_color(svg::Color("#800000"))
.light_color(svg::Color("#ffff80"))
.build();
println!("{}", image);
2017-05-22 22:25:32 +00:00
}
```
Generates this SVG:
2020-02-09 13:24:59 +00:00
[![Output](src/test_annex_i_micro_qr_as_svg.svg)](src/test_annex_i_micro_qr_as_svg.svg)
## Unicode string generation
```rust
use qrcode::QrCode;
2020-02-11 08:05:22 +00:00
use qrcode::render::unicode;
2020-02-09 13:24:59 +00:00
fn main() {
let code = QrCode::new("mow mow").unwrap();
2020-02-11 08:05:22 +00:00
let image = code.render::<unicode::Dense1x2>()
.dark_color(unicode::Dense1x2::Light)
.light_color(unicode::Dense1x2::Dark)
2020-02-09 13:24:59 +00:00
.build();
println!("{}", image);
}
```
Generates this output:
```text
2020-02-09 13:24:59 +00:00
█████████████████████████████
█████████████████████████████
████ ▄▄▄▄▄ █ ▀▀▀▄█ ▄▄▄▄▄ ████
████ █ █ █▀ ▀ ▀█ █ █ ████
████ █▄▄▄█ ██▄ ▀█ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ ▀▄▀ █▄▄▄▄▄▄▄████
████▄▀ ▄▀ ▄ █▄█ ▀ ▀█ █▄ ████
████▄██▄▄▀▄▄▀█▄ ██▀▀█▀▄▄▄████
█████▄▄▄█▄▄█ ▀▀▄█▀▀▀▄█▄▄████
████ ▄▄▄▄▄ █ ▄▄██▄ ▄ ▀▀████
████ █ █ █▀▄▄▀▄▄ ▄▄▄▄ ▄████
████ █▄▄▄█ █▄ █▄▀▄▀██▄█▀████
████▄▄▄▄▄▄▄█▄████▄█▄██▄██████
█████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
```