31

是否有 .NET API 可以生成这样的二维码

微薄的人类需要一部手机来读取二维码。 哈哈哈。

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

4

7 回答 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
}
于 2010-09-25T12:54:46.253 回答
28

一种选择是使用Google Chart Server API来完成。

例如,这是该页面的 QR 码...

不需要代码:)

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

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

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

于 2010-09-25T12:35:04.417 回答
5

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

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

于 2011-02-11T17:45:58.683 回答
4

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

于 2011-07-10T14:51:23.863 回答
3

试试http://qrcodenet.codeplex.com

于 2011-09-29T21:42:36.553 回答
0

这是另一个简单的 REST Web 服务:

例子

http://www.esponce.com/api/v3/generate?content=Meagre+human+needs+a+phone+to+read+QR+codes.+Ha+ha+ha.&size=200x200

于 2012-09-04T08:19:06.253 回答