C#利用QrCode.Net生成二维码

摘要:
Qr代码。Net是一个用C#编写的用于生成二维码图像的类库。它可用于为WinForm、WebForm、WPF、Silverlight和WindowsPhone7应用程序提供QR码编码和输出功能。测试结果如下(微秒):输入字符串长度:74 ECperformance1000Tests~QrCode。Net:3929ZXing:5221,QrCodeNet可以分析字符串并决定是否使用UTF-8编码。)如何使用QrCode:创建一个新项目,添加对类库的引用,然后引入Gma。QrCodeNet。正在编码命名空间。?

Soar、毅现在网上很多应用都是用二维码来分享网址或者其它的信息。尤其在移动领域,二维码更是有很大的应用场景。因为项目的需要,需要在网站中增加一个生成二维码分析网址的功能,在谷歌大幅度抽筋的情况下无奈使用百度。百度N多,找到一些项目,但是可用性不强。(有一个项目是用VS2005开发的,在2010中调试不开。)终于在codeplex上找到一个“神器”,这个“神器”可以很方便的生成二维码,速度那是相当的快,并且可支持中文,遵从MIT协议。

QrCode.Net是一个使用C#编写的用于生成二维码图片的类库,使用它可以非常方便的为WinForm、WebForm、WPF、Silverlight和Windows Phone 7应用程序提供二维码编码输出功能。可以将二维码文件导出为eps格式。

项目地址为:http://qrcodenet.codeplex.com

QrCode.Net不再采用http://code.google.com/p/zxing/ ZXing的端口,新的版本将有更好的性能。C#利用QrCode.Net生成二维码第2张

测试结果如下(微秒):

输入字符串长度:74个

EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221

同时,QrCode.Net可以对字符串进行分析,决定是否使用UTF-8编码。(比如使用中文的时候。)

QrCode使用方法:

新建项目添加对类库的引用,然后引入Gma.QrCodeNet.Encoding命名空间。

usingGma.QrCodeNet.Encoding;

在控制台中输出二维码:

Console.Write(@"Type some text to QR code: ");
stringsampleText = Console.ReadLine();
QrEncoder qrEncoder = newQrEncoder(ErrorCorrectionLevel.M);
QrCode qrCode = qrEncoder.Encode(sampleText);
for(intj = 0; j < qrCode.Matrix.Width; j++)
{
    for(inti = 0; i < qrCode.Matrix.Width; i++)
    {
 
        charcharToPrint = qrCode.Matrix[i, j] ? '█': ' ';
        Console.Write(charToPrint);
    }
    Console.WriteLine();
}
Console.WriteLine(@"Press any key to quit.");
Console.ReadKey();

此代码将产生以下输出:

 C#利用QrCode.Net生成二维码第3张

在Graphics上绘制二维码:

conststringhelloWorld = "Hello World!";
 
     QrEncoder qrEncoder = newQrEncoder(ErrorCorrectionLevel.H);
     QrCode qrCode = qrEncoder.Encode(helloWorld);
 
     constintmoduleSizeInPixels = 5;
     Renderer renderer = newRenderer(moduleSizeInPixels, Brushes.Black, Brushes.White);
 
     Panel panel = newPanel();
     Point padding =  newPoint(10,16);
     Size qrCodeSize = renderer.Measure(qrCode.Matrix.Width);
     panel.AutoSize = false;
     panel.Size = qrCodeSize + newSize(2 * padding.X, 2 * padding.Y);
      
     using(Graphics graphics = panel.CreateGraphics())
     {
         renderer.Draw(graphics, qrCode.Matrix, padding);
     }

在WriteableBitmap上绘制二维码:

conststringhelloWorld = "Hello World!";
 
QrEncoder qrEncoder = newQrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = newQrCode();
qrEncoder.TryEncode(helloWorld, outqrCode);
 
constintmoduleSizeInPixels = 5;
Renderer renderer = newRenderer(moduleSizeInPixels);   //Black&White is default colour for drawing QrCode
 
