Use SkiaSharp to support SvgQRCode in .NET6
Summary
Created a new class SvgQRCode_NET6 to support SvgQRCode is .NET6 including non-Windows platform like Linux.
This PR fixes/implements the following bugs/features:
- [ ] Feature 1 : Support SvgQRCode in .NET6 (not only Windows) by new class SvgQRCode_NET6
- [ ] Breaking changes : Adding SkiaSharp to replace System.Drawing, the core logic is the same as SvgQRCode
What existing problem does the pull request solve?**
Since NET6, System.Drawing is no longer support unless it is running in Windows, to make it work in other OS like Linux, this pull request introduce SkiaSharp to replace System.Drawing in .NET6. The new class SvgQRCode_NET6 is basically the same as SvgQRCode in .NET5 or before but skipping System.Drawing
Test plan
// Use below code snippet will be able to generate the QRCode image array
///// <summary>
///// .NET 6 version by QrCoder, Generate QrCode image in byte array
///// </summary>
///// <param name="qrCodeContent"></param>
///// <param name="qrCodeFormat"></param>
///// <param name="qrCodeSize"></param>
///// <param name="qrCodeStrokeColor"></param>
///// <param name="qrCodeBackgroundColor"></param>
///// <param name="logoBase64"></param>
///// <param name="logoSizePercent"></param>
///// <returns></returns>
public static byte[] GenerateQrCodeImageByteArray(string qrCodeContent, string qrCodeFormat, int qrCodeSize, string qrCodeStrokeColor, string qrCodeBackgroundColor, string logoBase64, int logoSizePercent, bool drawQuietZones)
{
// QRCode generater
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrCodeContent, QRCodeGenerator.ECCLevel.Q);
SvgQRCode_NET6 qrCode = new SvgQRCode_NET6(qrCodeData);
// Create QR Code with information
// lightColor = Background color, darkColor = stroke color
SvgLogo_NET6 logo = null;
if (!string.IsNullOrWhiteSpace(logoBase64))
{
byte[] data = Convert.FromBase64String(logoBase64);
var logoBitmap = SKImage.FromEncodedData(data);
logo = new SvgQRCode_NET6.SvgLogo_NET6(iconRasterized: logoBitmap, 15);
}
string qrCodeAsSvg = qrCode.GetGraphic(
viewBox: new SKSize(qrCodeSize, qrCodeSize),
darkColorHex: qrCodeStrokeColor,
lightColorHex: qrCodeBackgroundColor,
sizingMode: SvgQRCode_NET6.SizingMode.WidthHeightAttribute,
drawQuietZones: drawQuietZones,
logo: logo );
byte[] imageArray = null;
switch (qrCodeFormat.ToLower())
{
case "svg":
imageArray = Encoding.ASCII.GetBytes(qrCodeAsSvg);
break;
case "jpeg":
case "png":
using (var imageStream = new MemoryStream())
{
/// https://github.com/wieslawsoltes/Svg.Skia
using (var svg = new Svg.Skia.SKSvg())
{
svg.FromSvg(qrCodeAsSvg);
SKEncodedImageFormat imageFormat = SKEncodedImageFormat.Png;
if (qrCodeFormat.ToLower() == "jpeg")
imageFormat = SKEncodedImageFormat.Jpeg;
else if (qrCodeFormat.ToLower() == "png")
imageFormat = SKEncodedImageFormat.Png;
svg.Save(imageStream, SKColor.Parse(qrCodeBackgroundColor), imageFormat);
}
imageArray = imageStream.ToArray();
}
break;
}
return imageArray;
}
Closing issues
Fixes #
I wouldn't endorse adding a third party (non-MS) dependency to the base QRCoder NuGet package. I would rather see something like PR #407 where there are separate packages depending on the needs of the user.