Skip to content

Commit 70f644a

Browse files
committed
Add ImageSharp renderers for .net 6.0, 5.0 and standart 2.0
1 parent dd35a3b commit 70f644a

File tree

10 files changed

+504
-16
lines changed

10 files changed

+504
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using SixLabors.ImageSharp.Formats;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace QRCoder.ImageSharp
9+
{
10+
public class ImageSharpBase64QRCode : AbstractQRCode, IDisposable
11+
{
12+
private ImageSharpQRCode qr;
13+
14+
/// <summary>
15+
/// Constructor without params to be used in COM Objects connections
16+
/// </summary>
17+
public ImageSharpBase64QRCode()
18+
{
19+
qr = new ImageSharpQRCode();
20+
}
21+
22+
public ImageSharpBase64QRCode(QRCodeData data)
23+
: base(data)
24+
{
25+
qr = new ImageSharpQRCode(data);
26+
}
27+
28+
public override void SetQRCodeData(QRCodeData data)
29+
{
30+
qr.SetQRCodeData(data);
31+
}
32+
33+
public string GetGraphic(int pixelsPerModule)
34+
{
35+
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
36+
}
37+
38+
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
39+
{
40+
return GetGraphic(pixelsPerModule, Color.Parse(darkColorHtmlHex), Color.Parse(lightColorHtmlHex), drawQuietZones, imgType);
41+
}
42+
43+
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
44+
{
45+
var base64 = string.Empty;
46+
using (Image img = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
47+
{
48+
base64 = BitmapToBase64(img, imgType);
49+
}
50+
51+
return base64;
52+
}
53+
54+
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Image icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
55+
{
56+
var base64 = string.Empty;
57+
using (Image bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
58+
{
59+
base64 = BitmapToBase64(bmp, imgType);
60+
}
61+
62+
return base64;
63+
}
64+
65+
private string BitmapToBase64(Image img, ImageType imgType)
66+
{
67+
var base64 = string.Empty;
68+
IImageEncoder iFormat;
69+
switch (imgType)
70+
{
71+
default:
72+
case ImageType.Png:
73+
iFormat = new SixLabors.ImageSharp.Formats.Png.PngEncoder();
74+
break;
75+
case ImageType.Jpeg:
76+
iFormat = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder();
77+
break;
78+
case ImageType.Gif:
79+
iFormat = new SixLabors.ImageSharp.Formats.Gif.GifEncoder();
80+
break;
81+
}
82+
83+
using (var memoryStream = new MemoryStream())
84+
{
85+
img.Save(memoryStream, iFormat);
86+
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
87+
}
88+
89+
return base64;
90+
}
91+
}
92+
}
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using SixLabors.ImageSharp;
3+
using SixLabors.ImageSharp.PixelFormats;
4+
using SixLabors.ImageSharp.Processing;
5+
using static QRCoder.QRCodeGenerator;
6+
7+
namespace QRCoder.ImageSharp
8+
{
9+
public class ImageSharpQRCode : AbstractQRCode, IDisposable
10+
{
11+
/// <summary>
12+
/// Constructor without params to be used in COM Objects connections
13+
/// </summary>
14+
public ImageSharpQRCode() { }
15+
16+
public ImageSharpQRCode(QRCodeData data)
17+
: base(data) { }
18+
19+
public Image GetGraphic(int pixelsPerModule)
20+
{
21+
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
22+
}
23+
24+
public Image GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true)
25+
{
26+
return GetGraphic(pixelsPerModule, Color.Parse(darkColorHtmlHex), Color.Parse(lightColorHtmlHex), drawQuietZones);
27+
}
28+
29+
public Image GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true)
30+
{
31+
var moduleOffset = drawQuietZones ? 0 : 4;
32+
var size = (QrCodeData.ModuleMatrix.Count - (moduleOffset * 2)) * pixelsPerModule;
33+
34+
var image = new Image<Rgba32>(size, size);
35+
DrawQRCode(image, pixelsPerModule, moduleOffset, darkColor, lightColor);
36+
37+
return image;
38+
}
39+
40+
public Image GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Image icon = null, int iconSizePercent = 15, int iconBorderWidth = 0, bool drawQuietZones = true, Color? iconBackgroundColor = null)
41+
{
42+
var img = GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones) as Image<Rgba32>;
43+
if (icon != null && iconSizePercent > 0 && iconSizePercent <= 100)
44+
{
45+
var iconDestWidth = iconSizePercent * img.Width / 100f;
46+
var iconDestHeight = iconDestWidth * icon.Height / icon.Width;
47+
var iconX = (img.Width - iconDestWidth) / 2;
48+
var iconY = (img.Height - iconDestHeight) / 2;
49+
var centerDest = new RectangleF(iconX - iconBorderWidth, iconY - iconBorderWidth, iconDestWidth + (iconBorderWidth * 2), iconDestHeight + (iconBorderWidth * 2));
50+
var iconDestRect = new RectangleF(iconX, iconY, iconDestWidth, iconDestHeight);
51+
52+
if (iconBorderWidth > 0)
53+
{
54+
if (!iconBackgroundColor.HasValue)
55+
{
56+
iconBackgroundColor = lightColor;
57+
}
58+
59+
if (iconBackgroundColor != Color.Transparent)
60+
{
61+
img.ProcessPixelRows(accessor =>
62+
{
63+
for (var y = (int)centerDest.Top; y <= (int)centerDest.Bottom; y++)
64+
{
65+
var pixelRow = accessor.GetRowSpan(y);
66+
67+
for (var x = (int)centerDest.Left; x <= (int)centerDest.Right; x++)
68+
{
69+
pixelRow[x] = iconBackgroundColor ?? lightColor;
70+
}
71+
}
72+
});
73+
}
74+
}
75+
76+
var sizedIcon = icon.Clone(x => x.Resize((int)iconDestWidth, (int)iconDestHeight));
77+
img.Mutate(x => x.DrawImage(sizedIcon, new Point((int)iconDestRect.X, (int)iconDestRect.Y), 1));
78+
}
79+
80+
return img;
81+
}
82+
83+
private void DrawQRCode(Image<Rgba32> image, int pixelsPerModule, int moduleOffset, Color darkColor, Color lightColor)
84+
{
85+
var row = new Rgba32[image.Width];
86+
87+
image.ProcessPixelRows(accessor =>
88+
{
89+
for (var modY = moduleOffset; modY < QrCodeData.ModuleMatrix.Count - moduleOffset; modY++)
90+
{
91+
// Generate row for this y-Module
92+
for (var modX = moduleOffset; modX < QrCodeData.ModuleMatrix.Count - moduleOffset; modX++)
93+
{
94+
for (var idx = 0; idx < pixelsPerModule; idx++)
95+
{
96+
row[((modX - moduleOffset) * pixelsPerModule) + idx] = this.QrCodeData.ModuleMatrix[modY][modX] ? darkColor : lightColor;
97+
}
98+
}
99+
100+
// Copy the prepared row to the image
101+
for (var idx = 0; idx < pixelsPerModule; idx++)
102+
{
103+
var pixelRow = accessor.GetRowSpan(((modY - moduleOffset) * pixelsPerModule) + idx);
104+
row.CopyTo(pixelRow);
105+
}
106+
}
107+
});
108+
}
109+
}
110+
}