//Matrix under qrCode might be null if input string is null or empty. 21 module wide is version 1 QrCode's width.
intpixelSize = qrCode.Matrix == null? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width);
 
WriteableBitmap wBitmap = newWriteableBitmap(pixelSize, pixelSize, 96, 96, PixelFormats.Gray8, null);
 
//If wBitmap is null value. renderer will create Gray8 Bitmap as default.
renderer.Draw(wBitmap, qrCode.Matrix);    //Default offset position is (0, 0);
 
//Now you can put wBitmap to Image control's Source or use it to create image file.

如果需要把二维码呈现在WinForm或者WPF应用程序中,可以直接把类库拖入工具箱,然后直接在窗体上拖出控件。

C#利用QrCode.Net生成二维码第4张

直接把二维码保存到文件:

QrEncoder qrEncoder = newQrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = newQrCode();
qrEncoder.TryEncode(helloWorld, outqrCode);
 
Renderer renderer = newRenderer(5, Brushes.Black, Brushes.White);
renderer.CreateImageFile(qrCode.Matrix, @"c: empHelloWorld.png", ImageFormat.Png);

将二维码写入Stream:

QrEncoder qrEncoder = newQrEncoder(ErrorCorrectionLevel.H);
QrCode qrCode = newQrCode();
qrEncoder.TryEncode(helloWorld, outqrCode);
 
Renderer renderer = newRenderer(5, Brushes.Black, Brushes.White);
MemoryStream ms = newMemoryStream();
renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.png);

出处:https://www.cnblogs.com/Soar1991/archive/2012/03/30/2426115.html

免责声明:文章转载自《C#利用QrCode.Net生成二维码》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇StartUML 各种类图的例子Linux 串口驱动设计一下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Google API快速生成QR二维码

Google API快速生成QR二维码现在来说生成二维码最简单的方法是使用Google Chart API来实现,再次膜拜Google大神~ Google Chart API是一套可以让你在线生成报表图的系统,通过URL你可以得到各种图表。举个例子:你在浏览器中输入 https://chart.googleapis.com/chart?cht=qr&...

nodejs qr-image 生成二维码

代码示例: var qr_image = require('qr-image') var fs = require('fs') var temp_qrcode = qr_image.image('https://www.baidu.com', { //设置容错率,L(低), M(中,默认), Q(高), H(最高). ec_level:...

Json与List的相互转换

问题由来: 最近由于做一个项目,项目的一个功能就是根据Listview的内容生成一个二维码,然后扫描二维码获取list,再重新显示listview。 核心就是: list—->生成二维码——>获取二维码—–>获取list 生成二维码的方法: http://blog.csdn.net/demonliuhui/article/details/...

php生成二维码

<?php // QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize); // 第二种方式 保存成一个文件,并且打印出来 include 'C:wodewenjianwampwwwphpqrcodephpqrcode.php'; $store_id = 11;$tabl...

基于QRcode创建和识别二维码的研究

至于什么是二维码,大家都使用过,其实比较形象,对比之前的条形码,就很容易理解,就是基于水平方向排列的通过小竖条的宽度不同表示不同的信息,而二维码,表达信息的方式是基于二维的黑白相间(不一定就是黑白,多数看到的可能是黑白,其实颜色是可以随着自己的需要,灵活调整的)的小方块,按照一定的规则排列的一个矩形区域内,形成一个传递信息的编码方式。 二维码(本博客重点介...

iOS系统原生 二维码的生成、扫描和读取(高清、彩色)

由于近期工作中遇到了个需求:需要将一些固定的字段 在多个移动端进行相互传输,所以就想到了 二维码 这个神奇的东东! 现在的大街上、连个摊煎饼的大妈 都有自己的二维码来让大家进行扫码支付。可见现在的二维码使用率多高,不光如此,在很多的社交类的APP 基本都有扫一扫加好友这个功能吧,因此决定学一学这个神奇的东西。 查找了一些资料博客啊发现,iOS7之前 对于开...