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

C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

时间:2018-12-14 21:17:53      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:.net   wing   tps   生成   位置   get   erro   hybrid   插入   

1.使用ZXint.Net生成带logo的二维码

/// <summary>
/// 生成带Logo的二维码
/// </summary>
/// <param name="text"></param>
static void Generate3(string text)
{
//Logo 图片
Bitmap logo = new Bitmap(@"H:\桌面\截图\102.jpg");
//构造二维码写码器
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);

//生成二维码
BitMatrix bm = writer.encode(text,BarcodeFormat.QR_CODE,300,300,hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm);


//获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle();

//计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[2] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[3]/3.5),logo.Height);
int middleL = (map.Width - middleW) / 2;
int middleT = (map.Height - middleH) / 2;

//将img转换成bmp格式,否则后面无法创建Graphics对象
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);
}
//将二维码插入图片
Graphics myGraphic = Graphics.FromImage(bmpimg);
//白底
myGraphic.FillRectangle(Brushes.White,middleL,middleT,middleW,middleH);
myGraphic.DrawImage(logo,middleL,middleT,middleW,middleH);

//保存成图片
bmpimg.Save(@"H:\桌面\截图\generate3.png",ImageFormat.Png);
}

 

调用:

//生成带图片的二维码
Generate3("https://www.baidu.com/");
Generate3("ionic是一个强大的混合式/hybrid HTML5移动开发框架,特点是使用标准的HTML、CSS和JavaScript,开发跨平台的应用 ,只需要几步就可以快速创建您的Ionic应用,创建应用从这里开始");

C# ZXing.Net生成二维码、识别二维码、生成带Logo的二维码(二)

标签:.net   wing   tps   生成   位置   get   erro   hybrid   插入   

原文地址:https://www.cnblogs.com/zxtceq/p/10121389.html

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