C# 根據字符串生成二維碼

1.先下載NuGet包(ZXing.Net)

2.新建控制器及編寫後臺代碼

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXing;
using ZXing.QrCode;

namespace WebApplication1.Controllers
{
    public class StrController : Controller
    {
        // GET: Str
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 生成二維碼方法
        /// </summary>
        /// <param name="text">輸入的字符串</param>
        /// <param name="width">二維碼寬度</param>
        /// <param name="height">二維碼高度</param>
        /// <returns></returns>
        public string QRcode(string text, string width, string height)
        {
            string Response = "";
            try
            {
                BarcodeWriter writer = new BarcodeWriter();
                writer.Format = BarcodeFormat.QR_CODE;
                QrCodeEncodingOptions options = new QrCodeEncodingOptions();
                options.DisableECI = true;
                //設置內容編碼
                options.CharacterSet = "UTF-8";
                //將傳來的值賦給二維碼的寬度和高度
                options.Width = Convert.ToInt32(width);
                options.Height = Convert.ToInt32(height);
                //設置二維碼的邊距,單位不是固定像素
                options.Margin = 1;
                writer.Options = options;

                Bitmap map = writer.Write(text);
                string di = text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
                //二維碼會顯示在桌面(你也想顯示在桌面的話,要改一下路徑)
                string path = Path.Combine("C:\\Users\\zhulin\\Desktop", di);
                map.Save(path, ImageFormat.Png);
                map.Dispose();
                Response = "二維碼生成成功!";
            }
            catch (Exception)
            {
                Response = "二維碼生成失敗!";
            }
            return Response;
        }
    }
}

3.前端

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Scripts/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-3.3.1.min.js"></script>
    <script src="~/Scripts/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btn").click(function () {
                var w = $("#wd").val();
                var h = $("#hg").val();
                var text = $("#tx").val();
                $.ajax({
                    url: "/Str/QRcode",
                    data: "text=" + text + "&width=" + w + "&height=" + h,
                    success: function (e) {
                        alert(e);
                    }
                });
            });
        })

    </script>
</head>
<body>

    <div style="margin-top:20px;margin-left:20px;">
        <p>高度:<input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="wd" /><span style="margin-left:10px;">寬度:</span><input type="text" style="width:60px;height:32px;border:1px solid #66b1ff;margin-left:3px;" id="hg" /></p>
        <input type="text" style="width:200px;height:32px;border:1px solid #66b1ff;" id="tx" placeholder="請輸入字符串..." /><button type="button" class="btn btn-info" id="btn" style="margin-left:5px;margin-top:-1px;height:33px;">提交</button>
    </div>
</body>
</html>

4.效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章