代码之家  ›  专栏  ›  技术社区  ›  Drew Noakes

ASP.NET MVC中的二维码生成[关闭]

  •  28
  • Drew Noakes  · 技术社区  · 14 年前
    7 回复  |  直到 12 年前
        1
  •  54
  •   Drew Noakes    13 年前

    我编写了一个基本的HTML助手方法来发出正确的 <img> 标签来利用谷歌的API。因此,在您的页面上(假设为ASPX视图引擎)使用如下内容:

    <%: Html.QRCodeImage(Request.Url.AbsolutePath) %>
    <%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %>
    

    或者,如果要以像素为单位指定大小(图像始终为正方形):

    <%: Html.QRCodeImage(Request.Url.AbsolutePath, size: 92) %>
    

    代码如下:

    public static class QRCodeHtmlHelper
    {
        /// <summary>
        /// Produces the markup for an image element that displays a QR Code image, as provided by Google's chart API.
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="data">The data to be encoded, as a string.</param>
        /// <param name="size">The square length of the resulting image, in pixels.</param>
        /// <param name="margin">The width of the border that surrounds the image, measured in rows (not pixels).</param>
        /// <param name="errorCorrectionLevel">The amount of error correction to build into the image.  Higher error correction comes at the expense of reduced space for data.</param>
        /// <param name="htmlAttributes">Optional HTML attributes to include on the image element.</param>
        /// <returns></returns>
        public static MvcHtmlString QRCode(this HtmlHelper htmlHelper, string data, int size = 80, int margin = 4, QRCodeErrorCorrectionLevel errorCorrectionLevel = QRCodeErrorCorrectionLevel.Low, object htmlAttributes = null)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (size < 1)
                throw new ArgumentOutOfRangeException("size", size, "Must be greater than zero.");
            if (margin < 0)
                throw new ArgumentOutOfRangeException("margin", margin, "Must be greater than or equal to zero.");
            if (!Enum.IsDefined(typeof(QRCodeErrorCorrectionLevel), errorCorrectionLevel))
                throw new InvalidEnumArgumentException("errorCorrectionLevel", (int)errorCorrectionLevel, typeof (QRCodeErrorCorrectionLevel));
    
            var url = string.Format("http://chart.apis.google.com/chart?cht=qr&chld={2}|{3}&chs={0}x{0}&chl={1}", size, HttpUtility.UrlEncode(data), errorCorrectionLevel.ToString()[0], margin);
    
            var tag = new TagBuilder("img");
            if (htmlAttributes != null)
                tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            tag.Attributes.Add("src", url);
            tag.Attributes.Add("width", size.ToString());
            tag.Attributes.Add("height", size.ToString());
    
            return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
        }
    }
    
    public enum QRCodeErrorCorrectionLevel
    {
        /// <summary>Recovers from up to 7% erroneous data.</summary>
        Low,
        /// <summary>Recovers from up to 15% erroneous data.</summary>
        Medium,
        /// <summary>Recovers from up to 25% erroneous data.</summary>
        QuiteGood,
        /// <summary>Recovers from up to 30% erroneous data.</summary>
        High
    }
    
        2
  •  28
  •   Community Egal    7 年前

    一个选项是使用谷歌图表服务器api google chart server api to do it, like this

    例如,这是这个页面的二维码……

    不需要代码:)

    要做到这一点, like this .

    例如,这是这个页面的二维码……

    不需要代码:)

        4
  •  5
  •   TheLukeMcCarthy    13 年前

    您还可以考虑“代码项目上的开源qrcode库”

    http://www.codeproject.com/KB/cs/qrcode.aspx

        5
  •  4
  •   Dan Atkinson    12 年前

    还有一个Nuget包- QRCodeHelper 那是基于密码丛的 QRCode Helper 项目。

        6
  •  3
  •   George Mamaladze    13 年前