Jon Skeet
2010-09-25 12:35:04
Thanks. I found this API shortly after posting and wrapped it up with an ASP.NET MVC helper method, as I'm going to call it from a bunch of places. The code is posted in an answer, in case it helps someone else.
Drew Noakes
2010-09-25 12:56:05
+2
A:
A quick search yields many QRCode libraries (all of them commercial except the first one):
- 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
Mauricio Scheffer
2010-09-25 12:36:51
Thanks. The first link looks interesting. By the way, that link is out of date (I'll edit your answer.)
Drew Noakes
2010-09-25 12:58:18
+2
A:
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.QRCodeImage(Request.Url.AbsolutePath) %>
Or if you want to specify the size:
<%: 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, [NotNull] 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
}
Drew Noakes
2010-09-25 12:54:46