jlyonsmith / rust_qr_code_example

Rust QR code encode/decode example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

QR Code Rust Example

This is an example of how to decode and encode QR codes using the currently available Rust crates. It's a good idea to read How QR codes work for some background. Also, this YouTube video on How QR Codes Are Built is the best I have found.

The app uses the image to read and write image files.

For decoding I tried a bunch of crates that showed up on Google and the quircs crate seems to work best. All the decoders I tried are really slow if the image is too big, and often failed to detect the QR code, so down sampling the image to around ~500 wide is a good idea. In Rust you can down sample with ispc-downsampler. For this example, I just used Acorn on macOS for a one time down sample, which is in the examples directory.

Rust has many QR code encoding crates. After some research, I found a lot of them encode using the UTF-8 character encoding, and not ISO 8859-1 (Windows 1252). Although most libraries correctly decect UTF-8, the QR Code specification indicates that ISO 8859-1 is the intended character encoding. Take a look at the codes generated by qrcodegen which does it correctly. I discovered this by capturing QR codes from around town and trying to round trip decode then re-encode them. The encoding_rs crate explains more about different character encoding schemes. Most likely, the difference is because to encode UTF-8 in a QR code you need to use a Extended Channel Interpretation (ECI) to wrap the data.

The qrcodegen project has a good web interface for testing.

About

Rust QR code encode/decode example


Languages

Language:Rust 100.0%