濮阳杆衣贸易有限公司

主頁 > 知識庫 > APS.NET MVC4生成二維碼簡單解析

APS.NET MVC4生成二維碼簡單解析

熱門標(biāo)簽:南昌自動外呼系統(tǒng)線路 云南外呼系統(tǒng)代理 辦公用地圖標(biāo)注網(wǎng)點怎么操作 寧德防封版電銷卡 上海市三維地圖標(biāo)注 西寧電銷外呼系統(tǒng)公司 海東防封電銷卡 聊城智能電銷機器人電話 安陸市地圖標(biāo)注app

一、視圖

@{
 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下二維碼的生成過程,希望對大家的學(xué)習(xí)有所幫助,制作屬于自己的二維碼。

下載源碼:APS.NET MVC4二維碼

您可能感興趣的文章:
  • javaScript矢量圖表庫-gRaphael幾行代碼實現(xiàn)精美的條形圖/餅圖/點圖/曲線圖
  • 基于C#生成條形碼操作知識匯總附源碼下載

標(biāo)簽:南寧 汕尾 崇左 衢州 青海 洛陽 贛州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《APS.NET MVC4生成二維碼簡單解析》,本文關(guān)鍵詞  APS.NET,MVC4,生成,二維,碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《APS.NET MVC4生成二維碼簡單解析》相關(guān)的同類信息!
  • 本頁收集關(guān)于APS.NET MVC4生成二維碼簡單解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    绍兴市| 成都市| 玛沁县| 临沭县| 赣榆县| 卢湾区| 固原市| 古丈县| 浦东新区| 鞍山市| 渝北区| 石城县| 吴旗县| 桦甸市| 筠连县| 仙居县| 贵州省| 阳东县| 岚皋县| 兰坪| 藁城市| 信阳市| 仪陇县| 神木县| 南岸区| 隆子县| 宾川县| 奉节县| 景德镇市| 汶川县| 开封县| 万全县| 平罗县| 连州市| 屏东市| 承德市| 新民市| 伊川县| 泾阳县| 馆陶县| 扎赉特旗|