快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

如何使用APS.NETMVC4生成二维码-创新互联

本篇内容主要讲解“如何使用APS.NET MVC4生成二维码”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用APS.NET MVC4生成二维码”吧!

我们提供的服务有:成都网站设计、成都网站建设、微信公众号开发、网站优化、网站认证、松桃ssl等。为超过千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的松桃网站制作公司

一、视图


@{
 Layout = null;
}




 
 Index
 
 
 $(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('');
   $("#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结束
 }
 


 
 
          
 
          
   
 

二、控制器


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
 {
 ///  
 /// 生成二维码 
 ///  
 /// 要编码的内容 
 /// 返回二维码位图 
 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;
 }
 }
}

如何使用APS.NET MVC4生成二维码

到此,相信大家对“如何使用APS.NET MVC4生成二维码”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


标题名称:如何使用APS.NETMVC4生成二维码-创新互联
当前网址:http://6mz.cn/article/didjdo.html

其他资讯