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" ...
随机推荐
- Cocos2d-x游戏引擎实战开发炸弹超人项目教程 全套下载 1至6课
下载地址: http://pan.baidu.com/s/1b19HN
- Meteor + node-imap(nodejs) + mailparser(nodejs) 实现完整收发邮件
版本信息: Meteor:windows MIS安装 0.6.4 node-imap:npm指定的0.8.0版,不是默认的0.7.x版. mailparser:npm安装0.3.6 以下是记录踩到的 ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- JavaScript中的百变大咖~this
原文链接:http://www.jeffjade.com/2015/08/03/2015-08-03-javascript-this/ JavaScript作为一种脚本语言身份的存在,因此被很多人认为 ...
- php ajax 交互
html 页面 <body> <button id="oBtn">点击我</button> <script type="text ...
- 研究一下javascript的模块规范(CommonJs/AMD/CMD)
最近写react需要使用nodejs作为开发环境,需要通过npm安装一些第三方的依赖库,因此慢慢感觉到nodejs基础薄弱对我带来了一些不安全感,尤其是javascript模块这一块听到了很多概念,比 ...
- PS批处理的使用
一. 前言 做开发的时候,最多的时候就是图片的使用了.有时候图片的处理都按照同样的步骤,比如说统一将图片的大小调整为固定大小,或者统一在所有的图片的的某个位置上加入文字或者小图片等等,这时候PS的批处 ...
- 快速搭建php环境
WAMP:在windows系统下搭建PHP开发环境 APPSERVER: 两种可用于开发环境的,一般用WAMP LAMP构架 Linux系统 Apache服务器管理软件 Mysql数据库 php语言 ...
- ASP.NET Core的配置(4):多样性的配置来源[上篇]
较之传统通过App.config和Web.config这两个XML文件承载的配置系统,ASP.NET Core采用的这个全新的配置模型的最大一个优势就是针对多种不同配置源的支持.我们可以将内存变量.命 ...
- 跟着9张思维导图学习Javascript
学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将 po 出我收集的 9 张 javascript 相关的思维导图(非原创). 思维导图小tips: 思维导图又叫心智图,是表达发射性思维的 ...