QrCode.Net 二维码

分类:.Net知识问答| 发布:camnprbubuol| 查看: | 发表时间:2011/11/17

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 

Using the Library

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

 Collapse |  Collapse | Copy Code

            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)

来源:http://www.codeproject.com/Articles/258779/Just-launched-new-open-source-project-QrCode-Net-a.aspx

365据说看到好文章不转的人,服务器容易宕机
原创文章如转载,请注明:转载自郑州网建-前端开发 http://camnpr.com/
本文链接:http://camnpr.com/net-wiki/498.html

相关文章