QRCode
这个星期, 领导要我总结项目中用到的一些技术, 然后交付文档. 嘿嘿, 奉命整理.
二维码, 相信很多项目中都会要求生成这个, 然后由手机端去扫描, 或存储一些详情信息, 或存储一条链接, 可以快捷访问.
一、示例
public ActionResult QrCode()
{
var s = CreateQr("策士", "", "男", DateTime.Now.AddYears(-), ""); return File(s, "image/jpeg,image/png");
} /// <summary>
/// 生成二维码
/// </summary>
/// <returns>二维码相对路径</returns>
public string CreateQr(string name, string code, string sex, DateTime birthday, string phone)
{
var codeParams = CodeDescriptor.Init(HttpContext.Request);
var fileName = code + ".png";
var path = Server.MapPath("~/QRCode/");
var fullFileName = path + fileName; string content = string.Format("姓名:{0}\n编号:{1}\n性别:{2}\n年龄:{3}\n联系电话:{4}\n",
name,
code,
sex,
(birthday.Year > ? (DateTime.Now.Year - birthday.Year).ToString() : ""),
phone);
codeParams.Content = content;
// Encode the content
codeParams.TryEncode();
using (var ms = new MemoryStream())
{
codeParams.Render(ms); #region 保存图片
var img = Image.FromStream(ms);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
img.Save(fullFileName); #endregion
}
return fullFileName;
}

对这个二维码扫一扫, 就可以看到相关信息了.
二、参数解析
/// <summary>
/// Parse QueryString that define the QR code properties
/// </summary>
/// <param name="request">HttpRequest containing HTTP GET data</param>
/// <returns>A QR code descriptor object</returns>
public static CodeDescriptor Init(HttpRequestBase request)
{
var cp = new CodeDescriptor(); // Error correction level
if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))
cp.Ecl = ErrorCorrectionLevel.L; // Code content to encode
cp.Content = request.QueryString["t"]; // Size of the quiet zone
if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))
cp.QuietZones = QuietZoneModules.Two; // Module size
if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))
cp.ModuleSize = ; return cp;
}
1. 容错率
二维码的容错率有四个级别, 不过我得先介绍一下什么叫二维码容错率.
二维码容错率就是, 在二维码编码的时候, 进行冗余操作, 这种做法的目的, 就是希望二维码在有部分被遮挡的情况下, 还能扫描出正确结果. 就像abc编码成abcabc.
public enum ErrorCorrectionLevel
{
L = , //low 7%的字码可以被修正
M = , //medium 15%
Q = , //quartile 25%
H = , //high 30%
}
测试方法, 其实就是拿着扫一扫, 对二维码扫描, 扫描的时候, 慢慢的将二维码放入扫描匡, 会发现, 其实并不需要完全放入扫描匡, 就已经能出结果了.
容错率越高, 越容易快速扫描, 代价就是, 二维码编码的内容增多, 增加了二维码的复杂度.
默认情况下, 会选择L.
2. 空白
public enum QuietZoneModules
{
Zero = ,
Two = ,
Four = ,
}
这个属性, 表示二维码边上的空白区域的厚度, Zero表示没有空白边框, 最后的边框厚度, 是Two * 2 得到的.
3. 尺寸
这里的 ModuleSize 就是二维码图片的尺寸, 尺寸越大, 能容纳信息越多.
4. 内容编码
二维码内容默认编码为utf-8,
这里还有一些别的属性, 比如背景颜色, 绘制颜色之类的, 就不一一细说了
二维码的内容长度限制, 在文档中, 并没有找到, Api文档中说, 少到1个字符, 多到900个字符, 二维码都是能正常显示的. 当然, 二维码存储信息不易过多. The shorter the better.
如果内容过多, 可以通过二维码提供链接的方式, 让用户去请求接口, 而不是通过扫描二维码直接得到内容.
具体方法, 就是
codeParams.Content = "http://www.baidu.com";
这里的http://是必须要的, 否则会将内容当做普通字符去解析

