Skip to main content

qrcode_generator/
lib.rs

1/*!
2# QR Code Generator
3
4This crate generates ISO/IEC 18004 QR Code and Micro QR Code symbols and ISO/IEC 23941 rMQR symbols in pure Rust, then renders them as grayscale, PNG and SVG images.
5
6You give it data and get back a ready-to-scan image. The text encoders automatically choose the shortest Numeric, Alphanumeric, Byte and optional Kanji segmentation for each candidate version, and the Model 2 and rMQR encoders also optimize ECI transitions.
7
8## Quick start
9
10Encode text and read the raw module grid:
11
12```rust
13# #[cfg(feature = "qr")] {
14use qrcode_generator::qr::{Encoder, ErrorCorrection};
15
16let symbol = Encoder::new(ErrorCorrection::Medium)
17    .encode_text("Hello, world!")
18    .unwrap();
19
20// `true` is a dark module and `false` is a light module.
21let matrix: Vec<Vec<bool>> = symbol.to_matrix();
22
23println!("{} modules per side", symbol.size());
24# }
25```
26
27Or render straight to image files:
28
29```rust,no_run
30# #[cfg(all(feature = "std", feature = "qr", feature = "image"))] {
31use qrcode_generator::{Renderer, qr::{Encoder, ErrorCorrection}};
32
33let symbol = Encoder::new(ErrorCorrection::Medium)
34    .encode_text("Hello, world!")
35    .unwrap();
36
37Renderer::new(&symbol, 512).save_svg("hello.svg", None::<&str>).unwrap();
38Renderer::new(&symbol, 512).save_png("hello.png").unwrap();
39# }
40```
41
42## Encode, then render
43
44Every workflow has two steps. An `Encoder` (from the `qr`, `micro` or `rmqr` module) turns your data into a `Symbol`, the abstract grid of modules. A `Renderer` then turns that `Symbol` into a concrete output such as an SVG string, a PNG file or a grayscale buffer. The same `Symbol` can be rendered many times at different sizes.
45
46## Key terms
47
48A few QR Code words appear throughout this documentation:
49
50- **Module** — the smallest square of a QR Code, the equivalent of one pixel. A dark module is `true` in the matrix.
51- **Matrix** — the full grid of modules. `Symbol::to_matrix` returns it as `Vec<Vec<bool>>`, and `Symbol::modules` exposes it as one flat row-major slice without copying.
52- **Symbol** — one complete QR Code, Micro QR Code or rMQR symbol.
53- **Version** — the symbol size. QR Code versions run from 1 (21×21 modules) to 40 (177×177), Micro QR Code has versions M1 to M4 (11×11 to 17×17), and rMQR has 32 rectangular sizes.
54- **Error correction level** — how much redundancy is added so a dirty or partly hidden symbol still scans. The Low, Medium, Quartile and High levels recover roughly 7%, 15%, 25% and 30% of the codewords, and a higher level is more robust but leaves less room for your own data. Micro QR Code version M1 instead uses a detection-only level that finds errors without correcting them.
55- **Mask** — a regular pattern applied over the data to avoid layouts that confuse scanners. The best mask is chosen automatically, so you rarely set it yourself.
56- **Quiet zone** — the plain margin around the symbol that scanners need. It defaults to 4 modules for QR Code and 2 for Micro QR Code and rMQR.
57- **Mode / segment** — how characters are packed. Numeric is the most compact, then Alphanumeric, then Byte, plus optional Kanji, and the encoder mixes them automatically to save space.
58- **ECI** — an Extended Channel Interpretation header that declares the character set, such as UTF-8, when it is not the default ISO-8859-1.
59
60## Encoding
61
62`encode_text` interprets the input as ISO-8859-1 when possible and adds a UTF-8 ECI header only when it is needed:
63
64```rust
65# #[cfg(feature = "qr")] {
66use qrcode_generator::qr::{Encoder, ErrorCorrection};
67
68let symbol = Encoder::new(ErrorCorrection::Medium)
69    .encode_text("café ☕")
70    .unwrap();
71# let _ = symbol;
72# }
73```
74
75`encode_bytes` stores an exact binary payload and never guesses a character set:
76
77```rust
78# #[cfg(feature = "qr")] {
79use qrcode_generator::qr::{Encoder, ErrorCorrection};
80
81let symbol = Encoder::new(ErrorCorrection::Quartile)
82    .encode_bytes(b"raw\0bytes")
83    .unwrap();
84# let _ = symbol;
85# }
86```
87
88The encoder is configured with a builder. You can restrict the versions it may use, or force a specific mask:
89
90```rust
91# #[cfg(feature = "qr")] {
92use qrcode_generator::qr::{Encoder, ErrorCorrection, Version};
93
94let symbol = Encoder::new(ErrorCorrection::Low)
95    .version_range(Version::new(1).unwrap()..=Version::new(10).unwrap())
96    .encode_text("HELLO")
97    .unwrap();
98# let _ = symbol;
99# }
100```
101
102By default the encoder upgrades the error correction level for free when the chosen version has spare room. Call `boost_error_correction(false)` to keep the exact level you requested.
103
104### Explicit segments
105
106Automatic segmentation is optimal for most inputs, but you can also build `Segment` values yourself and keep their boundaries:
107
108```rust
109# #[cfg(feature = "qr")] {
110use qrcode_generator::{Segment, qr::{Encoder, ErrorCorrection}};
111
112let segments = [
113    Segment::numeric("1234567").unwrap(),
114    Segment::alphanumeric("ABCDEFG").unwrap(),
115];
116
117let symbol = Encoder::new(ErrorCorrection::Low)
118    .encode_segments(&segments)
119    .unwrap();
120# let _ = symbol;
121# }
122```
123
124### Custom text
125
126[`ToQRText`] lets a structured value choose a shorter equivalent spelling before the optimizer runs:
127
128```rust
129# #[cfg(feature = "qr")] {
130use qrcode_generator::{ToQRText, qr::{Encoder, ErrorCorrection}};
131
132struct ProductCode(&'static str);
133
134impl ToQRText for ProductCode {
135    fn to_qr_text(&self) -> String {
136        self.0.to_ascii_uppercase()
137    }
138}
139
140let symbol = Encoder::new(ErrorCorrection::Medium)
141    .encode_to_qr_text(&ProductCode("item-123"))
142    .unwrap();
143# let _ = symbol;
144# }
145```
146
147The value must keep its meaning. With the optional `url` feature, `url::Url` implements `ToQRText` by normalizing case-insensitive URL parts and percent escapes:
148
149```rust
150# #[cfg(all(feature = "qr", feature = "url"))] {
151use qrcode_generator::qr::{Encoder, ErrorCorrection};
152use url::Url;
153
154let url = Url::parse("https://magiclen.org").unwrap();
155let symbol = Encoder::new(ErrorCorrection::Medium).encode_to_qr_text(&url).unwrap();
156# let _ = symbol;
157# }
158```
159
160## Rendering
161
162A `Renderer` draws a `Symbol` at exact pixel dimensions. `Renderer::new` keeps the square interface, while `Renderer::new_with_dimensions` accepts a separate width and height for rectangular symbols. In-memory grayscale and SVG output work with `no_std + alloc` and need no extra feature:
163
164```rust
165# #[cfg(feature = "qr")] {
166use qrcode_generator::{Renderer, qr::{Encoder, ErrorCorrection}};
167
168let symbol = Encoder::new(ErrorCorrection::Low).encode_text("Hello").unwrap();
169
170let svg: String = Renderer::new(&symbol, 512).to_svg_string(None::<&str>).unwrap();
171let pixels: Vec<u8> = Renderer::new(&symbol, 512).to_luma8().unwrap();
172# let _ = (svg, pixels);
173# }
174```
175
176The renderer also implements `Display`, drawing the symbol as compact Unicode text that packs two module rows into every line through half block characters, including the quiet zone. The default form draws dark modules as visible blocks; the alternate form (`{:#}`) draws light modules instead, which keeps the standard contrast on dark terminal backgrounds. Text output works in module units, so the pixel size passed to the renderer does not affect it:
177
178```rust
179# #[cfg(feature = "qr")] {
180use qrcode_generator::{Renderer, qr::{Encoder, ErrorCorrection}};
181
182let symbol = Encoder::new(ErrorCorrection::Low).encode_text("Hello").unwrap();
183let renderer = Renderer::new(&symbol, 0);
184
185// For light terminal backgrounds.
186print!("{renderer}");
187
188// For dark terminal backgrounds.
189print!("{renderer:#}");
190# }
191```
192
193PNG and `ImageBuffer` output need the default `image` feature:
194
195```rust,no_run
196# #[cfg(all(feature = "std", feature = "qr", feature = "image"))] {
197use qrcode_generator::{Renderer, qr::{Encoder, ErrorCorrection}};
198
199let symbol = Encoder::new(ErrorCorrection::Low).encode_text("Hello").unwrap();
200
201let png: Vec<u8> = Renderer::new(&symbol, 512).to_png_vec().unwrap();
202
203Renderer::new(&symbol, 512).save_png("hello.png").unwrap();
204# let _ = png;
205# }
206```
207
208The default `std` feature adds synchronous writer and file output. File output through `save_svg` and `save_png` is written atomically, so an existing file is left untouched if rendering fails. The requested size is exact: modules are scaled by the largest whole number that fits, and any pixels left over widen the quiet zone evenly. Use `quiet_zone` to change the margin, down to zero if you want.
209
210## Async writing
211
212The optional `tokio` feature adds `write_svg_async` and, with the `image` feature, `write_png_async`, which write and flush to any `tokio::io::AsyncWrite`, including a `tokio::fs::File`. Rendering stays synchronous; only writing and flushing are asynchronous:
213
214```rust,no_run
215# #[cfg(all(feature = "qr", feature = "tokio"))]
216# mod example {
217use qrcode_generator::{
218    RenderError, Renderer,
219    qr::{Encoder, ErrorCorrection},
220};
221
222async fn write<W: tokio::io::AsyncWrite + Unpin>(writer: W) -> Result<(), RenderError> {
223    let symbol = Encoder::new(ErrorCorrection::Low).encode_text("Hello").unwrap();
224
225    Renderer::new(&symbol, 512).write_svg_async(writer, None::<&str>).await
226}
227# }
228```
229
230The feature also adds `save_svg_async` and `save_png_async`, which save to a path atomically like their synchronous counterparts by running the write on tokio's blocking pool.
231
232## Micro QR Code
233
234The optional `micro-qr` feature adds an independent encoder for the four Micro QR Code versions, which are smaller than QR Code and suit short payloads:
235
236```rust
237# #[cfg(feature = "micro-qr")] {
238use qrcode_generator::micro::{Encoder, ErrorCorrection, Version};
239
240let symbol = Encoder::new(ErrorCorrection::Low)
241    .version(Version::M2)
242    .encode_text("12345")
243    .unwrap();
244# let _ = symbol;
245# }
246```
247
248Version M1 supports only the `DetectionOnly` error correction level, which detects errors without correcting them; M2 and M3 add Low and Medium, and M4 also offers Quartile.
249
250Micro QR Code does not support ECI, FNC1 or Structured Append. Its text API accepts ISO-8859-1 and, with the `kanji` feature, eligible Shift JIS Kanji characters.
251
252## Rectangular Micro QR Code
253
254The optional `rmqr` feature adds all 32 ISO/IEC 23941 rMQR versions. The encoder automatically selects the smallest-area version that fits and performs exact segment and ECI optimization for each character-count profile:
255
256```rust
257# #[cfg(feature = "rmqr")] {
258use qrcode_generator::{Renderer, rmqr::{Encoder, ErrorCorrection}};
259
260let symbol = Encoder::new(ErrorCorrection::Medium)
261    .encode_text("https://magiclen.org")
262    .unwrap();
263
264let width = (symbol.width() + 4) * 8;
265let height = (symbol.height() + 4) * 8;
266let svg = Renderer::new_with_dimensions(&symbol, width, height)
267    .to_svg_string(None::<&str>)
268    .unwrap();
269# }
270```
271
272rMQR supports Medium and High error correction, ECI, FNC1 and optional Kanji mode. It has one fixed data mask and does not support Structured Append.
273
274## Automatic family selection
275
276With both `qr` and `micro-qr` enabled, `AutoEncoder` tries the Micro QR Code encoder first and falls back to the Model 2 encoder when the data does not fit, giving you the smallest symbol that works:
277
278```rust
279# #[cfg(all(feature = "qr", feature = "micro-qr"))] {
280use qrcode_generator::{AutoEncoder, micro, qr};
281
282let encoder = AutoEncoder::new(
283    qr::Encoder::new(qr::ErrorCorrection::Medium),
284    micro::Encoder::new(micro::ErrorCorrection::Medium),
285);
286
287let symbol = encoder.encode_text("HELLO 123").unwrap();
288# let _ = symbol;
289# }
290```
291
292## FNC1 and Structured Append
293
294These two QR Code features serve data-exchange standards rather than plain text.
295
296*FNC1* marks a symbol as following a formatted-data standard rather than plain text. First position selects GS1, whose element strings concatenate application identifiers and separate variable-length ones with a `0x1D` (GS) byte; second position selects an industry application indicator through `ApplicationIndicator`. Pass the data with the `0x1D` separators already in place and turn FNC1 on with `qr::Encoder::fnc1`:
297
298```rust
299# #[cfg(feature = "qr")] {
300use qrcode_generator::qr::{Encoder, ErrorCorrection, Fnc1};
301
302// AI 01 is a GTIN, the 0x1D (GS) byte separates it, then AI 10 is a batch number.
303let symbol = Encoder::new(ErrorCorrection::Medium)
304    .fnc1(Some(Fnc1::Gs1))
305    .encode_bytes(b"0101234567890128\x1D10ABC")
306    .unwrap();
307# let _ = symbol;
308# }
309```
310
311*Structured Append* spreads one message across up to 16 symbols that a reader stitches back together. You can supply the parts yourself, or let the encoder split a message automatically:
312
313```rust,no_run
314# #[cfg(feature = "qr")] {
315use qrcode_generator::qr::{Encoder, ErrorCorrection};
316
317let symbols = Encoder::new(ErrorCorrection::Medium)
318    .encode_text_with_structured_append("a very long message …")
319    .unwrap();
320
321println!("{} symbols", symbols.len());
322# }
323```
324
325Automatic splitting first minimizes the number of symbols, then their largest version, and finally the total symbol area.
326
327## Kanji
328
329The optional `kanji` feature adds full Shift JIS support to automatic text segmentation and exposes `Segment::kanji`. Eligible characters use the compact 13-bit Kanji mode, and other Shift JIS text such as half-width katakana can be stored as Shift JIS bytes behind an ECI 000020 header. Following the strict ECI rules of ISO/IEC 18004, Kanji mode is only used while the default interpretation or an explicit Shift JIS ECI is in force, so inputs mixing UTF-8 and Kanji data stay readable for strict ECI decoders. The feature is off by default for the widest scanner compatibility.
330
331## Cargo features
332
333- `std` (default) enables synchronous writer and file output.
334- `qr` (default) enables the Model 2 QR Code encoder.
335- `image` (default, requires `std`) enables PNG and `ImageBuffer` output.
336- `micro-qr` enables the Micro QR Code encoder with all four versions.
337- `rmqr` enables the Rectangular Micro QR Code encoder with all 32 versions.
338- `kanji` enables Shift JIS Kanji segments and automatic Kanji mode selection.
339- `url` implements `ToQRText` for `url::Url` and works with `no_std + alloc`.
340- `tokio` (requires `std`) enables the tokio asynchronous writer and atomic file-save methods.
341*/
342
343#![cfg_attr(not(feature = "std"), no_std)]
344#![cfg_attr(docsrs, feature(doc_cfg))]
345
346extern crate alloc;
347#[cfg(test)]
348extern crate std;
349
350mod encode;
351mod error;
352#[cfg(any(feature = "qr", feature = "micro-qr", feature = "rmqr"))]
353mod render;
354#[cfg(feature = "url")]
355mod url;
356
357#[cfg(all(feature = "qr", feature = "micro-qr"))]
358pub use encode::AutoEncoder;
359pub use encode::{Segment, ToQRText};
360#[cfg(any(feature = "qr", feature = "micro-qr", feature = "rmqr"))]
361pub use encode::{Symbol, SymbolErrorCorrection, SymbolVersion};
362pub use error::{EncodeError, RenderError};
363#[cfg(any(feature = "qr", feature = "micro-qr", feature = "rmqr"))]
364pub use render::Renderer;
365
366/// Model 2 QR Code encoding types.
367#[cfg(feature = "qr")]
368pub mod qr {
369    pub use crate::encode::{
370        ApplicationIndicator, EciAssignment, Fnc1, QrEncoder as Encoder,
371        QrErrorCorrection as ErrorCorrection, QrMask as Mask, QrVersion as Version,
372        StructuredAppendInfo,
373    };
374}
375
376/// Micro QR Code encoding types.
377#[cfg(feature = "micro-qr")]
378pub mod micro {
379    pub use crate::encode::{
380        MicroEncoder as Encoder, MicroErrorCorrection as ErrorCorrection, MicroMask as Mask,
381        MicroVersion as Version,
382    };
383}
384
385/// Rectangular Micro QR Code encoding types.
386#[cfg(feature = "rmqr")]
387pub mod rmqr {
388    pub use crate::encode::{
389        ApplicationIndicator, EciAssignment, Fnc1, RmqrEncoder as Encoder,
390        RmqrErrorCorrection as ErrorCorrection, RmqrVersion as Version,
391    };
392}