C#利用QrCode.Net生成二維碼(Qr碼)的方法
現(xiàn)在網(wǎng)上很多應(yīng)用都是用二維碼來分享網(wǎng)址或者其它的信息。尤其在移動(dòng)領(lǐng)域,二維碼更是有很大的應(yīng)用場景。因?yàn)轫?xiàng)目的需要,需要在網(wǎng)站中增加一個(gè)生成二維碼分析網(wǎng)址的功能,在谷歌大幅度抽筋的情況下無奈使用百度。百度N多,找到一些項(xiàng)目,但是可用性不強(qiáng)。(有一個(gè)項(xiàng)目是用VS2005開發(fā)的,在2010中調(diào)試不開。)終于在codeplex上找到一個(gè)“神器”,這個(gè)“神器”可以很方便的生成二維碼,速度那是相當(dāng)?shù)目?,并且可支持中文,遵從MIT協(xié)議。
QrCode.Net是一個(gè)使用C#編寫的用于生成二維碼圖片的類庫,使用它可以非常方便的為WinForm、WebForm、WPF、Silverlight和Windows Phone 7應(yīng)用程序提供二維碼編碼輸出功能??梢詫⒍S碼文件導(dǎo)出為eps格式。
項(xiàng)目地址為:http://qrcodenet.codeplex.com
QrCode.Net不再采用http://code.google.com/p/zxing/ ZXing的端口,新的版本將有更好的性能。
測試結(jié)果如下(微秒):
輸入字符串長度:74個(gè)
EC performance 1000 Tests~ QrCode.Net: 3929 ZXing: 5221
同時(shí),QrCode.Net可以對(duì)字符串進(jìn)行分析,決定是否使用UTF-8編碼。(比如使用中文的時(shí)候。)
QrCode使用方法:
新建項(xiàng)目添加對(duì)類庫的引用,然后引入Gma.QrCodeNet.Encoding命名空間。
using Gma.QrCodeNet.Encoding;
在控制臺(tái)中輸出二維碼:
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();
此代碼將產(chǎn)生以下輸出:
在Graphics上繪制二維碼:
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.Black, Brushes.White); 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); }
在WriteableBitmap上繪制二維碼:
const string helloWorld = "Hello World!"; QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(helloWorld, out qrCode); const int moduleSizeInPixels = 5; Renderer renderer = new Renderer(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. int pixelSize = qrCode.Matrix == null ? renderer.Measure(21) : renderer.Measure(qrCode.Matrix.Width); WriteableBitmap wBitmap = new WriteableBitmap(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.
如果需要把二維碼呈現(xiàn)在WinForm或者WPF應(yīng)用程序中,可以直接把類庫拖入工具箱,然后直接在窗體上拖出控件。
直接把二維碼保存到文件:
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(helloWorld, out qrCode); Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White); renderer.CreateImageFile(qrCode.Matrix, @"c:\temp\HelloWorld.png", ImageFormat.Png);
將二維碼寫入Stream:
QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.H); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(helloWorld, out qrCode); Renderer renderer = new Renderer(5, Brushes.Black, Brushes.White); MemoryStream ms = new MemoryStream(); renderer.WriteToStream(qrCode.Matrix, ms, ImageFormat.png);
以上所述是小編給大家介紹的C#使用QrCode.Net生成二維碼(Qr碼),希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- c# 生成二維碼的示例
- C#如何用ThoughtWorks生成二維碼
- C# 根據(jù)字符串生成二維碼的實(shí)例代碼
- C#實(shí)現(xiàn)掃描槍掃描二維碼并打印(實(shí)例代碼)
- C#基于QRCode實(shí)現(xiàn)動(dòng)態(tài)生成自定義二維碼圖片功能示例
- C#生成帶二維碼的專屬微信公眾號(hào)推廣海報(bào)實(shí)例代碼
- C#二維碼圖片識(shí)別代碼
- C#利用ZXing.Net生成條形碼和二維碼
- C#生成帶logo的二維碼
- C#生成二維碼的方法
- .NET C#利用ZXing生成、識(shí)別二維碼/條形碼
- C#實(shí)現(xiàn)將網(wǎng)址生成二維碼圖片方法介紹
相關(guān)文章
C#簡單實(shí)現(xiàn)在網(wǎng)頁上發(fā)郵件的案例
本文分享一個(gè)C#利用SMTP發(fā)送郵件的案例,提供了前后臺(tái)代碼,方便大家學(xué)習(xí)。2016-03-03c#獲取當(dāng)前年的周數(shù)及當(dāng)前月的天數(shù)示例代碼
本篇文章主要是對(duì)c#獲取當(dāng)前年的周數(shù)及當(dāng)前月的天數(shù)示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法
今天小編就為大家分享一篇關(guān)于C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10WPF實(shí)現(xiàn)動(dòng)畫效果(一)之基本概念
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之基本概念,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06