zoukankan      html  css  js  c++  java
  • asp.net mvc 生成二维码

    生成二维码,帮助类:

    using Gma.QrCodeNet.Encoding;
    using Gma.QrCodeNet.Encoding.Windows.Render;
    using System;
    using System.Collections.Generic;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Piano.Utility.Common
    {
       public  class QRCodeHelper
        {
            /// <summary>
            /// 获取二维码
            /// </summary>
            /// <param name="strContent">待编码的字符</param>
            /// <param name="ms">输出流</param>
            ///<returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
            public static bool GetQRCode(string strContent, MemoryStream ms)
            {
                ErrorCorrectionLevel Ecl = ErrorCorrectionLevel.M; //误差校正水平 
                string Content = strContent;//待编码内容
                QuietZoneModules QuietZones = QuietZoneModules.Two;  //空白区域 
                int ModuleSize = 12;//大小
                var encoder = new QrEncoder(Ecl);
                QrCode qr;
                if (encoder.TryEncode(Content, out qr))//对内容进行编码,并保存生成的矩阵
                {
                    var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
                    render.WriteToStream(qr.Matrix, ImageFormat.Png, ms);
                }
                else
                {
                    return false;
                }
                return true;
            } 
        }
    }

    使用方法(此处是MVC5.2.0模式下):

     //二维码生成
            public ActionResult getQrCode()
            {
    
                // Render the QR code as an image
                using (var ms = new MemoryStream())
                {
                    string url = string.Format(ConfigurationManager.AppSettings["WxgzhUrl"], UserValidator.GetInstituteId());
                    string stringtest = url;
                    QRCodeHelper.GetQRCode(stringtest, ms);
                    Response.ContentType = "image/Png";
                    Response.OutputStream.Write(ms.GetBuffer(), 0, (int)ms.Length);
                    Response.End();
                }
    
                return View();
            }

    前端调用:

     <img src="/My/getQrCode" alt="" style="160px;height:160px;" /><span class="remindmsg">扫一扫查看</span>
  • 相关阅读:
    python之字典dict
    python之 tuple元组
    python之列表list
    数字图像处理
    深度神经网络优化
    神经网络的前向后向及更新
    0220 kd树(鸢尾花分类)
    024 查看数据库的编码格式
    208 MySQL性能分析之Explain
    207 MySQL索引的数据结构B+树介绍
  • 原文地址:https://www.cnblogs.com/MissQing/p/6477742.html
Copyright © 2011-2022 走看看