ZXing.Net 的Core平台生成二维码

编程入门 行业动态 更新时间:2024-10-10 15:18:54

ZXing.Net 的Core<a href=https://www.elefans.com/category/jswz/34/1769748.html style=平台生成二维码"/>

ZXing.Net 的Core平台生成二维码

一、引用

二、代码

帮助类
/// <summary>/// ZXing.NET 二维码帮助类/// </summary>public class ZXingHelper{/// <summary>/// 站点二维码的目录/// </summary>private static string QRCodeDirectory = "QRCode";/// <summary>/// 使用zxing动态库生成二维码/// </summary>/// <param name="conetnt">二维码内容</param>/// <param name="logoPath">logo图片路径,默认为空。为空时生成的二维码不带logo</param>/// <param name="height">二维码图片高度,默认240 单位 pixels</param>/// <param name="width">二维码图片宽度,默认240 单位 pixels</param>/// <param name="margin">二维码图片边距,默认为0</param>/// <returns></returns>public static System.DrawingCore.Bitmap GenerateQRCode(string conetnt, string logoPath = "", int height = 240, int width = 240, int margin = 0){try{BarcodeWriter barCodeWriter = new BarcodeWriter();barCodeWriter.Format = BarcodeFormat.QR_CODE;barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);barCodeWriter.Options.Height = height;barCodeWriter.Options.Width = width;barCodeWriter.Options.Margin = margin;BitMatrix bm = barCodeWriter.Encode(conetnt);System.DrawingCore.Bitmap qrCodeImage = barCodeWriter.Write(bm);if (!string.IsNullOrEmpty(logoPath)){// 添加LogoSystem.DrawingCore.Bitmap logo = new System.DrawingCore.Bitmap(logoPath);int logoSize = (int)(qrCodeImage.Width * 0.2); // Logo大小为二维码大小的1/5int logoX = (qrCodeImage.Width - logoSize) / 2;int logoY = (qrCodeImage.Height - logoSize) / 2;System.DrawingCore.Graphics qrGraphics = System.DrawingCore.Graphics.FromImage(qrCodeImage);qrGraphics.DrawImage(logo, new System.DrawingCore.Rectangle(logoX, logoY, logoSize, logoSize));}return qrCodeImage;}catch (Exception ex){return null;}}/// <summary>///生成二维码/// </summary>/// <param name="message"></param>/// <param name="width"></param>/// <param name="height"></param>/// <returns></returns>public static byte[] Create(string message, int width = 600, int height = 600){try{int heig = width;if (width > height){heig = height;width = height;}if (string.IsNullOrWhiteSpace(message)){return null;}var w = new QRCodeWriter();BitMatrix b = w.encode(message, BarcodeFormat.QR_CODE, width, heig);var zzb = new BarcodeWriter();zzb.Options = new EncodingOptions(){Margin = 0,};var  b2 = zzb.Write(b);byte[] bytes = BitmapToArray(b2);return bytes;}catch (Exception ex){return null;}}/// <summary>/// 将Bitmap  写为byte[]的方法/// </summary>/// <param name="bmp"></param>/// <returns></returns>public static byte[] BitmapToArray(System.DrawingCore.Bitmap bmp){byte[] byteArray = null;using (MemoryStream stream = new MemoryStream()){bmp.Save(stream, ImageFormat.Png);byteArray = stream.GetBuffer();}return byteArray;}}
调用
/// <summary>/// 获取二维码/// </summary>/// <param name="qrUrl"></param>/// <returns></returns>public byte[] GetQrCode(string qrUrl){System.DrawingCore.Bitmap qrImage = null;if (!System.IO.File.Exists(QRCodeLogoPath)){qrImage = ZXingHelper.GenerateQRCode(qrUrl);}else{qrImage = ZXingHelper.GenerateQRCode(qrUrl, QRCodeLogoPath);}MemoryStream ms = new MemoryStream();qrImage.Save(ms, System.DrawingCore.Imaging.ImageFormat.Bmp);byte[] bytes = ms.GetBuffer();ms.Close();//return ZXingHelper.Create(qrUrl);return bytes;}

更多推荐

ZXing.Net 的Core平台生成二维码

本文发布于:2023-12-07 08:57:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1670754.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平台   二维码   ZXing   Net   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!