ASP.NET MVC中的QR代码生成

31

是否存在一个.NET API,可以生成像这样的QR代码

Meagre human needs a phone to read QR codes.  Ha ha ha.

我想在页面上显示这些二维码,以便用户可以打印出来。


4
二维码上写着:“渺小的人类需要一部手机才能读取二维码。哈哈哈。”太有趣了。 :) - Dan Atkinson
2
我不知道为什么这被设置为离题。我认为它恰好是相关主题... :/ - Dan Atkinson
7个回答

55

我写了一个基本的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
}

2
+1 为有趣的示例字符串 - usr
1
+1 正是我所寻找的。 - Grahame A
1
请注意,根据developers.google.com/chart/infographics,Google Chart QR Code已被弃用。 - Alexandre

28
一种选项是使用Google Chart Server API来生成QR码。

例如,这就是这个页面的QR码...

无需编写代码 :)

详细信息请参阅文档,但您可以从https://chart.googleapis.com/chart?开始,然后添加查询参数:

  • cht=qr:指定要生成QR码
  • chs=size:指定大小,例如200x200
  • chl=data:指定数据,例如URL

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


1
谢谢。我在发布后不久发现了这个API,并将其与ASP.NET MVC帮助程序方法一起封装起来,因为我将从许多地方调用它。如果有帮助其他人的话,代码已经发布在答案中了。 - Drew Noakes
1
快速 URL 更新:http://code.google.com/apis/chart/infographics/docs/qr_codes.html - benpage
1
请注意,根据https://developers.google.com/chart/infographics/ ,Google Chart QR Code已经过时。 - Alexandre
坏链接,这就是为什么仅仅把链接作为答案是一个不好的做法。 - sairfan
1
@sairfan:嗯,这不仅仅是一个链接——它还是一个例子。我已经更新了链接并添加了更多细节。 - Jon Skeet

7

谢谢。第一个链接看起来很有趣。顺便说一下,那个链接已经过时了(我会编辑你的答案)。 - Drew Noakes
您还可以使用以下产品:https://downloaddevtools.com/zh/product/169/download-aspose-barcode-for-dotnet-with-license-keyhttps://downloaddevtools.com/zh/product/2319/download-bytescout-barcode-generator-sdkhttps://downloaddevtools.com/zh/product/3235/download-tec-it-barcode-studio - Hadi Delphi

5

4

3

FYI,它没有强名称。 - Mark Sowul


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接