Skip to content

Commit

Permalink
Add XML comments to all public members (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebude authored Jun 3, 2024
2 parents 4a8b36b + a5dd7dd commit d7b9c81
Show file tree
Hide file tree
Showing 42 changed files with 1,504 additions and 228 deletions.
6 changes: 6 additions & 0 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

namespace QRCoder
{
/// <summary>
/// Represents an ASCII-style QR code generator that provides functionality to render QR codes as textual representations.
/// </summary>
public class AsciiQRCode : AbstractQRCode, IDisposable
{
/// <summary>
Expand Down Expand Up @@ -124,6 +127,9 @@ public string GetGraphicSmall(bool drawQuietZones = true, bool invert = false, s
}


/// <summary>
/// Provides static methods for generating ASCII-style QR codes.
/// </summary>
public static class AsciiQRCodeHelper
{
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorString, string whiteSpaceString, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, string endOfLine = "\n", bool drawQuietZones = true)
Expand Down
24 changes: 21 additions & 3 deletions QRCoder/AbstractQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
namespace QRCoder
{
/// <summary>
/// Abstract base class for generating QR codes.
/// Derived classes typically render a QR code into a specific format (png, System.Drawing.Bitmap, PDF, etc).
/// </summary>
public abstract class AbstractQRCode
{
/// <summary>
/// Gets or sets the QRCodeData used to generate the QR code.
/// </summary>
protected QRCodeData QrCodeData { get; set; }

/// <summary>
/// Initializes a new instance of the AbstractQRCode class.
/// </summary>
protected AbstractQRCode() {
this.QrCodeData = null!;
}

/// <summary>
/// Initializes a new instance of the AbstractQRCode class with the specified QRCodeData.
/// </summary>
/// <param name="data">The QRCodeData object generated by QRCodeGenerator.CreateQrCode().</param>
protected AbstractQRCode(QRCodeData data) {
this.QrCodeData = data;
}

/// <summary>
/// Set a QRCodeData object that will be used to generate QR code. Used in COM Objects connections
/// Sets the QRCodeData object that will be used to generate the QR code.
/// This method is useful for COM objects connections.
/// </summary>
/// <param name="data">Need a QRCodeData object generated by QRCodeGenerator.CreateQrCode()</param>
/// <param name="data">The QRCodeData object generated by QRCodeGenerator.CreateQrCode().</param>
virtual public void SetQRCodeData(QRCodeData data) {
this.QrCodeData = data;
}

/// <summary>
/// Disposes the QRCodeData object.
/// </summary>
public void Dispose()
{
this.QrCodeData?.Dispose();
this.QrCodeData = null!;
}
}
}
}
11 changes: 9 additions & 2 deletions QRCoder/ArtQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@
// pull request raised to extend library used.
namespace QRCoder
{
/// <summary>
/// Represents an art-style QR code generator that provides functionality to render QR codes with dots as modules.
/// </summary>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public class ArtQRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Initializes a new instance of the <see cref="ArtQRCode"/> class.
/// Constructor without params to be used in COM Objects connections
/// </summary>
public ArtQRCode() { }

/// <summary>
/// Creates new ArtQrCode object
/// Initializes a new instance of the <see cref="ArtQRCode"/> class with the specified <see cref="QRCodeData"/>.
/// </summary>
/// <param name="data">QRCodeData generated by the QRCodeGenerator</param>
/// <param name="data"><see cref="QRCodeData"/> generated by the <see cref="QRCodeGenerator"/>.</param>
public ArtQRCode(QRCodeData data) : base(data) { }

/// <summary>
Expand Down Expand Up @@ -256,6 +260,9 @@ public enum BackgroundImageStyle
}
}

/// <summary>
/// Provides static methods for creating art-style QR codes.
/// </summary>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Expand Down
84 changes: 81 additions & 3 deletions QRCoder/Base64QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,58 @@

namespace QRCoder
{
/// <summary>
/// Represents a QR code generator that outputs base64-encoded images.
/// </summary>
public class Base64QRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// 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)
{
return this.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)
{
return this.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)
Expand Down Expand Up @@ -78,6 +108,18 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
}

#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
Expand All @@ -94,6 +136,12 @@ public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
#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
Expand Down Expand Up @@ -124,23 +172,53 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
}
#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())
Expand All @@ -151,4 +229,4 @@ public static string GetQRCode(string plainText, int pixelsPerModule, string dar
}
}

