Update README and examples.

This commit is contained in:
kennytm 2017-05-23 06:25:32 +08:00
parent cd5223640c
commit a32ac8bd32
5 changed files with 120 additions and 4 deletions

View file

@ -2,7 +2,7 @@
name = "qrcode"
description = "QR code encoder in Rust"
license = "MIT / Apache-2.0"
version = "0.3.0"
version = "0.4.0"
authors = ["kennytm <kennytm@gmail.com>"]
keywords = ["qrcode"]
repository = "https://github.com/kennytm/qrcode-rust"
@ -25,3 +25,14 @@ svg = []
[[bin]]
name = "qrencode"
[[example]]
name = "encode_image"
required-features = ["image"]
[[example]]
name = "encode_string"
[[example]]
name = "encode_svg"
required-features = ["svg"]

View file

@ -26,21 +26,21 @@ qrcode = { version = "0.4", default-features = false }
Example
-------
This code:
## Image generation
```rust
extern crate qrcode;
extern crate image;
use qrcode::QrCode;
use image::GrayImage;
use image::Luma;
fn main() {
// Encode some data into bits.
let code = QrCode::new(b"01234567").unwrap();
// Render the bits into an image.
let image: GrayImage = code.render().to_image();
let image = code.render::<Luma<u8>>().build();
// Save the image.
image.save("/tmp/qrcode.png").unwrap();
@ -51,3 +51,67 @@ Generates this image:
![Output](src/test_annex_i_qr_as_image.png)
## String generation
```rust
extern crate qrcode;
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
extern crate qrcode;
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!("{}", string);
}
```
Generates this SVG:
[![Output](src/test_annex_i_micro_qr_as_svg.svg)](src/test_annex_i_micro_qr_as_svg.svg)

16
examples/encode_image.rs Normal file
View file

@ -0,0 +1,16 @@
extern crate qrcode;
extern crate image;
use qrcode::QrCode;
use image::Luma;
fn main() {
// Encode some data into bits.
let code = QrCode::new(b"01234567").unwrap();
// Render the bits into an image.
let image = code.render::<Luma<u8>>().build();
// Save the image.
image.save("/tmp/qrcode.png").unwrap();
}

11
examples/encode_string.rs Normal file
View file

@ -0,0 +1,11 @@
extern crate qrcode;
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);
}

14
examples/encode_svg.rs Normal file
View file

@ -0,0 +1,14 @@
extern crate qrcode;
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);
}