ASP.NET MVC中的QR代码生成
发布时间:2020-05-23 06:36:28 所属栏目:asp.Net 来源:互联网
导读:有一个.NET API生成 QR Codes,如这一个? 我想在我希望我的用户打印出来的页面上显示这些。 我写了一个基本的HTML帮助方法来发出正确的 img标记以利用Google的API。所以,在你的页面(假设ASPX视图引擎)使用这样的东西: %: Html.QRCodeImage(Request.Url.Abs
|
有一个.NET API生成 QR Codes,如这一个? 我想在我希望我的用户打印出来的页面上显示这些。 解决方法我写了一个基本的HTML帮助方法来发出正确的< img>标记以利用Google的API。所以,在你的页面(假设ASPX视图引擎)使用这样的东西:<%: Html.QRCodeImage(Request.Url.AbsolutePath) %>
<%: Html.QRCodeImage("Meagre human needs a phone to read QR codes. Ha ha ha.") %>
或者如果要指定像素大小(图像始终为正方形): <%: Html.QRCodeImage(Request.Url.AbsolutePath,size: 92) %> 这里是代码: 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}",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
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 转换常规的MVC网站,用于电话沟通
- asp.net – 通过ASP .Net成员身份进行用户登录
- asp.net-mvc – 如何在.NET MVC3 HTML表单中的必需可空Date
- asp.net-mvc-2 – ASP.NET-MVC2预览1:有任何重大变化吗?
- asp.net-mvc – MVC 5:Asp.net身份:如何建模UserRole
- asp.net – 从请求中获取所有发布数据
- 实时监控ASP.NET Web应用程序的工具和方法?
- asp.net-mvc – LINQ将DateTime转换为字符串
- asp.net-mvc – VS2013不编译ASP.NET MVC5视图
- asp.net – Tridion分析和个性化错误:用户不能为空.请确保
推荐文章
站长推荐
- asp.net-mvc – 如何在ActionFilterAttribute AS
- ASP.NET UserControl不初始化子控件
- asp.net – 选择下拉列表项目findbytext没有区分
- asp.net – 多数据库同步 – 带消息背板的Signal
- asp.net-core – 在Visual Studio 2015 for ASP.
- asp.net-mvc – ASP MVC 3 RequireHttps属性将所
- asp.net – 检查是否在集成管道模式
- asp.net – 如何使我的硒测试不那么脆弱?
- asp.net – Web.config:在xml属性中放一个注释
- asp.net – 表达式标记中的问题,用于绑定字符串变
热点阅读
