APS.NET MVC4生成二維碼簡單解析
一、視圖
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function () { //生成 $('#createBtn').click(function () { createImage(); }); //解析 $('#analysisBtn').click(function () { analysisImage(); }); }); //生成二維碼 function createImage() { var content = $('#content').val(); //ajax開始 $.post("/ORCode/GetORImage/", { "content": content }, function (data) { $('#imgDiv').empty(); $('#imgDiv').append('<img id="ORImage" />'); $("#ORImage").attr("src", data); }); //ajax結(jié)束 } //解析二維碼 function analysisImage() { //獲取圖片名稱 var imageUrl = $('#ORImage').attr("src"); var array = imageUrl.split('/'); var imageName = array[array.length - 1]; //如:123.jpg //ajax開始 $.post("/ORCode/GetORImageContent/", { "imageName": imageName }, function (data) { $('#ORCodeContent').empty(); $('#ORCodeContent').text(data); }); //ajax結(jié)束 } </script> </head> <body> <div style="text-align:center;"> <div> <textarea style="width:230px;height:120px;" id="content"></textarea> <br /> <input id="createBtn" type="button" value="生成二維碼" /> </div> <div> <textarea style="width:230px;height:120px;" id="ORCodeContent"></textarea> <br /> <input id="analysisBtn" type="button" value="解析二維碼" /> </div> <div style="margin-top:20px;" id="imgDiv"> </div> </div> </body> </html>
二、控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Common; using System.Drawing; namespace QRCodeDemo.Controllers { public class ORCodeController : Controller { // // GET: /ORCode/ public ActionResult Index() { return View(); } [HttpPost] public ActionResult GetORImage(string content) { //if (string.IsNullOrEmpty(content)) //{ // return Content(""); //} string timeStr = DateTime.Now.ToFileTime().ToString(); Bitmap bitmap = QRCodeOp.QRCodeEncoderUtil(content); string fileName = Server.MapPath("~") + "Content\\Images\\QRImages\\" + timeStr + ".jpg"; bitmap.Save(fileName);//保存位圖 string imageUrl = "/Content/Images/QRImages/" + timeStr + ".jpg";//顯示圖片 return Content(imageUrl); } [HttpPost] public ActionResult GetORImageContent(string imageName) { string fileUrl = Server.MapPath("~") + "Content\\Images\\QRImages\\" + imageName; Bitmap bitMap = new Bitmap(fileUrl); string content = QRCodeOp.QRCodeDecoderUtil(bitMap); return Content(content); } } }
三、二維碼生成工具類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; using System.Drawing; namespace Common { public class QRCodeOp { /// <summary> /// 生成二維碼 /// </summary> /// <param name="qrCodeContent">要編碼的內(nèi)容</param> /// <returns>返回二維碼位圖</returns> public static Bitmap QRCodeEncoderUtil(string qrCodeContent) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeVersion = 0; Bitmap img = qrCodeEncoder.Encode(qrCodeContent, Encoding.UTF8);//指定utf-8編碼, 支持中文 return img; } /// <summary> /// 解析二維碼 /// </summary> /// <param name="bitmap">要解析的二維碼位圖</param> /// <returns>解析后的字符串</returns> public static string QRCodeDecoderUtil(Bitmap bitmap) { QRCodeDecoder decoder = new QRCodeDecoder(); string decodedString = decoder.decode(new QRCodeBitmapImage(bitmap), Encoding.UTF8);//指定utf-8編碼, 支持中文 return decodedString; } } }
以上就是簡單解析了APS.NET MVC4下二維碼的生成過程,希望對大家的學(xué)習(xí)有所幫助,制作屬于自己的二維碼。
下載源碼:APS.NET MVC4二維碼
相關(guān)文章
asp.net 頁面逐步呈現(xiàn)的方法總結(jié)
分塊編碼 ( chunked encoding )就是讓 response 分塊編碼進(jìn)行傳輸。response 分塊編碼,可以先傳輸一部分不需要處理的 html 代碼到客戶端,等其他耗時(shí)代碼執(zhí)行完畢后再傳輸另外的 html 代碼。2010-06-06asp.net GridView模板列中實(shí)現(xiàn)選擇行功能
近來在項(xiàng)目中用到了GridView控件,用它實(shí)現(xiàn)添加、修改、刪除、選擇、顯示復(fù)雜表頭等功能2010-07-07asp.net(C#)函數(shù)對象參數(shù)傳遞的問題
我們知道在.net里class是引用類型,在函數(shù)參數(shù)表中的對象傳遞的都是對象的引用,所以在函數(shù)體內(nèi)對其對象參數(shù)的修改會(huì)影響函數(shù)外對應(yīng)的對象本身,例如下面的程序.2009-12-12.NET Core之微信支付之公眾號(hào)、H5支付詳解
這篇文章主要介紹了.NET Core之微信支付之公眾號(hào)、H5支付篇,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03.NET Core中的HttpClientFactory類用法詳解
本文詳細(xì)講解了.NET Core中的HttpClientFactory類的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03asp.net運(yùn)算符之邏輯運(yùn)算符以及其他運(yùn)算符介紹與實(shí)例
在.net中運(yùn)算符分類很多種類型,包括有我們常用的boolean型運(yùn)算符,通用的運(yùn)行符有 ==、!=、<、>、<=、>=、binary +、binary -、^、&、|、~、++、-- 和 sizeof()2013-08-08