码迷,mamicode.com
首页 > Windows程序 > 详细

C# 生成二维码

时间:2016-06-30 16:01:29      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

项目引用引用ThoughtWorks.QRCode.dll 类

新建类QrCodeHelper

引用

using ThoughtWorks.QRCode.Codec;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

创建生成二维码的方法CreateEQcoder

 1 /// <summary>
 2         /// 生成二维码
 3         /// </summary>
 4         /// <param name="qrCodeScale">尺寸4-15</param>
 5         /// <param name="qrCodeVersion">复杂级别3-12</param>
 6         /// <param name="qrCodeErrorCorrect">容错量"H","L","M","Q"</param>
 7         /// <param name="url">二维码信息</param>
 8         /// <param name="uId">用户ID(唯一标识)</param>
 9         /// <returns></returns>
10         public static string CreateEQcoder(int qrCodeScale, int qrCodeVersion, string qrCodeErrorCorrect, string url, string uId)
11         {
12             QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
13             qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
14             qrCodeEncoder.QRCodeScale = qrCodeScale;
15             qrCodeEncoder.QRCodeVersion = qrCodeVersion;
16             switch (qrCodeErrorCorrect)
17             {
18                 case "H":
19                     qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
20                     break;
21                 case "L":
22                     qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
23                     break;
24                 case "M":
25                     qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
26                     break;
27                 case "Q":
28                     qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
29                     break;
30             }
31             qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
32             Bitmap bmPhoto = qrCodeEncoder.Encode(url, System.Text.Encoding.GetEncoding("UTF-8"));
33             string fileName = DateTime.Now.ToString("yyyy") + ".jpg";
34             string savePath = "/Tempuploads/" + uId + "/QRCode/";
35 
36             string dirPath = System.Web.HttpContext.Current.Server.MapPath(savePath);
37 
38             try
39             {
40                 if (!Directory.Exists(dirPath))
41                 {
42                     Directory.CreateDirectory(dirPath);
43                 }
44                 bmPhoto.Save(System.Web.HttpContext.Current.Server.MapPath("/") + savePath + fileName, ImageFormat.Jpeg);
45                 bmPhoto.Dispose();
46                 return savePath + fileName;
47             }
48             catch (Exception)
49             {
50                 return "";
51             }
52             finally
53             {
54                 bmPhoto.Dispose();
55             }
56         }

解析二维码

QRCodeDecoder decoder = new QRCodeDecoder();   
String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(bmPhoto)));   
string Text = decodedString; 

 

C# 生成二维码

标签:

原文地址:http://www.cnblogs.com/mobobo/p/5630154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!