QRCoder1.4.3生成二维码,不依赖System.Drawing,解决"未能找到类型或命名空间名QRCode","及ImageFormatPng仅在windows上受支持"

生成二维码1(简单)

包引用:

<PackageReference Include="QRCoder" Version="1.4.3" />

复制代码
using QRCoder;
复制代码
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="data">escape后的数据,防止中文等特殊字符引起问题</param>
        /// <param name="size">二维码宽高</param>
        /// <param name="level">0.L (7%), 1.M (15%), 2.Q (25%) 3.H (30%)</param>
        /// <param name="border">是否包含白边</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Generate(string text, int size = 400, int level = 1, bool border = false)
        {
            if (string.IsNullOrEmpty(text))
                return BadRequest("内容不能为空.");

            //设置最小和最大尺寸50-1920
            size = size < 50 ? 50 : size > 1920 ? 1920 : size;

            QRCodeGenerator qrGenerator = new();
            QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, (QRCodeGenerator.ECCLevel)level);

            int pixelsPerModule = size / 21; //size = pixelsPerModule * 21
            using PngByteQRCode qrCode = new(qrCodeData);
            byte[] qrCodeBytes = qrCode.GetGraphic(pixelsPerModule, drawQuietZones: border);

            // 将字节数组转换为Base64字符串
            string base64Image = Convert.ToBase64String(qrCodeBytes);
            // 返回Base64字符串作为图片的src属性值 // return $"data:image/png;base64,{base64Image}";
            return File(qrCodeBytes, "image/png", "QrCode.png");
        }

优点:生成的base64比较小

缺点:不能控制白边的宽度

size大小需要是21的倍数,否则生成的二维码宽度<=指定的size大小

生成二维码2(自定义白边的宽度

引用包:

<PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />

复制代码
using QRCoder;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using WebApplication7.Models;
using Color = SixLabors.ImageSharp.Color;
using Image = SixLabors.ImageSharp.Image;
using Point = SixLabors.ImageSharp.Point;
复制代码
[HttpGet]
复制代码
public IActionResult Generate2(string text, int size = 400, int level = 1, int borderWidth = 0)
 {
     if (string.IsNullOrEmpty(text))
         return BadRequest("内容不能为空.");

     //设置最小和最大尺寸50-1920
     size = size < 50 ? 50 : size > 1920 ? 1920 : size;

     QRCodeGenerator qrGenerator = new();
     QRCodeData qrCodeData = qrGenerator.CreateQrCode(text, (QRCodeGenerator.ECCLevel)level);

     using PngByteQRCode qrCode = new(qrCodeData);
     byte[] qrCodeBytes = qrCode.GetGraphic(50, drawQuietZones: false);

     using MemoryStream stream = new(qrCodeBytes);
     using Image<Rgba32> qrCodeImage = Image.Load<Rgba32>(stream);

     // 调整图像大小
     qrCodeImage.Mutate(ctx => ctx.Resize(size, size));

     // 添加空白来模拟白边
     borderWidth = borderWidth <= 0 ? 50 : borderWidth; // 调整白边的宽度
     Image<Rgba32> borderedImage = new(qrCodeImage.Width + 2 * borderWidth, qrCodeImage.Height + 2 * borderWidth);
     borderedImage.Mutate(ctx => ctx.BackgroundColor(Color.White).DrawImage(qrCodeImage, new Point(borderWidth, borderWidth), 1f));

     // 将调整大小后的图像保存为字节数组
     using MemoryStream resizedStream = new();
     borderedImage.Save(resizedStream, new PngEncoder());
     byte[] resizedBytes = resizedStream.ToArray();

     // 将字节数组转换为Base64字符串
     string base64Image = Convert.ToBase64String(resizedBytes);
     // 返回Base64字符串作为图片的src属性值 // return $"data:image/png;base64,{base64Image}";
     return File(resizedBytes, "image/png", "QrCode.png");
 }

优缺点与方法1相反