最后, 贴上完整的封装:
/// <summary>
/// Class containing the description of the QR code and wrapping encoding and rendering.
/// </summary>
public class CodeDescriptor
{
public ErrorCorrectionLevel Ecl;
public string Content;
public QuietZoneModules QuietZones;
public int ModuleSize;
public BitMatrix Matrix;
public string ContentType; /// <summary>
/// Parse QueryString that define the QR code properties
/// </summary>
/// <param name="request">HttpRequest containing HTTP GET data</param>
/// <returns>A QR code descriptor object</returns>
public static CodeDescriptor Init(HttpRequestBase request)
{
var cp = new CodeDescriptor(); // Error correction level
if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))
cp.Ecl = ErrorCorrectionLevel.L; // Code content to encode
cp.Content = request.QueryString["t"]; // Size of the quiet zone
if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))
cp.QuietZones = QuietZoneModules.Two; // Module size
if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))
cp.ModuleSize = ; return cp;
} /// <summary>
/// Parse QueryString that define the QR code properties
/// </summary>
/// <param name="request">HttpRequest containing HTTP GET data</param>
/// <returns>A QR code descriptor object</returns>
public static CodeDescriptor Init(HttpRequest request)
{
var cp = new CodeDescriptor(); // Error correction level
if (!Enum.TryParse(request.QueryString["e"], out cp.Ecl))
cp.Ecl = ErrorCorrectionLevel.L; // Code content to encode
cp.Content = request.QueryString["t"]; // Size of the quiet zone
if (!Enum.TryParse(request.QueryString["q"], out cp.QuietZones))
cp.QuietZones = QuietZoneModules.Two; // Module size
if (!int.TryParse(request.QueryString["s"], out cp.ModuleSize))
cp.ModuleSize = ; return cp;
} /// <summary>
/// Encode the content with desired parameters and save the generated Matrix
/// </summary>
/// <returns>True if the encoding succeeded, false if the content is empty or too large to fit in a QR code</returns>
public bool TryEncode()
{
var encoder = new QrEncoder(Ecl);
QrCode qr;
if (!encoder.TryEncode(Content, out qr))
return false; Matrix = qr.Matrix;
return true;
} /// <summary>
/// Render the Matrix as a PNG image
/// </summary>
/// <param name="ms">MemoryStream to store the image bytes into</param>
public void Render(MemoryStream ms)
{
var render = new GraphicsRenderer(new FixedModuleSize(ModuleSize, QuietZones));
render.WriteToStream(Matrix, ImageFormat.Png, ms);
ContentType = "image/png";
}
}
二维码
参考:
QRCode的更多相关文章
- 免费开源的DotNet二维码操作组件ThoughtWorks.QRCode(.NET组件介绍之四)
在生活中有一种东西几乎已经快要成为我们的另一个电子”身份证“,那就是二维码.无论是在软件开发的过程中,还是在普通用户的日常中,几乎都离不开二维码.二维码 (dimensional barcode) , ...
- 使用jquery.qrcode生成二维码(转)
jQuery 的 qrcode 插件就可以在浏览器端生成二维码图片. 这个插件的使用非常简单: 1.首先在页面中加入jquery库文件和qrcode插件. <script type=" ...
- 动态生成二维码插件 jquery.qrcode.js
前段时间做项目,需要动态生成一个二维码,于是就在网上找了一下发现一个jquery插件jquery.qrcode.js,所以今天就简单说一下这个插件的使用: jquery.qrcode.js是依赖jqu ...
- qrcode 生成验证码带文字
/** * 生成二维码 * * @param int $id * @param string $file * @param boolean $is_download */public function ...
- Jquery.Qrcode在客户端动态生成二维码并添加自定义Logo
0 Jquery.Qrcode简介 Jquery.Qrcode.js是一个在浏览器端基于Jquery动态生成二维码的插件,支持Canvas和Table两种渲染方式,它的优点是在客户端动态生成,减轻了服 ...
- Pyqt+QRcode 生成 识别 二维码
1.生成二维码 python生成二维码是件很简单的事,使用第三方库Python QRCode就可生成二维码,我用Pyqt给QRcode打个壳 一.python-qrcode介绍 python-qrco ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...
- iOS - QRCode 二维码
1.QRCode 在 iOS7 以前,在 iOS 中实现二维码和条形码扫描,我们所知的有,两大开源组件 ZBar 与 ZXing. 这两大组件我们都有用过,这里总结下各自的缺点: 1.ZBar 在扫描 ...
- jquery.qrcode 生成二维码带logo
<div id="container">这里是二维码显示位置</div> <script language="JavaScript" ...
随机推荐
- Android零散
2016-03-13 Android零散 ListView中嵌套GridView 要实现分组列表这样的效果:点击ListView中的分组名称,即展开此分组显示其包含的项目.使用ExpandableLi ...
- Paket 介绍
在国外.NET社区有一个很火的话题是Packet(https://fsprojects.github.io/Paket/index.html ),它本质上是Nuget 之外的另一种方式管理.NET项目 ...
- Edge.js:让.NET和Node.js代码比翼齐飞
通过Edge.js项目,你可以在一个进程中同时运行Node.js和.NET代码.在本文中,我将会论述这个项目背后的动机,并描述Edge.js提供的基本机制.随后将探讨一些Edge.js应用场景,它在这 ...
- Linux 查找已安装软件的方法
1.rpm 注意rpm区分大小写 查询已安装的以mysql开头的包 rpm -qa mysql* 查询已安装的mysql 包 rpm -qa|grep mysql rpm的方法有时候也所有已安装的包 ...
- 一次Mysql 死锁事故
故障描述: 简单描述一下需求:我们写的一个计步器的客户端软件,用户通过手机客户端将用户的运动计步信息传到服务器. 服务器侧记录每个用户每次上传明细,同时每个用户有一个汇总值,参与全省排名. 1.加入明 ...
- Java动态编译
程序产生过程 下图展示了从源代码到可运行程序的过程,正常情况下先编译(明文源码到字节码),后执行(JVM加载字节码,获得类模板,实例化,方法使用).本文来探索下当程序已经开始执行,但在.class甚至 ...
- 当前不会命中断点。源代码与原始版本不同 (VS2012)
遇到“当前不会命中断点.源代码与原始版本不同”的问题. 在网上查的类似: 一般studio会提示将“工具”,“选项”,“调试”,“要求源文件与原始版本完成匹配”去掉勾.但是这个配置去掉治标不治本,错误 ...
- @OutputCache 详解-文章目录
OutputCache概念学习 OutputCache属性详解(一)一Duration.VaryByParam OutputCache属性详解(二)一 Location OutputCache属性详解 ...
- 初学者--bootstrap(四)栅格系统----在路上(8)
---------------------------------------栅格系统:是bootstrap提供的响应式布局方式------------------------------------ ...
- "org.eclipse.wst.validation" has been removed 导入maven 项目出错。
在谷歌中找到解决方案: 右键关闭项目,在打开,将项目刷新,选中项目右键----->Maven4myeclipse------->Update maven project 错误消失. 若还有 ...