又好一段时间没有写写东西了,继续回归原来的模式,多做记录,最近要实现个unity的二维码方面的功能,首先就要解决生成二维码的问题,这个倒是有这方面的组件,然后我通过强大的反编译工具Reflector(想必.NET程序都知道的神器),来插件内部实现的原理。废话不多说,先看效果二维码在线生成工具,附带一句这里是QR码,当然是比较简单的,纯属新手入门级看的
在线测试:http://114.92.234.2:5005/
效果图
了解QR码
项目解析
1.用反编译神器来查看组件的内部实现原理
a)生成图片方法
publicvirtual Bitmap Encode(string content, Encoding encoding)
{
//计算得到bool值数组
bool[][] flagArray = this.calQrcode(encoding.GetBytes(content));
//定义画刷
SolidBrush brush = new SolidBrush(this.qrCodeBackgroundColor);
Bitmap image = new Bitmap((flagArray.Length * this.qrCodeScale) + 1, (flagArray.Length * this.qrCodeScale) + 1);
//绘图
Graphics graphics = Graphics.FromImage(image);
//绘制二维码背景
graphics.FillRectangle(brush, new Rectangle(0, 0, image.Width, image.Height));
//设置背景颜色
brush.Color = this.qrCodeForegroundColor;
//根据bool数组绘制前端二维码
for (int i = 0; i < flagArray.Length; i++)
{
for (int j = 0; j < flagArray.Length; j++)
{
if (flagArray[j][i])
{
graphics.FillRectangle(brush, j * this.qrCodeScale, i * this.qrCodeScale, this.qrCodeScale, this.qrCodeScale);
}
}
}
return image;
}
b)生成bool数组方法以及原理
这里主要是用到
创建.NET应用程序
Default.aspx
<%@ Page Language="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>无标题页</title>
</head>
<body>
<formid="form1"runat="server">
<div>
请输入内容:<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>
<br/>
生成的文件夹名称:<asp:TextBoxID="TextBox2"runat="server"></asp:TextBox>
<br/>
生成的二维码名称:<asp:TextBoxID="TextBox3"runat="server"></asp:TextBox>
<br/>
<asp:LabelID="Label1"runat="server"Text=""></asp:Label>
<br/>
<asp:ButtonID="Button1"runat="server"Text="生成二维码"onclick="Button1_Click"/>
</div>
<div>
<ahref="http://blog.****.net/dingxiaowei2013">项目解析</a>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using ThoughtWorks.QRCode.Codec;
using System.IO;
using System.Text;
using System.Drawing;
public partial class _Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
//设置背景颜色
//qrCodeEncoder.QRCodeBackgroundColor = Color.FromArgb(255, 255, 0);
//设置前景色
//qrCodeEncoder.QRCodeForegroundColor = Color.GreenYellow;
//编码格式
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//设置每个二维码像素点的大小
qrCodeEncoder.QRCodeScale = 4;
//QR码版本
//QR码所允许规格系列为21×21模块(版本1)~177×177模块(版本40)
qrCodeEncoder.QRCodeVersion = 8;
//纠错等级
//level L : 最大 7% 的错误能够被纠正;
//level M : 最大 15% 的错误能够被纠正;
//level Q : 最大 25% 的错误能够被纠正;
//level H : 最大 30% 的错误能够被纠正;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
//自定义的二维码数据
String data = TextBox1.Text.ToString();
//Response.Write(data);
//画图
System.Drawing.Bitmap image = qrCodeEncoder.Encode(data);
System.IO.MemoryStream MStream = new System.IO.MemoryStream();
image.Save(MStream, System.Drawing.Imaging.ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/Png";
//写图片
Response.BinaryWrite(MStream.ToArray());
//文件路径
//FileInfo f = new FileInfo(@"d:\zl.png");
//Directory.CreateDirectory("d:\\"+TextBox2.Text.Trim());
//当前项目路径
Directory.CreateDirectory(Server.MapPath("~/")+"images"+"//"+TextBox2.Text.Trim());
if (!File.Exists(Server.MapPath("~/") + "images" + "//" + TextBox2.Text.Trim() + "//" + TextBox3.Text.Trim() + ".png"))
{
//FileStream fs = new FileStream("d:\\" + TextBox2.Text.Trim() + "\\" + TextBox3.Text.Trim() + ".png", FileMode.CreateNew, FileAccess.ReadWrite);
FileStream fs = new FileStream(Server.MapPath("~/") + "images" + "//" + TextBox2.Text.Trim() + "//" + TextBox3.Text.Trim() + ".png", FileMode.CreateNew, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs, UTF8Encoding.UTF8);
byte[] by = MStream.ToArray();
for (int i = 0; i < MStream.ToArray().Length; i++)
bw.Write(by[i]);
fs.Close();
}
else
{
Label1.Text = "该图片已经存在";
}
}
}
本文出自 “丁小未的专栏” 博客,请务必保留此出处http://dingxiaowei.blog.51cto.com/4561335/1390756