diff --git a/Cargo.toml b/Cargo.toml index fffcbaf..142a27d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 "] 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"] diff --git a/README.md b/README.md index 59cb7bd..c4fd2b9 100644 --- a/README.md +++ b/README.md @@ -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::>().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::() + .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) \ No newline at end of file diff --git a/examples/encode_image.rs b/examples/encode_image.rs new file mode 100644 index 0000000..6e32801 --- /dev/null +++ b/examples/encode_image.rs @@ -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::>().build(); + + // Save the image. + image.save("/tmp/qrcode.png").unwrap(); +} diff --git a/examples/encode_string.rs b/examples/encode_string.rs new file mode 100644 index 0000000..f55dbe1 --- /dev/null +++ b/examples/encode_string.rs @@ -0,0 +1,11 @@ +extern crate qrcode; +use qrcode::QrCode; + +fn main() { + let code = QrCode::new(b"Hello").unwrap(); + let string = code.render::() + .quiet_zone(false) + .module_dimensions(2, 1) + .build(); + println!("{}", string); +} diff --git a/examples/encode_svg.rs b/examples/encode_svg.rs new file mode 100644 index 0000000..2b68018 --- /dev/null +++ b/examples/encode_svg.rs @@ -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); +}