第一步: 獲取QRCode組件jquery
能夠經過vs的nuget管理安裝Gma.QrCodeNet,測試
也能夠直接添加"Gma.QrCodeNet.Encoding.dll"的引用.ui
第二步:封裝操做方法,編寫QRCodeHelper幫助類(直接複製,黏貼便可)編碼
1 /// <summary> 2 /// 含有QR碼的描述類和包裝編碼和渲染 3 /// </summary> 4 public class QRCodeHelper 5 { 6 /// <summary> 7 /// 獲取二維碼 8 /// </summary> 9 /// <param name="strContent">待編碼的字符</param> 10 /// <param name="ms">輸出流</param> 11 /// <param name="moduleSize">大小</param> 12 ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns> 13 public static bool GetQRCode(string strContent,MemoryStream ms, int moduleSize = 12) 14 { 15 ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //偏差校訂水平 16 string Content = strContent;//待編碼內容 17 QuietZoneModules QuietZones = QuietZoneModules.Two; //空白區域 18 var encoder = new QrEncoder(Ecl); 19 QrCode qr; 20 if (encoder.TryEncode(Content, out qr))//對內容進行編碼,並保存生成的矩陣 21 { 22 var render = new GraphicsRenderer(new FixedModuleSize(moduleSize, QuietZones)); 23 render.WriteToStream(qr.Matrix, ImageFormat.Png, ms); 24 } 25 else 26 { 27 return false; 28 } 29 return true; 30 } 31 32 }
第三步: 測試調用,生成二維碼圖片spa
1 using (var ms = new MemoryStream()) 2 { 3 string strContent = "http://www.baidu.com"; 4 QRCodeHelper.GetQRCode(strContent, ms, 12); 5 Response.ContentType = "image/Png"; 6 Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length); 7 Response.End(); 8 }
補充:插件
若是想經過js動態生成二維碼,可以使用jQuery.QRCode插件.說明文檔: https://larsjung.de/jquery-qrcode/code
QRCode組件下載地址: https://pan.baidu.com/s/1slMrQHJorm