一、首先下載 ZXing.Net
地址是:http://zxingnet.codeplex.com/releases/view/117068
然后將對應版本 .dll 拖入項目中,再引用之。
主要是用 BarcodeWriter、BarcodeReader。
二、生成二維碼
.NET 平臺的代碼始終要簡單些。
QrCodeEncodingOptions options = new QrCodeEncodingOptions();options.CharacterSet = "UTF-8";options.DisableECI = true; // Extended Channel Interpretation (ECI) 主要用于特殊的字符集。并不是所有的掃描器都支持這種編碼。options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H; // 糾錯級別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 具備生成、寫入兩個功能{ MemoryStream ms = new MemoryStream(); { bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.ContentType = "image/png"; Response.BinaryWrite(ms.ToArray()); }}Response.End();
糾錯級別:
三、生成條形碼
QrCodeEncodingOptions options = new QrCodeEncodingOptions();options.CharacterSet = "UTF-8";options.Width = 300;options.Height = 50;options.Margin = 1;options.PureBarcode = false; // 是否是純碼,如果為 false,則會在圖片下方顯示數字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);}
總結
好了,以上就是這篇文章的全部內容了,如果要改變背景顏色、畫頭像,可以直接在 Bitmap 中畫,希望本文的內容對大家的學習或者工作能帶來一定的幫助
新聞熱點
疑難解答