Click here to Skip to main content
Licence CPOL
First Posted 22 Sep 2011
Views 11,646
Downloads 0
Bookmarked 16 times

Just launched new open source project QrCode.Net at http://qrcodenet.codeplex.com

By | 22 Sep 2011 | Technical Blog
Download binaries and application – 32K Source code – 99K Background Recently I was looking for a .NET implementation of QR code generator. Most of components either use online services to generate and/or recognize QR code or the implementation was not “good enough” for my pu
A Technical Blog article. View original blog here.[^]

Background

Recently I was looking for a .NET implementation of QR code generator. Most of 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 scan correctly, but contain 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.

Using the Library

If you can not wait to try it out just download source or binaries and follow this short guide.

Download binaries and application – 32K
Source code – 99K

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

using Gma.QrCodeNet.Encoding;

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

            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!

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:

using Gma.QrCodeNet.Rendering;

</span>
            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:

Sample 4: Saving QR code to file:

            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 ti improve. Furthermore the automated Java to c# converter produced a lot of mess.
  3. Refactoring and rewriting code in a TDD manner – 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 a 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)

About the Author

George Mamaladze

Software Developer

Germany Germany

Member

Follow on Twitter Follow on Twitter
Tweeter: @gmamaladze
Google+: gmamaladze
Blog: gmamaladze.wordpress.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionDecoding QR Code PinmemberLaunaSere16:06 9 Mar '12  
QuestionIndex out of range exception PinmemberLaunaSere2:52 28 Jan '12  
Questionqr code for .net PinmemberKeepDynamic4:22 15 Jan '12  
GeneralMy vote of 4 Pinmemberwvd_vegt7:26 13 Oct '11  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Sep 2011
Article Copyright 2011 by George Mamaladze
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid