40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
using QRCoder;
|
|
|
|
namespace YuanXuan.IM.Common.Helpers
|
|
{
|
|
/// <summary>
|
|
/// 二维码生成器
|
|
/// </summary>
|
|
public class QRCoderHelper
|
|
{
|
|
/// <summary>
|
|
/// 生成二维码的 URL
|
|
/// </summary>
|
|
/// <param name="filePath">网络路径</param>
|
|
/// <returns>二维码图片的base64字符串</returns>
|
|
public static string GenerateQRCodeUrl(string filePath)
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(filePath))
|
|
{
|
|
throw new ArgumentException("文件路径不能为空", nameof(filePath));
|
|
}
|
|
|
|
using (var qrGenerator = new QRCodeGenerator())
|
|
{
|
|
// 生成二维码数据
|
|
var qrCodeData = qrGenerator.CreateQrCode(filePath, QRCodeGenerator.ECCLevel.Q);
|
|
|
|
// 使用 PngByteQRCode 生成二维码字节数组
|
|
var pngByteQRCode = new PngByteQRCode(qrCodeData);
|
|
var qrCodeBytes = pngByteQRCode.GetGraphic(20);
|
|
|
|
// 将字节数组转换为 Base64 字符串
|
|
var base64String = Convert.ToBase64String(qrCodeBytes);
|
|
return $"data:image/png;base64,{base64String}";
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|