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下二維碼的生成過程,希望對大家的學習有所幫助,制作屬于自己的二維碼。
下載源碼:APS.NET MVC4二維碼
相關(guān)文章
.net中string無重復數(shù)字的實現(xiàn)方法
今天做項目的時候,用js獲得了勾選的checkbox放在了hiddenfile里,然而hiddenfile的值變成了類似:“1,1,1,3,3,2,4,5,5,5”,后臺獲取的時候,只保留不重復的數(shù)字,于是想了一想;直接上代碼了。2013-04-04
ASP.NET將文件寫到另一服務(wù)器(圖文教程)及注意事項
有時我們需要將來自于客戶端的文件上傳到WEB服務(wù)器端,并在服務(wù)端將文件存儲到第三方文件服務(wù)器中存儲,既然有需求,那就有實現(xiàn)了,感興趣的你可以了解此文,或許對你學習asp.net 起到很好的作用哦2013-01-01
MVC默認路由實現(xiàn)分頁(PagerExtend.dll下載)
這篇文章主要介紹了MVC默認路由實現(xiàn)分頁,采用bootstrap的樣式,文末提供了PagerExtend.dll下載地址,感興趣的小伙伴們可以參考一下2016-07-07
使用JavaScript代碼實現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項目的時候,會用到反選這個功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實現(xiàn)功能,今天我想換一種語言實現(xiàn)一下,于是我就用JavaScript研究了一下怎么實現(xiàn)這個功能2011-12-12
Ajax Control Toolkit BalloonPopup的使用實例及效果
Ajax Control Toolkit 工具包的簡介及獲取方式等,BalloonPopup 控件可以顯示一個彈出層,里面可以包含很多內(nèi)容。2013-02-02
關(guān)于.net環(huán)境下跨進程、高頻率讀寫數(shù)據(jù)的問題
最近老大教給我一個項目,項目要求高頻次地讀寫數(shù)據(jù),數(shù)據(jù)量也不是很大,難點在于這個規(guī)模的熱點數(shù)據(jù),變化非常頻繁,下面把我的處理方法分享到腳本之家平臺,對.net跨進程高頻率讀寫數(shù)據(jù)相關(guān)知識感興趣的朋友跟隨小編一起學習下吧2021-05-05
ASP.NET Core3.1 Ocelot認證的實現(xiàn)
這篇文章主要介紹了ASP.NET Core3.1 Ocelot認證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫
這篇文章主要介紹了在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