#endif
#endif
64 changes: 62 additions & 2 deletions QRCoder/BitmapByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,53 @@
namespace QRCoder
{

/// <summary>
/// Represents a QR code generator that outputs QR codes as bitmap byte arrays.
/// </summary>
// ReSharper disable once InconsistentNaming
public class BitmapByteQRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// Initializes a new instance of the <see cref="BitmapByteQRCode"/> class.
/// Constructor without parameters to be used in COM objects connections.
/// </summary>
public BitmapByteQRCode() { }

/// <summary>
/// Initializes a new instance of the <see cref="BitmapByteQRCode"/> class with the specified <see cref="QRCodeData"/>.
/// </summary>
/// <param name="data"><see cref="QRCodeData"/> generated by the QRCodeGenerator.</param>
public BitmapByteQRCode(QRCodeData data) : base(data) { }

/// <summary>
/// Returns the QR code graphic as a bitmap byte array.
/// </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 bitmap byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF });
}

/// <summary>
/// Returns the QR code graphic as a bitmap byte array.
/// </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>
/// <returns>Returns the QR code graphic as a bitmap byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex)
{
return GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));
}

/// <summary>
/// Returns the QR code graphic as a bitmap byte array.
/// </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="darkColorRgb">The color of the dark modules as an RGB byte array.</param>
/// <param name="lightColorRgb">The color of the light modules as an RGB byte array.</param>
/// <returns>Returns the QR code graphic as a bitmap byte array.</returns>
public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightColorRgb)
{
var sideLength = this.QrCodeData.ModuleMatrix.Count * pixelsPerModule;
Expand Down Expand Up @@ -76,16 +103,26 @@ public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightC
return bmp.ToArray();
}

/// <summary>
/// Converts a hex color string to a byte array.
/// </summary>
/// <param name="colorString">The hex color string to convert.</param>
/// <returns>Returns the color as a byte array.</returns>
private byte[] HexColorToByteArray(string colorString)
{
if (colorString.StartsWith("#"))
colorString = colorString.Substring(1);
byte[] byteColor = new byte[colorString.Length / 2];
for (int i = 0; i < byteColor.Length; i++)
byteColor[i] = byte.Parse(colorString.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture);
byteColor[i] = byte.Parse(colorString.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture);
return byteColor;
}

/// <summary>
/// Converts an integer to a 4-byte array.
/// </summary>
/// <param name="inp">The integer to convert.</param>
/// <returns>Returns the integer as a 4-byte array.</returns>
private byte[] IntTo4Byte(int inp)
{
byte[] bytes = new byte[2];
Expand All @@ -99,8 +136,24 @@ private byte[] IntTo4Byte(int inp)
}


/// <summary>
/// Provides helper functions for creating bitmap byte array QR codes.
/// </summary>
public static class BitmapByteQRCodeHelper
{
/// <summary>
/// Creates a bitmap byte array 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>
/// <returns>Returns the QR code graphic as a bitmap byte array.</returns>
public static byte[] GetQRCode(string plainText, int pixelsPerModule, string darkColorHtmlHex,
string lightColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false,
EciMode eciMode = EciMode.Default, int requestedVersion = -1)
Expand All @@ -113,6 +166,13 @@ public static byte[] GetQRCode(string plainText, int pixelsPerModule, string dar
return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex);
}

/// <summary>
/// Creates a bitmap byte array QR code with a single function call.
/// </summary>
/// <param name="txt">The text or payload to be encoded inside the QR code.</param>
/// <param name="eccLevel">The level of error correction data.</param>
/// <param name="size">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 bitmap byte array.</returns>
public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size)
{
using (var qrGen = new QRCodeGenerator())
Expand Down
Loading

0 comments on commit d7b9c81

Please sign in to comment.