ASP.NET MVC中的QR代码生成

前端之家收集整理的这篇文章主要介绍了ASP.NET MVC中的QR代码生成前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有一个.NET API生成 QR Codes,如这一个?

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

解决方法

我写了一个基本的HTML帮助方法来发出正确的< img>标记以利用Google的API。所以,在你的页面(假设ASPX视图引擎)使用这样的东西:
  1. <%: Html.QRCodeImage(Request.Url.AbsolutePath) %>
  2. <%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %>

或者如果要指定像素大小(图像始终为正方形):

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

这里是代码

  1. public static class QRCodeHtmlHelper
  2. {
  3. /// <summary>
  4. /// Produces the markup for an image element that displays a QR Code image,as provided by Google's chart API.
  5. /// </summary>
  6. /// <param name="htmlHelper"></param>
  7. /// <param name="data">The data to be encoded,as a string.</param>
  8. /// <param name="size">The square length of the resulting image,in pixels.</param>
  9. /// <param name="margin">The width of the border that surrounds the image,measured in rows (not pixels).</param>
  10. /// <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>
  11. /// <param name="htmlAttributes">Optional HTML attributes to include on the image element.</param>
  12. /// <returns></returns>
  13. public static MvcHtmlString QRCode(this HtmlHelper htmlHelper,string data,int size = 80,int margin = 4,QRCodeErrorCorrectionLevel errorCorrectionLevel = QRCodeErrorCorrectionLevel.Low,object htmlAttributes = null)
  14. {
  15. if (data == null)
  16. throw new ArgumentNullException("data");
  17. if (size < 1)
  18. throw new ArgumentOutOfRangeException("size",size,"Must be greater than zero.");
  19. if (margin < 0)
  20. throw new ArgumentOutOfRangeException("margin",margin,"Must be greater than or equal to zero.");
  21. if (!Enum.IsDefined(typeof(QRCodeErrorCorrectionLevel),errorCorrectionLevel))
  22. throw new InvalidEnumArgumentException("errorCorrectionLevel",(int)errorCorrectionLevel,typeof (QRCodeErrorCorrectionLevel));
  23.  
  24. var url = string.Format("http://chart.apis.google.com/chart?cht=qr&chld={2}|{3}&chs={0}x{0}&chl={1}",HttpUtility.UrlEncode(data),errorCorrectionLevel.ToString()[0],margin);
  25.  
  26. var tag = new TagBuilder("img");
  27. if (htmlAttributes != null)
  28. tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
  29. tag.Attributes.Add("src",url);
  30. tag.Attributes.Add("width",size.ToString());
  31. tag.Attributes.Add("height",size.ToString());
  32.  
  33. return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
  34. }
  35. }
  36.  
  37. public enum QRCodeErrorCorrectionLevel
  38. {
  39. /// <summary>Recovers from up to 7% erroneous data.</summary>
  40. Low,/// <summary>Recovers from up to 15% erroneous data.</summary>
  41. Medium,/// <summary>Recovers from up to 25% erroneous data.</summary>
  42. QuiteGood,/// <summary>Recovers from up to 30% erroneous data.</summary>
  43. High
  44. }

猜你在找的asp.Net相关文章