.NET C#利用ZXing生成、識別二維碼/條形碼
一、首先下載 ZXing.Net
地址是:http://zxingnet.codeplex.com/releases/view/117068
然后將對應(yīng)版本 .dll 拖入項(xiàng)目中,再引用之。
主要是用 BarcodeWriter、BarcodeReader。
二、生成二維碼
.NET 平臺(tái)的代碼始終要簡單些。
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的掃描器都支持這種編碼。
options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 糾錯(cuò)級別
options.Width = 300;
options.Height = 300;
options.Margin = 1;
// options.Hints,更多屬性,也可以在這里添加。
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
Response.Clear();
using (Bitmap bmp = writer.Write("http://www.cftea.com")) // Write 具備生成、寫入兩個(gè)功能
{
MemoryStream ms = new MemoryStream();
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ContentType = "image/png";
Response.BinaryWrite(ms.ToArray());
}
}
Response.End();
糾錯(cuò)級別:
- L - 約 7% 糾錯(cuò)能力。
- M - 約 15% 糾錯(cuò)能力。
- Q - 約 25% 糾錯(cuò)能力。
- H - 約 30% 糾錯(cuò)能力。
三、生成條形碼
QrCodeEncodingOptions options = new QrCodeEncodingOptions();
options.CharacterSet = "UTF-8";
options.Width = 300;
options.Height = 50;
options.Margin = 1;
options.PureBarcode = false; // 是否是純碼,如果為 false,則會(huì)在圖片下方顯示數(shù)字
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.CODE_128;
writer.Options = options;
Response.Clear();
using (Bitmap bmp = writer.Write("12345678"))
{
MemoryStream ms = new MemoryStream();
{
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Response.ContentType = "image/png";
Response.BinaryWrite(ms.ToArray());
}
}
Response.End();
四、識別二維碼、條形碼
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
using (Bitmap bmp = new Bitmap("D:\\qr.png"))
{
Result result = reader.Decode(bmp);
Response.Write(result.Text);
}
總結(jié)
好了,以上就是這篇文章的全部內(nèi)容了,如果要改變背景顏色、畫頭像,可以直接在 Bitmap 中畫,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助
相關(guān)文章
利用C#編寫Linux守護(hù)進(jìn)程實(shí)例代碼
如今的編程是一場程序員和上帝的競賽,程序員要開發(fā)出更大更好、傻瓜都會(huì)用到軟件,下面這篇文章主要給大家介紹了關(guān)于利用C#編寫Linux守護(hù)進(jìn)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01
C#使用IronPython調(diào)用python代碼的實(shí)現(xiàn)示例
本文主要介紹了在C#中使用IronPython調(diào)用Python代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解
這篇文章主要介紹了C# web.config之<customErrors>節(jié)點(diǎn)說明案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08

