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

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

///


/// 生成带Logo的二维码
///

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