| 1 | <div style="display: flex; justify-content: center"> |
| 2 | <img src="assets/banner.svg" alt="Example qr for website example.com"/> |
| 3 | </div> |
| 4 | |
| 5 | `fast_qr` is approximately 6-7 times faster than `qrcode`, see [benchmarks](#benchmarks) |
| 6 | |
| 7 | You can create a QR as |
| 8 | |
| 9 | - [x] Raw matrix, well suited for custom usage |
| 10 | - [x] Vectorized image, well suited for web usage |
| 11 | - [x] Image, well suited for mobile / print usage |
| 12 | |
| 13 | # Usage |
| 14 | |
| 15 | ## Rust |
| 16 | |
| 17 | ### Examples |
| 18 | |
| 19 | You can run the examples with: |
| 20 | |
| 21 | ```sh |
| 22 | cargo run --example simple |
| 23 | cargo run --example svg -F svg |
| 24 | cargo run --example image -F image |
| 25 | ``` |
| 26 | |
| 27 | They are all explained in detail below. |
| 28 | |
| 29 | ### Converts `QRCode` to Unicode |
| 30 | |
| 31 | ```rust |
| 32 | use fast_qr::convert::ConvertError; |
| 33 | use fast_qr::qr::QRBuilder; |
| 34 | |
| 35 | fn main() -> Result<(), ConvertError> { |
| 36 | // QRBuilder::new can fail if content is too big for version, |
| 37 | // please check before unwrapping. |
| 38 | let qrcode = QRBuilder::new("https://example.com/") |
| 39 | .build() |
| 40 | .unwrap(); |
| 41 | |
| 42 | let str = qrcode.to_str(); // .print() exists |
| 43 | println!("{}", str); |
| 44 | |
| 45 | Ok(()) |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | ### Converts `QRCode` to SVG [docs.rs](https://docs.rs/fast_qr/latest/fast_qr/convert/svg/index.html) |
| 50 | |
| 51 | _Note: It requires the `svg` feature_ |
| 52 | |
| 53 | ```rust |
| 54 | use fast_qr::convert::ConvertError; |
| 55 | use fast_qr::convert::{svg::SvgBuilder, Builder, Shape}; |
| 56 | use fast_qr::qr::QRBuilder; |
| 57 | |
| 58 | fn main() -> Result<(), ConvertError> { |
| 59 | // QRBuilder::new can fail if content is too big for version, |
| 60 | // please check before unwrapping. |
| 61 | let qrcode = QRBuilder::new("https://example.com/") |
| 62 | .build() |
| 63 | .unwrap(); |
| 64 | |
| 65 | let _svg = SvgBuilder::default() |
| 66 | .shape(Shape::RoundedSquare) |
| 67 | .to_file(&qrcode, "out.svg"); |
| 68 | |
| 69 | Ok(()) |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | ### Converts `QRCode` to an image [docs.rs](https://docs.rs/fast_qr/latest/fast_qr/convert/image/index.html) |
| 74 | |
| 75 | _Note: It requires the `image` feature_ |
| 76 | |
| 77 | ```rust |
| 78 | use fast_qr::convert::ConvertError; |
| 79 | use fast_qr::convert::{image::ImageBuilder, Builder, Shape}; |
| 80 | use fast_qr::qr::QRBuilder; |
| 81 | |
| 82 | fn main() -> Result<(), ConvertError> { |
| 83 | // QRBuilder::new can fail if content is too big for version, |
| 84 | // please check before unwrapping. |
| 85 | let qrcode = QRBuilder::new("https://example.com/") |
| 86 | .build() |
| 87 | .unwrap(); |
| 88 | |
| 89 | let _img = ImageBuilder::default() |
| 90 | .shape(Shape::RoundedSquare) |
| 91 | .background_color([255, 255, 255, 0]) // Handles transparency |
| 92 | .fit_width(600) |
| 93 | .to_file(&qrcode, "out.png"); |
| 94 | |
| 95 | Ok(()) |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | ## JavaScript / Typescript |
| 100 | |
| 101 | ### Installation |
| 102 | |
| 103 | ```bash |
| 104 | npm install --save fast_qr |
| 105 | # Or |
| 106 | yarn add fast_qr |
| 107 | ``` |
| 108 | |
| 109 | ### Create an svg |
| 110 | |
| 111 | ```js |
| 112 | /// Once `init` is called, `qr_svg` can be called any number of times |
| 113 | import init, { qr_svg, SvgOptions, Shape } from '/pkg/fast_qr.js' |
| 114 | |
| 115 | const options = new SvgOptions() |
| 116 | .margin(4) |
| 117 | .shape(Shape.Square) |
| 118 | .image("") // Can be a URL or a base64 encoded image |
| 119 | .background_color("#b8a4e5") |
| 120 | .module_color("#ffffff"); |
| 121 | |
| 122 | // Using then / catch: |
| 123 | init() |
| 124 | .then(() => { |
| 125 | for (let i = 0; i < 10; i++) { |
| 126 | const svg = qr_svg("https://fast-qr.com", options); |
| 127 | console.log(svg); |
| 128 | } |
| 129 | }) |
| 130 | .catch(console.error); |
| 131 | |
| 132 | // Or using modern async await: |
| 133 | await init(); |
| 134 | for (let i = 0; i < 10; i++) { |
| 135 | const svg = qr_svg("https://fast-qr.com", options); |
| 136 | console.log(svg); |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | # Build WASM |
| 141 | |
| 142 | ### WASM module also exists in NPM registry |
| 143 | |
| 144 | Package is named `fast_qr` and can be installed like so : |
| 145 | |
| 146 | ``` |
| 147 | npm install --save fast_qr |
| 148 | ``` |
| 149 | |
| 150 | ### WASM module might be bundled |
| 151 | |
| 152 | Find a bundled version in the latest [release](https://github.com/erwanvivien/fast_qr/releases). |
| 153 | |
| 154 | ### WASM module can be built from source |
| 155 | |
| 156 | ```bash |
| 157 | ./wasm-pack.sh # Runs build in release mode and wasm-opt twice again |
| 158 | wasm-pack pack pkg # Creates an archive of said package |
| 159 | # wasm-pack publish pkg # Creates an archive & publish it to npm |
| 160 | ``` |
| 161 | |
| 162 | ## Benchmarks |
| 163 | |
| 164 | According to the following benchmarks, `fast_qr` is approximately 6-7x faster than `qrcode`. |
| 165 | |
| 166 | | Benchmark | Lower | Estimate | Upper | | |
| 167 | | :----------- | :-------: | :-------: | :-------: | ----------------------- | |
| 168 | | V03H/qrcode | 524.30 us | 535.02 us | 547.13 us | | |
| 169 | | V03H/fast_qr | 82.079 us | 82.189 us | 82.318 us | fast_qr is 6.51x faster | |
| 170 | | V10H/qrcode | 2.1105 ms | 2.1145 ms | 2.1186 ms | | |
| 171 | | V10H/fast_qr | 268.70 us | 269.28 us | 269.85 us | fast_qr is 7.85x faster | |
| 172 | | V40H/qrcode | 18.000 ms | 18.037 ms | 18.074 ms | | |
| 173 | | V40H/fast_qr | 2.4313 ms | 2.4362 ms | 2.4411 ms | fast_qr is 7.40x faster | |
| 174 | |
| 175 | More benchmarks can be found in [/benches folder](https://github.com/erwanvivien/fast_qr/tree/master/benches). |