You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.7 KiB
54 lines
1.7 KiB
using AIK.Common.Interface;
|
|
using QRCoder;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace AIK.Views.UIServices
|
|
{
|
|
public class QRCodeService : IQRCodeService
|
|
{
|
|
public byte[] GenerateQRCodeWithLogo(string text, string logoPath, int pixelsPerModule = 10)
|
|
{
|
|
using var qrGenerator = new QRCodeGenerator();
|
|
var qrCodeData = qrGenerator.CreateQrCode(text, QRCodeGenerator.ECCLevel.H);
|
|
using var qrCode = new QRCode(qrCodeData);
|
|
|
|
using var logo = LoadLogo(logoPath);
|
|
using var bitmap = qrCode.GetGraphic(
|
|
pixelsPerModule: pixelsPerModule,
|
|
darkColor: Color.Black,
|
|
lightColor: Color.White,
|
|
drawQuietZones: true,
|
|
icon: logo,
|
|
iconSizePercent: 15
|
|
);
|
|
|
|
// 👇 关键:返回 PNG 字节数组
|
|
using var memory = new MemoryStream();
|
|
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
|
|
return memory.ToArray();
|
|
}
|
|
|
|
private static Bitmap LoadLogo(string logoPath)
|
|
{
|
|
if (logoPath.StartsWith("AIK.Assets.Images."))
|
|
{
|
|
var assetsAssembly = Assembly.Load("AIK.Assets");
|
|
using var stream = assetsAssembly.GetManifestResourceStream(logoPath);
|
|
return new Bitmap(stream);
|
|
}
|
|
else
|
|
{
|
|
return new Bitmap(logoPath);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|