Asp.net Core 跨平台生成带Logo二维码

1. nuget 引用  QRCoder-ImageSharp

2.代码

using QRCoder;
using SixLabors.ImageSharp.Formats.Jpeg;
using Color = SixLabors.ImageSharp.Color;
using Image = SixLabors.ImageSharp.Image;

namespace Common
{
    public class QrCoderUtil
    {
       

        /// <summary>
        /// 生成带logo二维码
        /// </summary>
        /// <param name="content"></param>
        /// <param name="logoPath"></param>
        /// <returns></returns>
        public static byte[] CreateQrCode(string content, string logoPath)
        {
            QRCodeGenerator qrGenerator = new QRCodeGenerator();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(content, QRCodeGenerator.ECCLevel.M);
            QRCode qrCode = new QRCode(qrCodeData);
            var qrCodeImage = qrCode.GetGraphic(20,Color.Black, Color.White,Image.Load(new MemoryStream(File.ReadAllBytes(logoPath))));
          //  return qrCodeImage;

            MemoryStream ms = new MemoryStream();
            if (qrCodeImage == null)
                return new byte[ms.Length];
            qrCodeImage.Save(ms,new JpegEncoder());
            var bPicture = ms.GetBuffer();
            return bPicture;
        }

    }

}

 3.使用

   public class QrCodeModel : PageModel
    {
        private readonly IWebHostEnvironment _hostingStartup;
        public QrCodeModel(IWebHostEnvironment wHostingStartup)
        {
            _hostingStartup = wHostingStartup;
        }
        public IActionResult OnGet(string data)
        {
            var logoPath = _hostingStartup.WebRootPath+ "/images/qrcodelogo.png";
            Response.ContentType = "image/jpeg";
            //data = SecurityUtil.DecryptString(data);
            var bitmap = QrCoderUtil.CreateQrCode(data, logoPath);
            return File(bitmap, "image/png");
        }
    }

 

posted @ 2022-09-30 14:54  前缘Q+7589848  阅读(100)  评论(0编辑  收藏  举报