这篇文章主要介绍了C#如何用ThoughtWorks生成二维码,文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
在现在的项目中,较多的使用到二维码,前面介绍过一篇使用Gma生成二维码的操作,现在介绍一个第三方组件,主要介绍生成二维码,二维码的解析,以及对二维码的相关信息的选择,现在介绍ThoughtWorks用于生成二维码,本随笔采用.net4.5和C#6.0语法,也可以进行查看ThoughtWorks的底层源码。
1.生成二维码:
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="content">带生成二维码的字符串</param>
/// <param name="path">路径</param>
/// <returns></returns>
public static string CreatehoughtWorksQrCode(string content, string path)
{
if (string.IsNullOrEmpty(content))
{
throw new ArgumentNullException(content);
}
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException(path);
}
var qrCodeEncoder = new QRCodeEncoder
{
QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE,
QRCodeScale = 4,
QRCodeVersion = 8,
QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M
};
Image image = qrCodeEncoder.Encode(content);
var filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg";
var filepath = string.Format("{0}{1}", path, filename);
FileStream fs = null;
try
{
fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (IOException ex)
{
throw new IOException(ex.Message);
}
finally
{
if (fs != null) fs.Close();
image.Dispose();
}
return CodeDecoder(filepath);
}
2.解析二维码:
/// <summary>
/// 二维码解码
/// </summary>
/// <param name="filePath">图片路径</param>
/// <returns></returns>
public static string CodeDecoder(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException(filePath);
}
try
{
if (!System.IO.File.Exists(filePath))
return null;
var myBitmap = new Bitmap(Image.FromFile(filePath));
var decoder = new QRCodeDecoder();
var decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap));
return decodedString;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
3.选择生成的二维码参数:
/// <summary>
/// 选择生成二维码的相关类型
/// <param name="strData">要生成的文字或者数字,支持中文。如: "4408810820 深圳-广州" 或者:4444444444</param>
/// <param name="qrEncoding">三种尺寸:BYTE ,ALPHA_NUMERIC,NUMERIC</param>
/// <param name="level">大小:L M Q H</param>
/// <param name="version">版本:如 8</param>
/// <param name="scale">比例:如 4</param>
/// <returns></returns>
/// </summary>
public void CreateCode_Choose(string strData, string qrEncoding, string level, int version, int scale)
{
var qrCodeEncoder = new QRCodeEncoder();
var encoding = qrEncoding;
switch (encoding)
{
case "Byte":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
case "AlphaNumeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
break;
case "Numeric":
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
break;
default:
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
break;
}
qrCodeEncoder.QRCodeScale = scale;
qrCodeEncoder.QRCodeVersion = version;
switch (level)
{
case "L":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
break;
case "M":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
break;
case "Q":
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
break;
default:
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;
break;
}
Image image = null;
FileStream fs = null;
try
{
//文字生成图片
image = qrCodeEncoder.Encode(strData);
var filename = DateTime.Now.ToString("yyyymmddhhmmssfff") + ".jpg";
var filepath = HttpContext.Current.Server.MapPath(@"~\Upload") + "\\" + filename;
fs = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (IOException ex)
{
throw new IOException(ex.Message);
}
finally
{
if (fs != null) fs.Close();
if (image != null) image.Dispose();
}
}
以上就是C#如何用ThoughtWorks生成二维码的详细内容,更多关于c#生成二维码的资料请关注得得之家其它相关文章!
沃梦达教程
本文标题为:C#如何用ThoughtWorks生成二维码
基础教程推荐
猜你喜欢
- C# List实现行转列的通用方案 2022-11-02
- C#控制台实现飞行棋小游戏 2023-04-22
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- ZooKeeper的安装及部署教程 2023-01-22
- winform把Office转成PDF文件 2023-06-14
- unity实现动态排行榜 2023-04-27
- C#类和结构详解 2023-05-30
- C# windows语音识别与朗读实例 2023-04-27
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09