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" ...
随机推荐
- Sql Server 2012新特性 Online添加非空栏位.
我们都知道,Sql Server在一个数据量巨大的表中添加一个非空栏位是比较费心的,缺乏经验的DBA或是开发人员甚至可能鲁莽地直接添加导致阻塞相应业务,甚至可能因为资源欠缺造成实例的全局问题.当然这都 ...
- [备忘]没有为扩展名“.cshtml”注册的生成提供程序
webconfig中配置 <compilation debug="true" targetFramework="4.5.1"> < ...
- JAVA理论概念大神之概念汇总
我个人觉得,JAVA之所以能够经久不衰,有一个很重要的原因就是:JAVA的理论总是给人一种,虽然不知道是什么,但是感觉很厉害的样子.就单是这一点,他就已经超越许多其他语言了,至少吹牛的时候谈资总是很多 ...
- iOS瀑布流实现(Swift)
这段时间突然想到一个很久之前用到的知识-瀑布流,本来想用一个简单的方法,发现自己走入了歧途,最终只能狠下心来重写UICollectionViewFlowLayout.下面我将用两种方法实现瀑布流,以及 ...
- react-native 简述
1. RN 是什么 2. RN 与传统Hybrid框架相比的优势 3. RN的优势 4. RN的劣势 5. RN开发重点关注的问题 1 RN是什么 2 RN 与传统Hybrid框架相比的优势 传统的如 ...
- Android开发-之五大布局
在html中大家都知道布局是什么意思了,简单来说就是将页面划分模块,比如html中的div.table等.那么Android中也是这样的.Android五大布局让界面更加美化,开发起来也更加方便.当然 ...
- 如何在多线程leader-follower模式下正确的使用boost::asio。
#include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...
- CSS系列:CSS中盒子之间的关系
1. 标准文档流 标准文档流是值在不使用其他的雨排列和定位相关的特殊CSS规则时,各种元素的排列规则. 1.1 块级元素(block level) 块级元素不会排在同一行中,总是以一个块的形式表现出来 ...
- Android 文件读写
一.分类 文件读写作为Android四大数据存储方式之一,又分为内部存储和外部存储两种: (1)内部存储(Internal storage): 总是可用. 文件默认情况存储在/data/data/包名 ...
- c++ stringstream(老好用了)
前言: 以前没有接触过stringstream这个类的时候,常用的字符串和数字转换函数就是sscanf和sprintf函数.开始的时候就觉得这两个函数应经很叼了,但是毕竟是属于c的.c++中引入了流的 ...