forked from codebude/QRCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase64QRCode.cs
217 lines (202 loc) · 10.1 KB
/
Base64QRCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#if !NETSTANDARD1_3
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using static QRCoder.Base64QRCode;
using static QRCoder.QRCodeGenerator;
namespace QRCoder;
/// <summary>
/// Represents a QR code generator that outputs base64-encoded images.
/// </summary>
public class Base64QRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="Base64QRCode"/> class.
/// Constructor without parameters to be used in COM objects connections.
/// </summary>
public Base64QRCode()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Base64QRCode"/> class with the specified <see cref="QRCodeData"/>.
/// </summary>
/// <param name="data"><see cref="QRCodeData"/> generated by the QRCodeGenerator.</param>
public Base64QRCode(QRCodeData data) : base(data)
{
}
/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as a PNG image.
/// </summary>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
public string GetGraphic(int pixelsPerModule)
=> GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as an image.
/// </summary>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param>
/// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
=> GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), drawQuietZones, imgType);
/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as an image.
/// </summary>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <param name="darkColor">The color of the dark modules.</param>
/// <param name="lightColor">The color of the light modules.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
if (imgType == ImageType.Png)
{
var pngCoder = new PngByteQRCode(QrCodeData);
byte[] pngData;
if (darkColor == Color.Black && lightColor == Color.White)
{
pngData = pngCoder.GetGraphic(pixelsPerModule, drawQuietZones);
}
else
{
byte[] darkColorBytes;
byte[] lightColorBytes;
if (darkColor.A != 255 || lightColor.A != 255)
{
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A };
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A };
}
else
{
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B };
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B };
}
pngData = pngCoder.GetGraphic(pixelsPerModule, darkColorBytes, lightColorBytes, drawQuietZones);
}
return Convert.ToBase64String(pngData, Base64FormattingOptions.None);
}
#if SYSTEM_DRAWING
#pragma warning disable CA1416 // Validate platform compatibility
var qr = new QRCode(QrCodeData);
var base64 = string.Empty;
using (var bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
{
base64 = BitmapToBase64(bmp, imgType);
}
return base64;
#pragma warning restore CA1416 // Validate platform compatibility
#else
throw new PlatformNotSupportedException("Only the PNG image type is supported on this platform.");
#endif
}
#if SYSTEM_DRAWING
/// <summary>
/// Returns a base64-encoded string that contains the resulting QR code as an image with an embedded icon.
/// </summary>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <param name="darkColor">The color of the dark modules.</param>
/// <param name="lightColor">The color of the light modules.</param>
/// <param name="icon">The icon to embed in the center of the QR code.</param>
/// <param name="iconSizePercent">The size of the icon as a percentage of the QR code.</param>
/// <param name="iconBorderWidth">The width of the border around the icon.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
{
var qr = new QRCode(QrCodeData);
var base64 = string.Empty;
using (var bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
{
base64 = BitmapToBase64(bmp, imgType);
}
return base64;
}
#endif
#if SYSTEM_DRAWING
/// <summary>
/// Converts a bitmap to a base64-encoded string.
/// </summary>
/// <param name="bmp">The bitmap to convert.</param>
/// <param name="imgType">The type of image (PNG, JPEG, GIF).</param>
/// <returns>Returns the base64-encoded string representation of the bitmap.</returns>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
{
var iFormat = imgType switch
{
ImageType.Png => ImageFormat.Png,
ImageType.Jpeg => ImageFormat.Jpeg,
ImageType.Gif => ImageFormat.Gif,
_ => ImageFormat.Png,
};
using var memoryStream = new MemoryStream();
bmp.Save(memoryStream, iFormat);
return Convert.ToBase64String(memoryStream.ToArray(), Base64FormattingOptions.None);
}
#endif
/// <summary>
/// Specifies the type of image to generate.
/// </summary>
public enum ImageType
{
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
/// <summary>
/// GIF image format.
/// </summary>
Gif,
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
/// <summary>
/// JPEG image format.
/// </summary>
Jpeg,
/// <summary>
/// PNG image format.
/// </summary>
Png
}
}
/// <summary>
/// Provides static methods for creating base64-encoded QR codes.
/// </summary>
public static class Base64QRCodeHelper
{
/// <summary>
/// Creates a base64-encoded QR code with a single function call.
/// </summary>
/// <param name="plainText">The text or payload to be encoded inside the QR code.</param>
/// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param>
/// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param>
/// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param>
/// <param name="eccLevel">The level of error correction data.</param>
/// <param name="forceUtf8">Specifies whether the generator should be forced to work in UTF-8 mode.</param>
/// <param name="utf8BOM">Specifies whether the byte-order-mark should be used.</param>
/// <param name="eciMode">Specifies which ECI mode should be used.</param>
/// <param name="requestedVersion">Sets the fixed QR code target version.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <param name="imgType">The type of image to generate (PNG, JPEG, GIF).</param>
/// <returns>Returns the QR code graphic as a base64-encoded string.</returns>
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)
{
using var qrGenerator = new QRCodeGenerator();
using var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion);
using var qrCode = new Base64QRCode(qrCodeData);
return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex, drawQuietZones, imgType);
}
}
#endif