你的位置:首页 > ASP.net教程

[ASP.net教程]随手记一次利用开源zxing生成带嵌入logo的二维码图片


之前就在项目里面用过zxing生成二维码,最近另一个项目同样需要用到二维码,故重新在学了学利用zxing生成二维码


接下来先做准备工作了,因为我是用vs2013上开发的,故选择了.net4.5版本的zxing.dll.另外准备了一张准备嵌入的图片,代码如下:

 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Drawing; 5 using System.Linq; 6 using System.Web; 7 using ZXing; 8 using ZXing.QrCode.Internal; 9 using ZXing.Common;10 using ZXing.Datamatrix;11 using System.IO;12 using ZXing.Rendering;13 using ZXing.QrCode;14 15 16 namespace UsezxingCreateQRcode.Models17 {18   public class CreateQrCode19   {20     public static byte[] GetQrCodeByZXing(string msg, string logo)21     {22 23       //构造二维码写码器24       QRCodeWriter qrwriter = new QRCodeWriter();25       IDictionary<EncodeHintType, object> hints = new Dictionary<EncodeHintType, object>();26       hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");27       hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 容错级别28       hints.Add(EncodeHintType.MARGIN, 1);//二维码留白边距29       BarcodeWriter bw = new BarcodeWriter();30       BitMatrix bm = qrwriter.encode(msg, BarcodeFormat.QR_CODE, 300, 300, hints);31       Bitmap img = bw.Write(bm);32 //获取logo33       Image logoImage = Image.FromFile(logo);34       //计算插入图片的大小和位置,并计算logo占整个二维码图片的比例35 //根据实际需求可自定义logo所占比例36       int logoImageW = Math.Min((int)(img.Size.Width / 4), logoImage.Width);37       int logoImageH = Math.Min((int)(img.Size.Height / 4), logoImage.Height);38 39       int logoImageX = (img.Width - logoImageW) / 2;40       int logoImageY = (img.Height - logoImageH) / 2;41 42       //将img转换成bmp格式,并创建 Graphics对象43       Bitmap bmpimg = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);44       using (Graphics g = Graphics.FromImage(bmpimg))45       {46         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;47         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;48         g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;49         g.DrawImage(img, 0, 0);50       }51 52       //在二维码中插入图片53       System.Drawing.Graphics MyGraphic = System.Drawing.Graphics.FromImage(bmpimg);54       //白底55       MyGraphic.FillRectangle(Brushes.White, logoImageX, logoImageY, logoImageW, logoImageH);56       MyGraphic.DrawImage(logoImage, logoImageX, logoImageY, logoImageW, logoImageH); //嵌入logo57       MemoryStream ms = new MemoryStream();58 bmpimg.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);59       byte[] buffur = ms.ToArray();60       ms.Dispose();61       return buffur;62     }63   }64 }

View Code

使用以上方法,举例如下:

1  public ActionResult CreateQrCode()2     {3       string logo = AppDomain.CurrentDomain.BaseDirectory + "Images\\cloud.jpg";4       ViewBag.img = "data:image/Jpeg;base64," + Convert.ToBase64String(UsezxingCreateQRcode.Models.CreateQrCode.GetQrCodeByZXing("NO070133333", logo));5       return View();6     }

View Code

........时间不早了,上一个效果图吧: