ASP.NET MVC 中的二维码生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3793799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:46:12  来源:igfitidea点击:

QR Code generation in ASP.NET MVC

.netasp.net-mvccomputer-visionbarcodeqr-code

提问by Drew Noakes

Is there a .NET API that generates QR Codessuch as this one?

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

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

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

I'd like to display these on pages that I expect my users to print out.

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

回答by Drew Noakes

I wrote a basic HTML helper method to emit the correct <img>tag to take advantage of Google's API. So, on your page (assuming ASPX view engine) use something like this:

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

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

Or if you want to specify the size in pixels (the image is always square):

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

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

Here is the code:

这是代码:

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
}

回答by Jon Skeet

One option is to use the Google Chart Server APIto do it, like this.

一种选择是使用Google Chart Server API来执行此操作,如下所示

For instance, here's the QR code for this very page...

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

No code required :)

不需要代码:)

回答by TheLukeMcCarthy

You might also consider the "Open Source QRCode Library on code project"

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

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

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

回答by Dan Atkinson

There is also a Nuget package available - QRCodeHelperthat's based on the Codeplex QRCode Helperproject.

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