Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591;CS1572;CS1573;CS1587;IDE0005</NoWarn>
<NoWarn>$(NoWarn);IDE0005</NoWarn>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisMode>None</AnalysisMode>
</PropertyGroup>
Expand Down
5 changes: 5 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<DelaySign>false</DelaySign>
</PropertyGroup>

<!-- Suppress XML comment warnings for non-packable projects -->
<PropertyGroup Condition="'$(IsPackable)' != 'true'">
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>

<!-- Normalize file paths for releases -->
<PropertyGroup>
<ContinuousIntegrationBuild Condition="'$(Configuration)' == 'Release' AND '$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
Expand Down
63 changes: 63 additions & 0 deletions QRCoder.Xaml/XamlQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,75 @@

namespace QRCoder.Xaml;

/// <summary>
/// Represents a QR code generator that outputs QR codes as XAML DrawingImage objects.
/// </summary>
public class XamlQRCode : AbstractQRCode, IDisposable
{
/// <summary>
/// Constructor without params to be used in COM Objects connections
/// </summary>
public XamlQRCode() { }

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

/// <summary>
/// Returns a XAML DrawingImage that contains the resulting QR code.
/// </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 XAML DrawingImage.</returns>
public DrawingImage GetGraphic(int pixelsPerModule)
=> GetGraphic(pixelsPerModule, true);

/// <summary>
/// Returns a XAML DrawingImage that contains the resulting QR code.
/// </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="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a XAML DrawingImage.</returns>
public DrawingImage GetGraphic(int pixelsPerModule, bool drawQuietZones)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);
}

/// <summary>
/// Returns a XAML DrawingImage that contains the resulting QR code.
/// </summary>
/// <param name="viewBox">The size of the view box for the QR code.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a XAML DrawingImage.</returns>
public DrawingImage GetGraphic(Size viewBox, bool drawQuietZones = true)
=> GetGraphic(viewBox, new SolidColorBrush(Colors.Black), new SolidColorBrush(Colors.White), drawQuietZones);

/// <summary>
/// Returns a XAML DrawingImage that contains the resulting QR code with specified colors.
/// </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="darkColorHex">The color of the dark modules in hexadecimal format.</param>
/// <param name="lightColorHex">The color of the light modules in hexadecimal format.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a XAML DrawingImage.</returns>
public DrawingImage GetGraphic(int pixelsPerModule, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
var viewBox = new Size(pixelsPerModule * drawableModulesCount, pixelsPerModule * drawableModulesCount);
return GetGraphic(viewBox, new SolidColorBrush((Color)ColorConverter.ConvertFromString(darkColorHex)), new SolidColorBrush((Color)ColorConverter.ConvertFromString(lightColorHex)), drawQuietZones);
}

/// <summary>
/// Returns a XAML DrawingImage that contains the resulting QR code with specified brushes.
/// </summary>
/// <param name="viewBox">The size of the view box for the QR code.</param>
/// <param name="darkBrush">The brush for the dark modules.</param>
/// <param name="lightBrush">The brush for the light modules.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the QR code graphic as a XAML DrawingImage.</returns>
public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
Expand Down Expand Up @@ -67,6 +107,12 @@ public DrawingImage GetGraphic(Size viewBox, Brush darkBrush, Brush lightBrush,
private int GetDrawableModulesCount(bool drawQuietZones = true)
=> QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);

/// <summary>
/// Calculates the number of units per module for the given view box size.
/// </summary>
/// <param name="viewBox">The size of the view box for the QR code.</param>
/// <param name="drawQuietZones">Indicates if quiet zones around the QR code should be drawn.</param>
/// <returns>Returns the number of units per module.</returns>
public double GetUnitsPerModule(Size viewBox, bool drawQuietZones = true)
{
var drawableModulesCount = GetDrawableModulesCount(drawQuietZones);
Expand All @@ -75,8 +121,25 @@ public double GetUnitsPerModule(Size viewBox, bool drawQuietZones = true)
}
}

