Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QR Code generation in ASP.NET MVC [closed]

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

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.

like image 555
Drew Noakes Avatar asked Sep 25 '10 12:09

Drew Noakes


People also ask

Does generated QR code expire?

No, QR codes do not have an expiration date. The QR code has a Quick Link behind it. As long as the Quick Link is active, the QR code will continue to work.

How do I reactivate QR code generator?

If you want to reactivate a specific QR Code, you need to upgrade the account the QR Code was created in. To upgrade, log in to your account and go to Account - Features & Pricing. It's not possible to reactivate a QR Code by creating and upgrading a new account.

What happens when QR code generator trial ends?

Once your trial expires, Dynamic QR Codes will deactivate and lead to a service page. If you change your mind, you can log in to your account at a later time and upgrade to one of our plans.


2 Answers

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) %> <%: 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 } 
like image 136
Drew Noakes Avatar answered Oct 02 '22 12:10

Drew Noakes


One option is to use the Google Chart Server API to do it.

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

No code required :)

There are more details in the linked documentation, but you start with a URL of https://chart.googleapis.com/chart?, then add query parameters of:

  • cht=qr: Specify that you want a QR code
  • chs=size: Specify the size, e.g. 200x200
  • chl=data: Specify the data, e.g. a URL

There are other query parameters for output encoding and error correction.

like image 38
Jon Skeet Avatar answered Oct 02 '22 12:10

Jon Skeet