(编辑:jimmy 日期: 2025/11/1 浏览:2)
一、视图
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="/UploadFiles/2021-04-02/jquery-1.8.2.min.js">
二、控制器
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">要编码的内容</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二维码