# \[ANN\] Coder and Decoder of QR codes

**URL:** <https://discourse.julialang.org/t/ann-coder-and-decoder-of-qr-codes/88087>\
**Category:** Package Announcements\
**Created:** [October 1, 2022, 1:56pm UTC](https://discourse.julialang.org/t/ann-coder-and-decoder-of-qr-codes/88087 "2022-10-01T13:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rex\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rex_wang/32/35108_2.png) [@Rex\_Wang](https://discourse.julialang.org/u/Rex_Wang)\
**Post date:** [October 1, 2022, 1:56pm UTC](https://discourse.julialang.org/t/ann-coder-and-decoder-of-qr-codes/88087/1 "2022-10-01T13:56:01Z")

</div>

Hey guys!! I’m excited to announce two packages that I’ve recently completed as main works of the [OSPP’22](https://summer-ospp.ac.cn) project.

- QRCoders : [GitHub - JuliaImages/QRCoders.jl: Creating QR Codes within Julia](http://github.com/JuliaImages/QRCoders.jl)

- QRDecoders: [GitHub - JuliaImages/QRDecoders.jl: OSPP'22 project repository for QR format decoder support](http://github.com/JuliaImages/QRDecoders.jl)

## Features of QRCoders

[QRCoders.jl](http://github.com/JuliaImages/QRCoders.jl) is a rewrite of [QRCode.jl](http://github.com/JuliaImages/QRCode.jl) that was discussed [here](https://discourse.julialang.org/t/ann-qrcode-jl-create-qr-code-within-julia/26923) before.

The latter provides a very nice sketch of realization, but has defects with correctness and performance. So I correct these errors and provide a **more flexible and efficient implementation** , with the help of @johnnychen94.

Here are examples of version 1 and 40(max version) respectively.

```julia
using BenchmarkTools
using QRCoders: qrcode
using QRCode:qrcode as orgqrcode
mat1 = @btime qrcode("Hello world!"; compact=true);
mat2 = @btime orgqrcode("Hello world!"; compact=true);
mat1 = @btime qrcode("Hello world!"^190; compact=true);
mat2 = @btime orgqrcode("Hello world!"^190; compact=true);

```

Benchmark results:

| repo | version | cost |
| --- | --- | --- |
| **QRCoders.jl** | 1 | **149.392 μs** (370 allocations: **32.28 KiB** ) |
| QRCode.jl | 1 | 4.040 ms (175888 allocations: 8.43 MiB) |
| **QRCoders.jl** | 40 | **21.700 ms** (29459 allocations: **4.84 MiB** ) |
| QRCode.jl | 40 | 509.677 ms (19128212 allocations: 922.52 MiB) |

As shown above, the memory is about **200 times less** than the original one, and the time-cost is about **30 times less**. In fact, almost all functions are rewritten to achieve this.

## Features of QRDecoders

First and foremost, two algorithms are implemented for error correction:

- [Sugiyama’s adaption of Euclidean algorithm](https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction#Euclidean_decoder)
- [Berlekamp-Massey algorithm](https://en.wikipedia.org/wiki/Berlekamp%E2%80%93Massey_algorithm)

The first one is written with optimized performance (benchmark results comparing with other decoders will be added in the later works).

## Future works

The **Berlekamp-Massey algorithm needs more tricks in detail**. We might optimize the performance in the future, while there are some more worthwhile works.

For image processing, the decoder can only deal with _standard QR image_. The next plan is to **enhance the image detecting ability** , so that it can deal with more complex cases.

There are much more to do with QRCoders.jl as well. For instance, the **QR code style has a creative room that beyond one’s imaginations** , like

- Add logo as mentioned in the [issue](https://github.com/JuliaImages/QRCoders.jl/issues/19)
- Unicode plot mentioned in the [issue](https://github.com/JuliaImages/QRCoders.jl/issues/25)
- Draw a picture in the QR code, for example, video of _Bad apple_!! (see BadAppleQArt-[Github(C#)](https://github.com/ibukisaar/BadAppleQArt) and [bilibili](https://www.bilibili.com/video/BV1z64y147KS/))  
 ![20221001213427](https://global.discourse-cdn.com/julialang/original/3X/9/a/9a9204a9f07e8acef4bf6b08dff2b43319dafdc8.png)

* * *

This is the first package that I developed, and it goes well smoothly thanks to @johnnychen94.  
Look forward to feedback and suggestions, and feel free to criticize or discuss.

---

<div class="post-metadata">

**Author:** ![Rex\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rex_wang/32/35108_2.png) [@Rex\_Wang](https://discourse.julialang.org/u/Rex_Wang)\
**Post date:** [November 26, 2022, 6:06pm UTC](https://discourse.julialang.org/t/ann-coder-and-decoder-of-qr-codes/88087/2 "2022-11-26T18:06:04Z")

</div>

I’m happy to discuss with you the recent work of [QRCoders](https://github.com/JuliaImages/QRCoders.jl)!! Most of them follow the suggestions of a previous [discussion (2019)](https://discourse.julialang.org/t/ann-qrcode-jl-create-qr-code-within-julia/). Now I am writing this post for more advice.  
Moreover, if you also find these ideas interesting, welcome to contribute! Feel free to give your valuable suggestions to help improve this package!

## New features:

1. Introduce a new type `QRCode` as suggested by @c42f in the [comment](https://discourse.julialang.org/t/ann-qrcode-jl-create-qr-code-within-julia/26923/25). The new type do help in the design of styles!

2. Unicode plot, either by UnicodePlots.jl suggested by @carstenbauer, or by Unicode characters `['█', '▀', '▄', ' ']` mentioned in [issue#25](https://github.com/JuliaImages/QRCoders.jl/issues/25)  

3. Add support for `.jpg` and `.gif` images

More importantly, the styling plan is drawn on the schedule! Here is a draft.

 ![yangshi](https://global.discourse-cdn.com/julialang/original/3X/b/a/bafd197b2965f36d0c9956240bea49e1d970f6e7.png)

## Pixel drawing

Most of the QR code styles follow the same strategy: First build the QR code, then beautify the image.

However, _pixel drawing_ use a strategy of **free-bits-calculation and error-correction-bits-modification** , to similar the image by real QR-bits. This requires some decoding tricks. In this regard, I am very much grateful to [Saar Ibuki](https://github.com/ibukisaar) for the helpful and creative advice.

Here are applications by _pixel drawing_:

1. Direct drawing

2. Enlarging _the logo in the center of an image_  

3. Playing games – Tetris  

4. Playing videos – BadApples  

5. More idea, like Nest QR codes  

Due to my limited time and energy, I am currently focusing on the implementation of this part only. _When QRCoders can plot images, it can play everything in fact!_

## Strategies for other styles

1. Colorful QR Code

2. Scatter plot: use `scatterplot` from [UnicodePlots](https://github.com/JuliaPlots/UnicodePlots.jl), or use the Kronecker product `kron` which served as a map of _pixels_ to _matrices_

3. Theme plot, like the Pac-Man  

* * *

BTW, the style plan proposed in [issue#33](https://github.com/JuliaImages/QRCoders.jl/issues/33) is also asked in the juliacn-meetingup-[website](https://cn.julialang.org/meetup-website/).  
If you have good ideas suitable for newcomers, welcome to the [bounty issue](https://github.com/JuliaCN/meetup-website/issues/1) to ask more questions. 😀
