C# QR-Code Generator
Idea
I had learned the language C#. But I only ever used to code games with the Unity Engine. So, I wanted to put my C# skills to test with a very different kind of project. A project that wouldn't involve impressive graphics, but rather focus on C# and it's unique fundamentals.
I considered projects like, basic neural networks and pathfinding algorithms, but ultimately settled on a QR-Code Generator, that I would create from scratch. I assumed, that it might just take a little bit of data encoding and involve the one or the other algorithm. But little did I know what was ahead of me…
Process
I started off by reading the Wikipedia article on how QR-Codes work. It explained the basics well, but I needed to know the details, I needed to know what every single pixel meant. Therefore I started scouring the internet for better information and eventually came across an article by Thonky. This article explained literally everything: from finder patterns and masks to data encoding and error correction, everything was explained in great detail. So I got to work!
Type
The first step was to find the right kind of QR-Code since they come in hundreds of different unique variants. I chose to work with a Version 3 (29x29 pixel) QR-Code with the error correction level L (recovers about 7% of information). This combination allowed for a code that could display up to 53 characters, which is more than enough. Soon I started learning about the fundamentals of a QR-Code and started writing the code.
C#-Code
First of all, I started with the only constant part of a QR-Code: The finder pattern. For this task, as well as for the rest of the graphics I used WinForms.
Data Bits
Then I started with the first 440 dynamic pixels of the QR-Code, the data bits. It's their job to tell a scanner what message this QR-Code is hiding. This part required turning the message string into a 440 character long string consisting of 55 bytes of information. And consequently filling the designated data pixels with the corresponding data from the data string in a snaking pattern (this can be seen in the video above).
Error Correction Bits
Now the real hard part began. The Error Correction. The secret ingredient of QR-Codes. Even when multiple bits are corrupted a scanner can still read the code and this is all thanks to remarkable mathematics behind Error Correction. Long story short, Error Correction (Reed-Solomon) interprets multiple integers into one long polynomial, where if we plug "remainders" in, we can determine which integer has been corrupted and what it is was supposed to be. While the job of scanner is to substitute a value into the polynomial, it's the generators job to calculate the "remainders". There is just one issue: a remainder could be over 255 which can't be represented since it exceeds a byte. So, we perform all calculation in something called "Galois Field 256", which makes things quite difficult since the calculations require a lot of divisions, which can't directly be done in this field. Therefore, I had to use an algorithm consisting of only addition/subtraction and multiplication to divide two polynomials.
This was really difficult, it required a rigid understanding of the mathematics behind it, before I could even dare to convert it into code. And so I did: I started writing all the Galois Field functions, imported Log- Antilog tables, created the data string and the generator polynomial and coded the rest of the logic. After countless failed attempts and hours of debugging. After hours of learning. And after hours of rewriting and condensing my code. It finally worked.
