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
QR Code generation in ASP.NET MVC
提问by Drew Noakes
回答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 Mauricio Scheffer
A quick search yields many QRCode libraries (all of them commercial except the first one):
快速搜索会产生许多 QRCode 库(除了第一个之外,所有这些库都是商业的):
- http://www.twit88.com/platform/projects/show/mt-qrcode(previously here)
- http://www.barcodelib.com/net_barcode/barcode_symbologies/qrcode.html
- http://www.businessrefinery.com/products/barcode_net/barcodes/net-qr-code.html
- http://www.componentsource.com/products/dbarcode-net-qr-code/index.html
- http://www.onbarcode.com/products/net_barcode/barcodes/qrcode.html
- http://www.twit88.com/platform/projects/show/mt-qrcode(以前在这里)
- http://www.barcodelib.com/net_barcode/barcode_symbologies/qrcode.html
- http://www.businessrefinery.com/products/barcode_net/barcodes/net-qr-code.html
- http://www.componentsource.com/products/dbarcode-net-qr-code/index.html
- http://www.onbarcode.com/products/net_barcode/barcodes/qrcode.html
回答by TheLukeMcCarthy
You might also consider the "Open Source QRCode Library on code project"
您还可以考虑“代码项目上的开源 QRCode 库”
回答by Dan Atkinson
There is also a Nuget package available - QRCodeHelperthat's based on the Codeplex QRCode Helperproject.
还有一个可用的 Nuget 包 - QRCodeHelper,它基于 Codeplex QRCode Helper项目。
回答by George Mamaladze
回答by Avivo
Here is another simple REST web service:
这是另一个简单的 REST Web 服务:
Example
例子