/// <summary>
/// Provides static methods for creating XAML DrawingImage QR codes.
/// </summary>
public static class XamlQRCodeHelper
{
/// <summary>
/// Creates a XAML DrawingImage 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="darkColorHex">The color of the dark modules in hexadecimal format.</param>
/// <param name="lightColorHex">The color of the light modules in hexadecimal 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>
/// <returns>Returns the QR code graphic as a XAML DrawingImage.</returns>
public static DrawingImage GetQRCode(string plainText, int pixelsPerModule, string darkColorHex, string lightColorHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, bool drawQuietZones = true)
{
using var qrGenerator = new QRCodeGenerator();
Expand Down
32 changes: 32 additions & 0 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class AsciiQRCode : AbstractQRCode, IDisposable
/// </summary>
public AsciiQRCode() { }

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


Expand Down Expand Up @@ -132,6 +136,21 @@ public string GetGraphicSmall(bool drawQuietZones = true, bool invert = false, s
/// </summary>
public static class AsciiQRCodeHelper
{
/// <summary>
/// Generates an ASCII QR code graphic with specified parameters.
/// </summary>
/// <param name="plainText">The text to be encoded in the QR code.</param>
/// <param name="pixelsPerModule">Number of repeated characters per module.</param>
/// <param name="darkColorString">String representing dark modules.</param>
/// <param name="whiteSpaceString">String representing light modules.</param>
/// <param name="eccLevel">Error correction level for the QR code.</param>
/// <param name="forceUtf8">Force UTF-8 encoding.</param>
/// <param name="utf8BOM">Include UTF-8 byte order mark.</param>
/// <param name="eciMode">Extended Channel Interpretation mode.</param>
/// <param name="requestedVersion">Specific QR code version to use.</param>
/// <param name="endOfLine">End of line separator.</param>
/// <param name="drawQuietZones">Include quiet zones around the QR code.</param>
/// <returns>ASCII representation of the QR code.</returns>
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)
{
using var qrGenerator = new QRCodeGenerator();
Expand All @@ -140,6 +159,19 @@ public static string GetQRCode(string plainText, int pixelsPerModule, string dar
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, drawQuietZones, endOfLine);
}

/// <summary>
/// Generates a small ASCII QR code graphic with specified parameters.
/// </summary>
/// <param name="plainText">The text to be encoded in the QR code.</param>
/// <param name="eccLevel">Error correction level for the QR code.</param>
/// <param name="forceUtf8">Force UTF-8 encoding.</param>
/// <param name="utf8BOM">Include UTF-8 byte order mark.</param>
/// <param name="eciMode">Extended Channel Interpretation mode.</param>
/// <param name="requestedVersion">Specific QR code version to use.</param>
/// <param name="endOfLine">End of line separator.</param>
/// <param name="drawQuietZones">Include quiet zones around the QR code.</param>
/// <param name="invert">Invert dark and light colors.</param>
/// <returns>Minified ASCII representation of the QR code.</returns>
public static string GetQRCode(string plainText, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, string endOfLine = "\n", bool drawQuietZones = true, bool invert = true)
{
using var qrGenerator = new QRCodeGenerator();
Expand Down
14 changes: 14 additions & 0 deletions QRCoder/ArtQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,14 @@ private Bitmap Resize(Bitmap image, int newSize)
/// </summary>
public enum QuietZoneStyle
{
/// <summary>
/// Quiet zones are rendered with dotted modules.
/// </summary>
Dotted,

/// <summary>
/// Quiet zones are rendered with flat, solid modules.
/// </summary>
Flat
}

Expand All @@ -245,7 +252,14 @@ public enum QuietZoneStyle
/// </summary>
public enum BackgroundImageStyle
{
/// <summary>
/// Background image spans the complete graphic.
/// </summary>
Fill,

/// <summary>
/// Background image is only painted in the data area, excluding the quiet zone.
/// </summary>
DataAreaOnly
}
}
Expand Down
12 changes: 6 additions & 6 deletions QRCoder/Base64QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,22 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
/// </summary>
public enum ImageType
{
/// <summary>
/// Graphics Interchange Format (GIF) image format, a bitmap image format with limited color support
/// </summary>
#if NET6_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
Gif,
/// <summary>
/// GIF image format.
/// Joint Photographic Experts Group (JPEG) image format, a lossy compressed 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.
/// Portable Network Graphics (PNG) image format, a lossless raster graphics format
/// </summary>
Png
}
Expand Down
Loading