隨着互聯網愈來愈生活化,二維碼的使用愈來愈廣泛,不管是掃碼支付仍是掃碼關注引流,彷佛咱們老是離不開二維碼,那麼不少須要推廣的文章或社區想要本身的二維碼,那麼你是否是須要在網站直接提供給用戶呢?不少開發者就在網上百度解決方案,邊作邊踩坑,甚至不少人寫的開發案例都是截圖或者類庫引用都沒說清楚,在這個摸索的途中形成不少時間上的浪費。javascript
尤爲是嘗試新技術那些舊的操做還會有所改變,爲了節約開發時間,咱們把解決方案收入到一個個demo中,方便之後即拿即用。並且這些demo有博客文檔支持,幫助任何人很是容易上手開發跨平臺的.net core。隨着時間的推移,咱們的demo庫會日益強大請及時收藏GitHub。css
1、首先在Common公用項目中引用QRCoder類庫前端
Install-Package QRCoder -Version 1.3.3
2、在Common公用項目中建立QRCoderHelper類java
#region 普通二維碼 /// <summary> /// /// </summary> /// <param name="url">存儲內容</param> /// <param name="pixel">像素大小</param> /// <returns></returns> public static Bitmap GetPTQRCode(string url, int pixel) { QRCodeGenerator generator = new QRCodeGenerator(); QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true); QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData); Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, true); return qrImage; } #endregion #region 帶logo的二維碼 /// <summary> /// /// </summary> /// <param name="url">存儲內容</param> /// <param name="pixel">像素大小</param> /// <returns></returns> public static Bitmap GetLogoQRCode(string url,string logoPath, int pixel) { QRCodeGenerator generator = new QRCodeGenerator(); QRCodeData codeData = generator.CreateQrCode(url, QRCodeGenerator.ECCLevel.M, true); QRCoder.QRCode qrcode = new QRCoder.QRCode(codeData); Bitmap icon = new Bitmap(logoPath); Bitmap qrImage = qrcode.GetGraphic(pixel, Color.Black, Color.White, icon,15,6, true); #region 參數介紹 //GetGraphic方法參數介紹 //pixelsPerModule //生成二維碼圖片的像素大小 ,我這裏設置的是5 //darkColor //暗色 通常設置爲Color.Black 黑色 //lightColor //亮色 通常設置爲Color.White 白色 //icon //二維碼 水印圖標 例如:Bitmap icon = new Bitmap(context.Server.MapPath("~/images/zs.png")); 默認爲NULL ,加上這個二維碼中間會顯示一個圖標 //iconSizePercent //水印圖標的大小比例 ,可根據本身的喜愛設置 //iconBorderWidth // 水印圖標的邊框 //drawQuietZones //靜止區,位於二維碼某一邊的空白邊界,用來阻止讀者獲取與正在瀏覽的二維碼無關的信息 便是否繪畫二維碼的空白邊框區域 默認爲true #endregion return qrImage; } #endregion
注意:若是你想作其它調整能夠參考我第二個方法中的參數介紹進行設置,考慮到你們的難處,我把那些相關參數幫各位查了。jquery
3、添加QRCodeController控制器處理請求並返回二維碼圖片git
private readonly IWebHostEnvironment webHostEnvironment; //private readonly IHostingEnvironment _hostingEnvironment; 3.0以前使用它 public QRCodeController(IWebHostEnvironment _webHostEnvironment) { webHostEnvironment=_webHostEnvironment; } public IActionResult Index() { return View(); } public IActionResult GetPTQRCode(string url, int pixel=5) { url = HttpUtility.UrlDecode(url); Response.ContentType = "image/jpeg"; var bitmap = QRCoderHelper.GetPTQRCode(url, pixel); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Jpeg); return File(ms.ToArray(), "image/png"); } public IActionResult GetLogoQRCode(string url,string logoPath, int pixel = 5) { url = HttpUtility.UrlDecode(url); logoPath= webHostEnvironment.WebRootPath + HttpUtility.UrlDecode(logoPath); Response.ContentType = "image/jpeg"; var bitmap = QRCoderHelper.GetLogoQRCode(url, logoPath, pixel); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, ImageFormat.Jpeg); return File(ms.ToArray(), "image/png"); }
注意:其中IHostingEnvironment是.net core 3.0以前使用獲取目錄位置的,而咱們.net core 3.0已經由IHostingEnvironment變動爲IWebHostEnvironment
來獲取了。github
4、前端請求設計web
<link href="~/Lib/Mobile/jquery.mobile-1.4.5.min.css" rel="stylesheet" /> <script src="~/Lib/Mobile/jquery.mobile-1.4.5.min.js"></script> <script type="text/javascript"> $(function () { PTCreate(); LogoCreate(); }); function PTCreate() { pt = escape($("#PT").val());//防止中文等特殊字符引發問題 ptSize = $("#PTSize").val(); var id = document.getElementById("PTImg"); var str = "../QRCode/GetPTQRCode?url=" + pt + "&pixel=" + ptSize + "&random=" + Math.random(); id.setAttribute("src", str); } function LogoCreate() { logo = escape($("#Logo").val()); logoPath = escape($("#LogoPath").val()); logoSize = $("#LogoSize").val(); var id = document.getElementById("LogoImg"); var str = "../QRCode/GetLogoQRCode?url=" + logo + "&logoPath=" + logoPath + "&pixel=" + logoSize + "&random=" + Math.random(); id.setAttribute("src", str); } </script> <div style="width:60%; margin:auto;text-align:center;"> <h2>普通二維碼</h2> <input type="text" class="form-control" id="PT" placeholder="請輸入字符" value="www.jiyuwu.com"><button onclick="PTCreate();">生成</button> <input type="range" name="points" id="PTSize" value="5" min="1" max="20" onchange="PTCreate();"> <br /> <img id="PTImg" /> <h2>Logo二維碼</h2> <input type="text" class="form-control" id="Logo" placeholder="請輸入字符" value="www.jiyuwu.com"> <input type="text" class="form-control" id="LogoPath" placeholder="logo位置" value="\Lib\Markdown\images\logos\editormd-logo-32x32.png"> <button onclick="LogoCreate();">生成</button> <input type="range" name="points" id="LogoSize" value="5" min="1" max="20" onchange="LogoCreate();"> <img id="LogoImg" /> </div>
5、那麼看下效果吧asp.net
開源地址 動動小手,點個推薦吧!dom
注意:咱們機遇屋該項目將長期爲你們提供asp.net core各類好用demo,旨在幫助.net開發者提高競爭力和開發速度,建議儘早收藏該模板集合項目。