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" ...
随机推荐
- 采访Philipp Crocoll:安卓平台上整合Java和C#
在这个采访中,我们跟开源开发者Philipp Crocoll讨论了关于Keepass2Android的相关话题.Keepass2Android不仅具有强大的密码存储的功能,还是在一个单独的安卓应用同时 ...
- 负载均衡的场景下ASP.NET Core如何获取客户端IP地址
在ASP.NET中,使用负载均衡时,可以通过ServerVariables获取客户端的IP地址. var ip = request.ServerVariables["HTTP_X_FORWA ...
- Prim 最小生成树算法
Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). P ...
- mysql定义和调用存储过程
/*定义delimiter为 // */ delimiter // CREATE procedure sp_add3(a int, b int,out c int) begin set c=a+ b; ...
- C语言 · 矩阵乘法
问题描述 输入两个矩阵,分别是m*s,s*n大小.输出两个矩阵相乘的结果. 输入格式 第一行,空格隔开的三个正整数m,s,n(均不超过200). 接下来m行,每行s个空格隔开的整数,表示矩阵A(i,j ...
- iOS开发-闪退问题-解决之前上架的 App 在 iOS 9 会闪退问题
最新更新:(2015.10.02) 开发环境: Delphi 10 Seattle OS X El Capitan v10.11 需使用下列 HotfixID: 30398, PAServer Hot ...
- JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式
相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...
- 数据访问模式:Identity Map(标识映射)模式
1.Identity Map模式简介 Identity Map(标识映射)模式是通过将所有已加载对象放在一个映射中确保所有对象只被加载一次,并且在引用这些对象时使用该映射来查找对象.在处理数据并发访问 ...
- Snapshot Volume 操作 - 每天5分钟玩转 OpenStack(58)
Snapshot 可以为 volume 创建快照,快照中保存了 volume 当前的状态,以后可以通过 snapshot 回溯.snapshot 操作实现比较简单,流程图如下: 向 cinder-ap ...
- gsoap设置超时
1.修改gsoap自动生成的代码才能进行超时设置(我这边访问web service的代码都是gsoap工具自动生成.根据wsdl接口) 2.找到生成的soapwwwsdlBindingProxy.cp ...