.NET C#利用ZXing生成、識(shí)別二維碼/條形碼
一、首先下載 ZXing.Net
地址是:http://zxingnet.codeplex.com/releases/view/117068
然后將對(duì)應(yīng)版本 .dll 拖入項(xiàng)目中,再引用之。
主要是用 BarcodeWriter、BarcodeReader。
二、生成二維碼
.NET 平臺(tái)的代碼始終要簡(jiǎn)單些。
QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options.CharacterSet = "UTF-8"; options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的掃描器都支持這種編碼。 options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 糾錯(cuò)級(jí)別 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 具備生成、寫(xiě)入兩個(gè)功能 { MemoryStream ms = new MemoryStream(); { bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ContentType = "image/png"; Response.BinaryWrite(ms.ToArray()); } } Response.End();
糾錯(cuò)級(jí)別:
- 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();
四、識(shí)別二維碼、條形碼
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é)
好了,以上就是這篇文章的全部?jī)?nèi)容了,如果要改變背景顏色、畫(huà)頭像,可以直接在 Bitmap 中畫(huà),希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助
相關(guān)文章
C#自動(dòng)給文章關(guān)鍵字加鏈接實(shí)現(xiàn)代碼
這篇文章主要介紹了C#自動(dòng)給文章關(guān)鍵字加鏈接實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12C#實(shí)現(xiàn)模擬ATM自動(dòng)取款機(jī)功能
這篇文章介紹了C#實(shí)現(xiàn)模擬ATM自動(dòng)取款機(jī)功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享
這篇文章主要介紹了C#獲取Word文檔中所有表格的實(shí)現(xiàn)代碼分享,小編親測(cè)可用,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的代碼詳解
這篇文章主要介紹了C#實(shí)現(xiàn)windows系統(tǒng)重啟和關(guān)機(jī)的的方法,涉及C#調(diào)用windows系統(tǒng)命令實(shí)現(xiàn)控制開(kāi)機(jī)、關(guān)機(jī)等操作的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2024-02-02C#實(shí)現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法
這篇文章主要介紹了C#實(shí)現(xiàn)HSL顏色值轉(zhuǎn)換為RGB的方法,涉及C#數(shù)值判定與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#對(duì)INI文件進(jìn)行讀寫(xiě)操作的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#對(duì)INI文件進(jìn)行讀寫(xiě)操作的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,有需要的小伙伴可以參考一下2024-02-02C#運(yùn)用FileInfo類(lèi)實(shí)現(xiàn)拷貝文件的方法
這篇文章主要介紹了C#運(yùn)用FileInfo類(lèi)實(shí)現(xiàn)拷貝文件的方法,需要的朋友可以參考下2014-07-07