【问题标题】:QR Code generation in ASP.NET MVC [closed]ASP.NET MVC 中的二维码生成 [关闭]
【发布时间】:2011-04-17 03:59:43
【问题描述】:

是否有像这样生成QR Codes 的.NET API?

我想在希望用户打印出来的页面上显示这些内容。

【问题讨论】:

  • 二维码读取:“微不足道的人类需要一部手机来读取二维码。哈哈哈。”迷人的。 :)
  • 我不知道为什么这被设置为离题。我发现它完全符合主题...:/

标签: .net asp.net-mvc computer-vision barcode qr-code


【解决方案1】:

我编写了一个基本的 HTML 辅助方法来发出正确的 <img> 标记以利用 Google 的 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
}

【讨论】:

  • +1 表示有趣的示例字符串
  • +1 正是我想要的。
  • 请注意,根据这个 developers.google.com/chart/infographics Google Chart QR Code 已被弃用
【解决方案2】:

一种选择是使用Google Chart Server API 来执行此操作。

例如,这是该页面的二维码...

无需代码 :)

链接文档中有更多详细信息,但您以https://chart.googleapis.com/chart?的URL开头,然后添加查询参数:

  • cht=qr:指定要二维码
  • chs=size:指定大小,例如200x200
  • chl=data:指定数据,例如一个网址

还有其他查询参数用于输出编码和纠错。

【讨论】:

  • 谢谢。我在发布后不久发现了这个 API,并用一个 ASP.NET MVC 辅助方法将它包装起来,因为我要从很多地方调用它。该代码发布在答案中,以防对其他人有所帮助。
  • 请注意,developers.google.com/chart/infographics Google Chart QR 码已被弃用
  • 链接断开,这就是为什么只将链接作为答案是一种不好的做法。
  • @sairfan:嗯,这不仅仅是只是一个链接——它也是一个例子。不过,我已经更新了链接并添加了更多详细信息。
【解决方案3】:

【讨论】:

  • 谢谢。第一个链接看起来很有趣。顺便说一句,该链接已过时(我将编辑您的答案。)
【解决方案4】:

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

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

【讨论】:

    【解决方案5】:

    还有一个可用的 Nuget 包 - QRCodeHelper,它基于 Codeplex QRCode Helper 项目。

    【讨论】:

      【解决方案6】:

      【讨论】:

      • 仅供参考,它不是强名称
      【解决方案7】:
      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多