最近在做项目的时候遇到一个需求是将文本内容生成二维码图片的,对于这个需求那就直接上手(两种方法,我比较喜欢第二种方式,往上面也是有很多的方法。这里只作为个人纪录)
还有解析,并且还能添加彩色的点,应该是基于Drawing的
第一步: NeGet包引用ThoughtWorks.QRCode
using System;
using System.Drawing;
using System.Text;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 8;
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
System.Drawing.Image image = qrCodeEncoder.Encode("http://weixin.qq.com/r/qzj377TEKulVrfCM9225", Encoding.UTF8);
string filename = Guid.NewGuid() + ".jpg";
string filepath = filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();
//二维码解码
var codeDecoder = CodeDecoder(filepath);
Console.WriteLine($"二维码内容:{codeDecoder}");
Console.WriteLine("---");
Console.Read();
}
/// <summary>
/// 二维码解码
/// </summary>
/// <param name="filePath">图片路径</param>
/// <returns></returns>
public static string CodeDecoder(string filePath)
{
if (!System.IO.File.Exists(filePath))
return null;
Bitmap myBitmap = new Bitmap(Image.FromFile(filePath));
QRCodeDecoder decoder = new QRCodeDecoder();
string decodedString = decoder.decode(new QRCodeBitmapImage(myBitmap), Encoding.UTF8);
return decodedString;
}
}
}
我喜欢这个方法,先上效果图,这个可以自定义图片,而且还有边框,其他的方法暂时没有研究
第一步: NeGet包引用ThoughtWorks.QRCode
using System;
using System.Drawing;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
System.Drawing.Image image = code("http://weixin.qq.com/r/qzj377TEKulVrfCM9225", 5,10, "C:\\Users\\Pride\\Pictures\\myimage\\1.jpg", 15,1,true);
string filename = Guid.NewGuid() + ".jpg";
string filepath = filename;
System.IO.FileStream fs = new System.IO.FileStream(filepath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
fs.Close();
image.Dispose();
Console.WriteLine("OK");
Console.Read();
}
/// <summary>
/// 生成二维码
/// </summary>
/// <param name="msg">信息</param>
/// <param name="version">版本 1 ~ 40</param>
/// <param name="pixel">像素点大小</param>
/// <param name="icon_path">图标路径</param>
/// <param name="icon_size">图标尺寸</param>
/// <param name="icon_border">图标边框厚度</param>
/// <param name="white_edge">二维码白边</param>
/// <returns>位图</returns>
public static Bitmap code(string msg, int version, int pixel, string icon_path, int icon_size, int icon_border, bool white_edge)
{
QRCoder.QRCodeGenerator code_generator = new QRCoder.QRCodeGenerator();
QRCoder.QRCodeData code_data = code_generator.CreateQrCode(msg, QRCoder.QRCodeGenerator.ECCLevel.M/* 这里设置容错率的一个级别 */, true, true, QRCoder.QRCodeGenerator.EciMode.Utf8, version);
QRCoder.QRCode code = new QRCoder.QRCode(code_data);
Bitmap icon = new Bitmap(icon_path);
Bitmap bmp = code.GetGraphic(pixel, Color.Black, Color.White, icon, icon_size, icon_border, white_edge);
return bmp;
}
}
}
你喜欢哪种方式呢?