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

时间:2023-01-30 19:53:36

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,,,hint);
BarcodeWriter barcodeWriter = new BarcodeWriter();
Bitmap map = barcodeWriter.Write(bm); //获取二维码实际尺寸(去掉二维码两边空白后的实际尺寸)
int[] rectangle = bm.getEnclosingRectangle(); //计算插入图片的大小和位置
int middleW = Math.Min((int)(rectangle[] / 3.5), logo.Width);
int middleH = Math.Min((int)(rectangle[]/3.5),logo.Height);
int middleL = (map.Width - middleW) / ;
int middleT = (map.Height - middleH) / ; //将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,,);
}
//将二维码插入图片
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的二维码(二)

更多:

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