本篇內(nèi)容主要講解“如何使用APS.NET MVC4生成二維碼”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“如何使用APS.NET MVC4生成二維碼”吧!
一、視圖
@{ Layout = null; }Index
二、控制器
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 { ////// 生成二維碼 /// /// 要編碼的內(nèi)容 ///返回二維碼位圖 public static Bitmap QRCodeEncoderUtil(string qrCodeContent) { QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(); qrCodeEncoder.QRCodeVersion = 0; Bitmap img = qrCodeEncoder.Encode(qrCodeContent, Encoding.UTF8);//指定utf-8編碼, 支持中文 return img; } ////// 解析二維碼 /// /// 要解析的二維碼位圖 ///解析后的字符串 public static string QRCodeDecoderUtil(Bitmap bitmap) { QRCodeDecoder decoder = new QRCodeDecoder(); string decodedString = decoder.decode(new QRCodeBitmapImage(bitmap), Encoding.UTF8);//指定utf-8編碼, 支持中文 return decodedString; } } }
到此,相信大家對(duì)“如何使用APS.NET MVC4生成二維碼”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!