如何使用APS.NETMVC4生成二維碼-創(chuàng)新互聯(lián)

本篇內容主要講解“如何使用APS.NET MVC4生成二維碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“如何使用APS.NET MVC4生成二維碼”吧!

我們提供的服務有:成都網站設計、成都網站建設、微信公眾號開發(fā)、網站優(yōu)化、網站認證、松桃ssl等。為超過千家企事業(yè)單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的松桃網站制作公司

一、視圖


@{
 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結束
 }

 //解析二維碼
 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結束
 }
 </script>
</head>
<body>
 <div >
 <div>
  <textarea  id="content"></textarea>
  <br />
  <input id="createBtn" type="button" value="生成二維碼" />
 </div>
 <div>
  <textarea  id="ORCodeContent"></textarea>
  <br />
  <input id="analysisBtn" type="button" value="解析二維碼" />
 </div>
 <div  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">要編碼的內容</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生成二維碼”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

名稱欄目:如何使用APS.NETMVC4生成二維碼-創(chuàng)新互聯(lián)
網站路徑:http://bm7419.com/article10/didjdo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供網站內鏈、搜索引擎優(yōu)化網站設計公司、Google、網站維護、品牌網站制作

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網站建設