目标可以生成带网址的信息的二维码,可以是带logo 或者不带logo,或者下方带一行说明文字
public static Bitmap GenerateQrCode(string text, int w, int h,string desc = "")
{
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
QrCodeEncodingOptions options = new QrCodeEncodingOptions()
{
DisableECI = true,//设置内容编码
CharacterSet = "UTF-8", //设置二维码的宽度和高度
Width = w,
Height = h,
Margin = 1//设置二维码的边距,单位不是固定像素
};
writer.Options = options;
Bitmap map = writer.Write(text);
if (!string.IsNullOrWhiteSpace(desc))
{
return AddText(desc, map, w, h);
}
return map;
}
public static Bitmap GenerateQrCodeWithLogo(string text, int w, int h, string logoUrl,string desc = "")
{
Bitmap logo = new Bitmap(logoUrl);
//构造二维码写码器
MultiFormatWriter writer = new MultiFormatWriter();
Dictionary<EncodeHintType, object> hint = new Dictionary<EncodeHintType, object>();
hint.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
hint.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hint.Add(EncodeHintType.MARGIN, 1);
//生成二维码
BitMatrix bm = writer.encode(text, BarcodeFormat.QR_CODE, w, h, hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);
//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle();
//计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3), logo.Width);
int middleH = Math.Min((int)(rectangle[3] / 3), logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;
Bitmap bmpimg = new Bitmap(map.Width, map.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmpimg))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(map, 0, 0, w, h);
//白底将二维码插入图片
g.FillRectangle(Brushes.White, middleL, middleT, middleW, middleH);
g.DrawImage(logo, middleL, middleT, middleW, middleH);
}
if (!string.IsNullOrWhiteSpace(desc))
{
return AddText(desc, map, w, h);
}
return bmpimg;
}
private static Bitmap AddText(string desc ,Bitmap qrBitMap,int width,int height)
{
var txtHeight = 30; // 默认一行文字
Font font = new Font("GB2312", 11, FontStyle.Regular);//设置字体,大小
SolidBrush sbrush = new SolidBrush(Color.Black); // 设置颜色
var newMap = new Bitmap(width, height + txtHeight);
Graphics g = Graphics.FromImage(newMap);
g.Clear(Color.White);
var format = StringFormat.GenericDefault;
format.LineAlignment = StringAlignment.Center;
format.Alignment = StringAlignment.Center;
g.DrawString(desc, font, sbrush, new RectangleF(0,height,width, txtHeight), format);
// 合并位图
g.DrawImage(qrBitMap, new Rectangle(0, 0, width, height));
g.Dispose();
return newMap;
}
///
/// 解码二维码
///
/// 待解码的二维码图片
/// 扫码结果
public static string DecodeQrCode(Bitmap barcodeBitmap)
{
BarcodeReader reader = new BarcodeReader();
reader.Options.CharacterSet = "UTF-8";
var result = reader.Decode(barcodeBitmap);
return (result == null) ? null : result.Text;
}