QRCoder/Base64QRCode.cs

-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Drawing;
44
using System.Drawing.Imaging;
55
using System.IO;
6-
using static QRCoder.Base64QRCode;
76
using static QRCoder.QRCodeGenerator;
87

98
namespace QRCoder
@@ -88,13 +87,6 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
8887
return base64;
8988
}
9089

91-
public enum ImageType
92-
{
93-
Gif,
94-
Jpeg,
95-
Png
96-
}
97-
9890
}
9991

10092
#if NET6_0_WINDOWS

QRCoder/ImageSharp/Base64QRCode.cs

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System;
2+
using System.IO;
3+
using SixLabors.ImageSharp;
4+
using SixLabors.ImageSharp.Formats;
5+
using static QRCoder.QRCodeGenerator;
6+
7+
namespace QRCoder.ImageSharp
8+
{
9+
public class Base64QRCode : AbstractQRCode, IDisposable
10+
{
11+
private QRCode qr;
12+
13+
/// <summary>
14+
/// Constructor without params to be used in COM Objects connections
15+
/// </summary>
16+
public Base64QRCode()
17+
{
18+
qr = new QRCode();
19+
}
20+
21+
public Base64QRCode(QRCodeData data)
22+
: base(data)
23+
{
24+
qr = new QRCode(data);
25+
}
26+
27+
public override void SetQRCodeData(QRCodeData data)
28+
{
29+
qr.SetQRCodeData(data);
30+
}
31+
32+
public string GetGraphic(int pixelsPerModule)
33+
{
34+
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
35+
}
36+
37+
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
38+
{
39+
return GetGraphic(pixelsPerModule, Color.Parse(darkColorHtmlHex), Color.Parse(lightColorHtmlHex), drawQuietZones, imgType);
40+
}
41+
42+
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
43+
{
44+
var base64 = string.Empty;
45+
using (Image img = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
46+
{
47+
base64 = BitmapToBase64(img, imgType);
48+
}
49+
50+
return base64;
51+
}
52+
53+
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Image icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
54+
{
55+
var base64 = string.Empty;
56+
using (Image bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
57+
{
58+
base64 = BitmapToBase64(bmp, imgType);
59+
}
60+
61+
return base64;
62+
}
63+
64+
private string BitmapToBase64(Image img, ImageType imgType)
65+
{
66+
var base64 = string.Empty;
67+
IImageEncoder iFormat;
68+
switch (imgType)
69+
{
70+
default:
71+
case ImageType.Png:
72+
iFormat = new SixLabors.ImageSharp.Formats.Png.PngEncoder();
73+
break;
74+
case ImageType.Jpeg:
75+
iFormat = new SixLabors.ImageSharp.Formats.Jpeg.JpegEncoder();
76+
break;
77+
case ImageType.Gif:
78+
iFormat = new SixLabors.ImageSharp.Formats.Gif.GifEncoder();
79+
break;
80+
}
81+
82+
using (var memoryStream = new MemoryStream())
83+
{
84+
img.Save(memoryStream, iFormat);
85+
base64 = Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
86+
}
87+
88+
return base64;
89+
}
90+
}
91+
92+
public static class ImageSharpBase64QRCodeHelper
93+
{
94+
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
95+
{
96+
using (var qrGenerator = new QRCodeGenerator())
97+
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
98+
using (var qrCode = new Base64QRCode(qrCodeData))
99+
{
100+
return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex, drawQuietZones, imgType);
101+
}
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)