引用vs的NuGet 中·QRcode
@{
ViewBag.Title = null;
Layout = null;
}
<html>
<head>
<title>二维码</title>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<style type="text/css">
* {
margin:0px;
border:0px;
}
#concent {
width:650px;
overflow:hidden;
border:1px solid black;
margin:auto;
}
#left
{
float:left;
}
#right
{
float: right;
}
</style>
</head>
<body>
<div id="concent">
<div style="border:1px solid black;width:300px; margin:auto;" id="left">
<div style="width:100px;margin:auto;font-weight:800">生成二维码</div>
<table>
<tr>
<td style="height:40px;width:80px;">
<span>输入信息</span>
</td>
<td >
<textarea style="width:210px;height:100px;border:1px solid black" id="IN"> </textarea>
</td>
</tr>
<tr>
<td>
<input type="button" value="确定" id="bn_sure" style="width:80px;"/>
</td>
<td>
<input type="button" value="取消" id="bn_Cancle"style="width:80px;margin-left:120px;" />
</td>
</tr>
</table>
<div id="QRIMG" style="border:1px solid black;width:300px; height:230px; margin:auto;">
<div style="width:150px;margin:auto;font-weight:800">生成的二维码图片:</div>
@*<img src="#" id="QRImg" />*@
</div>
<div style="width:80px;margin:auto;">
<button id="download" style="width:80px">下载</button>
</div>
</div>
<div style="border:1px solid black;width:300px; margin:auto;" id="right">
<div style="width:100px;margin:auto; font-weight:800">二维码解析</div>
<div>
<input type="file" id="Upload"/>
</div>
<div>
<div id="imgshow" style="height:220px;width:300px;border:1px solid black">
</div>
<div style="width:80px;margin:auto;">
<input type="button" id="uplodBtn" value="确定" style="width:80px;" />
</div>
</div>
<div id="messageinfo">
<div style="width:140px;margin:auto;font-weight:800">二维码解析信息: </div>
<textarea id="messageinfotext" style="width:300px;height:95px;border:1px solid black"> </textarea>
</div>
</div>
</div>
</body>
</html>
js
<script type="text/javascript">
$(function () {
var url = "";
$("#bn_sure").click(function () {
var intext = $("#IN").val();
$.ajax({
url: '../TwoWeiMa/CreateImg',
data: { MessageInfo: intext},
datatype: 'Json',
type: 'post',
success: function (res) {
console.log(res)
if (res.IsTrue)
{
//$("#QRImg").attr('src', src = res.path ); //显示图片
$("#QRIMG").append("<div style='width:200px;margin-top:10px; margin:auto;'> <img src=" + '../' + res.path + " id='load'/></div>")
//$("#QRIMG").append("<div><a href=../" + res.path + ">下载 </a></div>")
//$("#QRIMG").append("<div><button id='downLoad'>下载</button></div>")
url = "../" + res.path;
}
else
{
alert(res.Meg);
}
}
})
})
///
$("#Upload").click(function () {
})
$("#uplodBtn").click(function () {
var formdata = new FormData();
formdata.append("file", $('#Upload')[0].files[0]);
$.ajax({
url: '../TwoWeiMa/QRcodeDECode',
data: formdata,
type: 'post',
contentType: false, // 不设置内容类型
processData: false, // 不处理数据
dataType: "json",
success: function (res) {
if (res.IsTrue)
{
$("#messageinfotext").val(res.Meg)
}
},
error: function ()
{
}
})
})
///下载
$("#download").click(function () {
var intext = $("#IN").val();
let alink = document.createElement("a");
alink.download = intext;//文件名,大部分浏览器兼容,IE10及以下不兼容
alink.href = url;//创建 url地址
alink.click(); //自动点击
})
})
</script>
后台
public ActionResult CreateImg(string MessageInfo) {
bool IsTrue = false;
string Meg = null;
string path = "";
try
{
string str = MessageInfo;
Bitmap bt;
string enCodeString = str;
//生成设置编码实例
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置二维码的规模,默认4
qrCodeEncoder.QRCodeScale = 4;
//设置二维码的版本,默认7
qrCodeEncoder.QRCodeVersion = 7;
//设置错误校验级别,默认中等
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//生成二维码图片
bt = qrCodeEncoder.Encode(enCodeString, Encoding.UTF8);
//二维码图片的名称
string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
//保存二维码图片在photos路径下
path = Server.MapPath("~/photos/") + filename + ".jpg";
bt.Save(path);
//图片控件要显示的二维码图片路径
//this.Image1.ImageUrl = "~/photos/" + filename + ".jpg";
DeCoder(path);
path = "photos/" + filename + ".jpg";
Meg = "二维码生成成功!";
IsTrue = true;
}
catch (Exception ex)
{
Meg = ex.ToString();
IsTrue = false;
}
return Json(new { IsTrue = IsTrue, Meg = Meg, path = path });
}
/// <summary>
/// QR二维码解析
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public ActionResult QRcodeDECode() {
bool IsTrue = false;
string Meg = null;
try
{
string[] FormKey = Request.Form.AllKeys;
foreach (string upload in Request.Files.AllKeys)
{
var file = Request.Files[upload];
string fileExt = "";
string name = file.FileName;
fileExt = name.Split('.')[1];
string FilePath = "UpLoadFile";
string fullPath = Server.MapPath("../" + FilePath);
String pathstr = fullPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + fileExt;
file.SaveAs(pathstr);
Meg = DeCoder(pathstr);
IsTrue = true;
}
}
catch (Exception ex)
{
Meg = ex.ToString();
IsTrue = false;
}
return Json(new { IsTrue = IsTrue, Meg = Meg });
}
/// <summary>
/// 二维码解析
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string DeCoder(string path)
{
string result = "";
try
{
string strSaveDir = path;
//if (!Directory.Exists(strSaveDir))
//{
// Directory.CreateDirectory(strSaveDir);
//}
string strSavePath = Path.Combine(strSaveDir, "wolfy.png");
strSavePath = path;
if (System.IO.File.Exists(strSavePath))
{
QRCodeDecoder decoder = new QRCodeDecoder();
result = decoder.decode(new ThoughtWorks.QRCode.Codec.Data.QRCodeBitmapImage(new Bitmap(System.Drawing.Image.FromFile(strSavePath))), Encoding.UTF8);
}
}
catch (Exception ex)
{
}
return result;
}