[.NET]二维码生成
又好一段时间没有写写东西了,继续回归原来的模式,多做记录,最近要实现个unity的二维码方面的功能,首先就要解决生成二维码的问题,这个倒是有这方面的组件,然后我通过强大的反编译工具Reflector(想必.NET程序都知道的神器),来插件内部实现的原理。废话不多说,先看效果二维码在线生成工具,附带一句这里是QR码
在线测试:http://114.92.234.2:5005/
效果图
了解QR码
项目解析
1.用反编译神器来查看组件的内部实现原理
a)生成图片方法
- public virtual 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">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>无标题页</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- 请输入内容:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <br />
- 生成的文件夹名称:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
- <br/>
- 生成的二维码名称:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
- <br/>
- <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
- <br/>
- <asp:Button ID="Button1" runat="server" Text="生成二维码" onclick="Button1_Click" />
- </div>
- <div>
- <a href="http://blog.csdn.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
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void 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 = "该图片已经存在";
- }
- }
- }
==================== 迂者 丁小未 CSDN博客专栏=================
MyBlog:http://blog.csdn.net/dingxiaowei2013 MyQQ:1213250243
Unity QQ群:375151422,858550,6348968 cocos2dx QQ群:280818155
====================== 相互学习,共同进步 ===================
[.NET]二维码生成的更多相关文章
- [开源]C#二维码生成解析工具,可添加自定义Logo
二维码又称 QR Code,QR 全称 Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的 Bar Code 条形码能存更多的信息,也能表示更多的数据类型:比如:字 ...
- 聊聊 Web 项目二维码生成的最佳姿势
在设计和实现的过程之后,你永远不知道部署上去的程序会已什么样的姿势运行. 本篇借一次生成二维码逻辑的不同实现,阐述 Web 项目中二维码生成的正确姿势. 文中如有批量,欢迎各位看客老爷拍砖.试运行前5 ...
- .NET 二维码生成(ThoughtWorks.QRCode)
引用ThoughtWorks.QRCode.dll (源代码里有) 1.简单二维码生成及解码代码: //生成二维码方法一 private void CreateCode_Simple(string n ...
- iOS开发 二维码生成
基于libqrencode的二维码生成 + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { ...
- PHP二维码生成的方法(google APi,PHP类库,libqrencode等)
原文地址: http://blog.csdn.net/liuxinmingcode/article/details/7910975 ================================== ...
- Android 二维码 生成和识别(附Demo源码)
今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...
- Chrome浏览器二维码生成插件
猛击就可以使用啦->>>猛击使用 源码如下: 源码打包 源码: jquery-2.1.3.min.js jquery.qrcode.min.js https://gith ...
- Android 二维码 生成和识别(转)
原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...
- wex5 实战 二维码生成,扫描,蓝牙打印
给人设计了一个小模块,要求是,把一个单号生成二维码,实现扫描查询单号具体信息,并能通过蓝牙把二维码打印出来.功能实现并不复杂,今天一口气把它搞定.来看效果. 一 效果演示: 二.二维码生成 1 在 ...
- atitit.二维码生成总结java zxing
atitit.二维码生成总结java zxing #-----zxing类库.. but zxing3.0 的类库core-3.0.jar 需要jdk7 只好zing2.2.jar ..jdk6走o ...
随机推荐
- nginx文件管理
管理文件下载nginx 可以自己实现,无需写代码即可: 修改配置文件: location /doc { autoindex on; autoindex_exact_size on; autoindex ...
- BZOJ 2120: 数颜色
2120: 数颜色 Time Limit: 6 Sec Memory Limit: 259 MBSubmit: 3623 Solved: 1396[Submit][Status][Discuss] ...
- windows下 nvm下载node被墙了解决办法
不需要这么麻烦的,在1.1.1版本中,确实没有实现命令行设置.这点你分析的很对,但是从配置文件中读取镜像地址已经完成,所以直接在settings.txt中手工设置一下就好了,无需编译.以下是我的文件位 ...
- Protobuf for Python测试保存和读取文件
安装pip, setuptools, and wheel 如果已经从python.org,安装啦Python 2 >=2.7.9 or Python 3 >=3.4 ,那么就已经有啦pip ...
- Mybatis传参数
1使用@Param注解传参数 mapper接口:public void updateUser(@Param("user")User user)throws Exception; m ...
- SAX与DOM
http://www.cnblogs.com/zhulin/archive/2012/05/03/2480962.html 在解析xml时(如浏览器解析html标签),主要存在两种方式:SAX模式和D ...
- ANDROID_HOME on Mac OS X
Where the Android-SDK is installed depends on how you installed it. If you downloaded the SDK throug ...
- <head>中<meta name="viewport" content="width=device-width,initical-scale=1"的作用>
<meta name="viewport" content="width=device-width,initical-scale=1"的作用> co ...
- mysql中binlog_format模式与配置详解
mysql复制主要有三种方式:基于SQL语句的复制(statement-based replication, SBR),基于行的复制(row-based replication, RBR),混合模式复 ...
- ThinkPHP2.2框架执行流程图,ThinkPHP控制器的执行流程
ThinkPHP2.2框架执行原理.流程图在线手册 ThinkPHP控制器的执行流程 对用户的第一次URL访问 http://<serverIp>/My/index.php/Index/s ...