Click here to Skip to main content
15,883,883 members
Articles / All Topics

Just Launched a New Open Source Project QrCode.Net at http://qrcodenet.codeplex.com

Rate me:
Please Sign up or sign in to vote.
4.86/5 (7 votes)
22 Sep 2011CPOL2 min read 98K   42   16
Just launched a new open source project QrCode.Net at http://qrcodenet.codeplex.com

Background

Recently, I was looking for a .NET implementation of QR code generator. Most of the components either use online services to generate and/or recognize QR code or the implementation was not “good enough” for my purposes. The most popular and very powerful Java implementation comes from Google’s open source project code.google.com/p/zxing called ZXing (Zebra Crossing => Z=Zebra + X=Cross + ing).

There I found a one-to-one c# port of earlier version. The project is focusing on additional features and further development in Java so it seems that no one is taking care of C# branch.

Thus, I have decided to set-up this project QRCode.Netqrcodenet.codeplex.com.

As a start point, I took a straight forward C# port of Google’s QR code implementation from ZXing project.
I wrote a wrapper around and a demo application which is able to generate QR code from text as you type and save it to file.

In addition, it contains a very naive and simple implementation of Artistic QR code generation. At the highest error correction level, it is possible to create artistic QR codes that still scans correctly, but contains intentional errors to make them more readable or attractive to the human eye, as well as to incorporate colors, logos and other features into the QR code block.

Image 1

Using the Library

If you cannot wait to try it out, just download the source or binaries and follow this short guide.

First of all, add a reference to the project or assembly _Gma.QrCodeNet.Encoding_ and import the namespace.

C#
using Gma.QrCodeNet.Encoding;

Sample 1

Encoding text to QR code bit matrix and printing out the result to console:

C#
Console.Write(@"Type some text to QR code: ");
string sampleText = Console.ReadLine();
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(sampleText);
for (int j = 0; j < qrCode.Matrix.Width; j++)
{
    for (int i = 0; i < qrCode.Matrix.Width; i++)
    {
       char charToPrint = qrCode.Matrix[i, j] ? '¦' : ' ';
       Console.Write(charToPrint);
    }
    Console.WriteLine();
}
Console.WriteLine(@"Press any key to quit.");
Console.ReadKey();

Tada!

Image 2

Sample 2

Rendering QR code to graphics, let’s say to panel control with segment size of 5 pixel, blue on yellow and 10 pixel padding:

C#
using Gma.QrCodeNet.Rendering;

const string helloWorld = "Hello World!";

QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode(helloWorld);

const int moduleSizeInPixels = 5;
Renderer renderer = new Renderer(moduleSizeInPixels, Brushes.Blue, Brushes.Yellow);

Panel panel = new Panel();
Point padding =  new Point(10,16);
Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);
panel.AutoSize = false;
panel.Size = qrCodeSize + new Size(2 * padding.X, 2 * padding.Y);

using (Graphics graphics = panel.CreateGraphics())
{
    renderer.Draw(graphics, qrCode.Matrix, padding);
}

Sample 3

Using out-of-the-box WinForms control. Just drag & drop it to your form an set Text and AutoSize property:

Image 3

Sample 4

Saving QR code to file:

C#
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = qrEncoder.Encode("Hello World!");

Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White);
renderer.CreateImageFile(qrCode.Matrix, @"c:\temp\HelloWorld.png", ImageFormat.Png);

Project TODOs and Plans

I think this project would be a good playground for:

  1. Understanding QR code form inside
  2. Designing and implementing efficient and elegant data structures and algorithms. I have looked at the ZXing codebase, especially encoding. The code is good, but there are still many things to improve. Furthermore, the automated Java to C# converter produced a lot of mess.
  3. Refactoring and rewriting code in a TDD manner – the existing implementation can be used as an excellent refactoring initial point. In some cases, I think I would prefer to rewrite and use old classes as reference implementations and run my test again, both the old and the new code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Comments and Discussions

 
GeneralYou look just like Robert Picardo :) Pin
strogg7-Nov-15 19:21
strogg7-Nov-15 19:21 
QuestionJavaScript implementation Pin
Kathrene73-Jul-15 2:18
Kathrene73-Jul-15 2:18 
Questionsaved image with right and bottom border Pin
Member 1137750315-Jan-15 3:36
Member 1137750315-Jan-15 3:36 
AnswerRe: saved image with right and bottom border Pin
Christopher Enengl22-Feb-16 4:46
Christopher Enengl22-Feb-16 4:46 
QuestionAbout dll file and project Pin
lepteo144-Mar-14 20:41
lepteo144-Mar-14 20:41 
QuestionUTF-8 Charset Pin
m_reshadi8-Nov-13 11:44
m_reshadi8-Nov-13 11:44 
QuestionIt dosen't work in Chinese Pin
bruce_wang20018-Aug-13 9:19
bruce_wang20018-Aug-13 9:19 
SuggestionFully test your artistic QR Code before publishing! Pin
mic212-May-13 6:26
mic212-May-13 6:26 
GeneralRe: Fully test your artistic QR Code before publishing! Pin
priyanga@y7mail.com4-Sep-13 2:38
priyanga@y7mail.com4-Sep-13 2:38 
Questionsample code for generating QR Code Pin
fdgsasfsd5-Feb-13 15:56
fdgsasfsd5-Feb-13 15:56 
QuestionvCard Support? Pin
i0030-Jan-13 17:57
i0030-Jan-13 17:57 
GeneralMy vote of 4 Pin
Burak Tunçbilek12-Jun-12 2:44
Burak Tunçbilek12-Jun-12 2:44 
QuestionDecoding QR Code Pin
LaunaSere9-Mar-12 16:06
LaunaSere9-Mar-12 16:06 
QuestionIndex out of range exception Pin
LaunaSere28-Jan-12 2:52
LaunaSere28-Jan-12 2:52 
GeneralMy vote of 4 Pin
wvd_vegt13-Oct-11 7:26
professionalwvd_vegt13-Oct-11 7:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.