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

1、視圖javascript

@{
    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 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>

 

2、控制器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);
        }
} }

 

3、二維碼生成工具類java

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;
        }
    }
}

 

4、效果jquery

 

5、源碼下載ajax

APS.NET MVC4生成二維碼簡單Demo源碼下載app

 

6、相關資源推薦asp.net

1.asp.net 生成、解析條形碼和二維碼工具

2.二維碼生成與解析代碼實現 post

3..NET 二維碼生成(ThoughtWorks.QRCode)編碼

4.[開源]C#二維碼生成解析工具,可添加自定義Logo

5.QRCode二維碼編碼支持中文生成二維碼的源碼Asp.Net(C#)  

相關文章
相關標籤/搜索