Skip to content

Commit f602bde

Browse files
committed
Cleanup ifdef
1 parent 295730d commit f602bde

24 files changed

+566
-159
lines changed

QRCoder.Xaml/XamlQRCode.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
2-
using System;
1+
using System;
32
using System.Windows;
43
using System.Windows.Media;
54
using static QRCoder.QRCodeGenerator;
@@ -93,5 +92,3 @@ public static DrawingImage GetQRCode(string plainText, int pixelsPerModule, stri
9392
}
9493
}
9594
}
96-
97-
#endif

QRCoder/ASCIIQRCode.cs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,29 @@ public AsciiQRCode(QRCodeData data) : base(data) { }
1616

1717

1818
/// <summary>
19-
/// Returns a strings that contains the resulting QR code as ASCII chars.
19+
/// Returns a strings that contains the resulting QR code as textual representation.
2020
/// </summary>
2121
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
2222
/// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
2323
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
24+
/// <param name="drawQuietZones">Bool that defines if quiet zones around the QR code shall be drawn</param>
2425
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
2526
/// <returns></returns>
2627
public string GetGraphic(int repeatPerModule, string darkColorString = "██", string whiteSpaceString = " ", bool drawQuietZones = true, string endOfLine = "\n")
2728
{
29+
if (repeatPerModule < 1)
30+
throw new Exception("The repeatPerModule-parameter must be 1 or greater.");
2831
return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString, drawQuietZones));
2932
}
30-
33+
3134

3235
/// <summary>
3336
/// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
3437
/// </summary>
3538
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
3639
/// <param name="darkColorString">String for use as dark color modules. In case of string make sure whiteSpaceString has the same length.</param>
3740
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
41+
/// <param name="drawQuietZones">Bool that defines if quiet zones around the QR code shall be drawn</param>
3842
/// <returns></returns>
3943
public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString = "██", string whiteSpaceString = " ", bool drawQuietZones = true)
4044
{
@@ -62,6 +66,61 @@ public string[] GetLineByLineGraphic(int repeatPerModule, string darkColorString
6266
}
6367
return qrCode.ToArray();
6468
}
69+
70+
/// <summary>
71+
/// Returns a strings that contains the resulting QR code as minified textual representation.
72+
/// </summary>
73+
/// <param name="drawQuietZones">Bool that defines if quiet zones around the QR code shall be drawn</param>
74+
/// <param name="invert">If set to true, dark and light colors will be inverted</param>
75+
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
76+
/// <returns></returns>
77+
public string GetGraphicSmall(bool drawQuietZones = true, bool invert = false, string endOfLine = "\n")
78+
{
79+
bool BLACK = true, WHITE = false;
80+
81+
var palette = new
82+
{
83+
WHITE_ALL = "\u2588",
84+
WHITE_BLACK = "\u2580",
85+
BLACK_WHITE = "\u2584",
86+
BLACK_ALL = " ",
87+
};
88+
89+
var moduleData = QrCodeData.ModuleMatrix;
90+
var sbSize = (moduleData.Count + endOfLine.Length) * (int)Math.Ceiling(moduleData.Count / 2.0) - 1;
91+
var lineBuilder = new StringBuilder(sbSize);
92+
93+
var quietZonesModifier = (drawQuietZones ? 0 : 8);
94+
var quietZonesOffset = (int)(quietZonesModifier * 0.5);
95+
var sideLength = (moduleData.Count - quietZonesModifier);
96+
97+
for (var row = 0; row < sideLength; row += 2)
98+
{
99+
for (var col = 0; col < sideLength; col++)
100+
{
101+
var current = moduleData[col + quietZonesOffset][row + quietZonesOffset] ^ invert;
102+
var nextRowId = row + quietZonesOffset + 1;
103+
104+
// Set next to whitespace "color"
105+
var next = BLACK;
106+
// Fill next with value, if in data range
107+
if (nextRowId < QrCodeData.ModuleMatrix.Count)
108+
next = moduleData[col + quietZonesOffset][nextRowId] ^ invert;
109+
110+
if (current == WHITE && next == WHITE)
111+
lineBuilder.Append(palette.WHITE_ALL);
112+
else if (current == WHITE && next == BLACK)
113+
lineBuilder.Append(palette.WHITE_BLACK);
114+
else if (current == BLACK && next == WHITE)
115+
lineBuilder.Append(palette.BLACK_WHITE);
116+
else
117+
lineBuilder.Append(palette.BLACK_ALL);
118+
}
119+
if (row + 2 < sideLength)
120+
lineBuilder.Append(endOfLine);
121+
}
122+
return lineBuilder.ToString();
123+
}
65124
}
66125

67126

@@ -74,5 +133,13 @@ public static string GetQRCode(string plainText, int pixelsPerModule, string dar
74133
using (var qrCode = new AsciiQRCode(qrCodeData))
75134
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, drawQuietZones, endOfLine);
76135
}
136+
137+
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)
138+
{
139+
using (var qrGenerator = new QRCodeGenerator())
140+
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
141+
using (var qrCode = new AsciiQRCode(qrCodeData))
142+
return qrCode.GetGraphicSmall(drawQuietZones, invert, endOfLine);
143+
}
77144
}
78145
}

QRCoder/ArtQRCode.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
1+
#if SYSTEM_DRAWING
22

33
using System;
44
using System.Drawing;
@@ -9,7 +9,7 @@
99
// pull request raised to extend library used.
1010
namespace QRCoder
1111
{
12-
#if NET6_0_WINDOWS
12+
#if NET6_0_OR_GREATER
1313
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
1414
#endif
1515
public class ArtQRCode : AbstractQRCode, IDisposable
@@ -120,9 +120,10 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
120120
if (finderPatternImage != null)
121121
{
122122
var finderPatternSize = 7 * pixelsPerModule;
123-
graphics.DrawImage(finderPatternImage, new Rectangle(0, 0, finderPatternSize, finderPatternSize));
124-
graphics.DrawImage(finderPatternImage, new Rectangle(size - finderPatternSize, 0, finderPatternSize, finderPatternSize));
125-
graphics.DrawImage(finderPatternImage, new Rectangle(0, size - finderPatternSize, finderPatternSize, finderPatternSize));
123+
var finderPatternOffset = drawQuietZones ? 4 * pixelsPerModule : 0;
124+
graphics.DrawImage(finderPatternImage, new Rectangle(finderPatternOffset, finderPatternOffset, finderPatternSize, finderPatternSize));
125+
graphics.DrawImage(finderPatternImage, new Rectangle(size - finderPatternOffset - finderPatternSize, finderPatternOffset, finderPatternSize, finderPatternSize));
126+
graphics.DrawImage(finderPatternImage, new Rectangle(finderPatternOffset, size - finderPatternOffset - finderPatternSize, finderPatternSize, finderPatternSize));
126127
}
127128
graphics.Save();
128129
}
@@ -257,7 +258,7 @@ public enum BackgroundImageStyle
257258
}
258259
}
259260

260-
#if NET6_0_WINDOWS
261+
#if NET6_0_OR_GREATER
261262
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
262263
#endif
263264
public static class ArtQRCodeHelper

QRCoder/Base64QRCode.cs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
1-
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
1+
#if !NETSTANDARD1_3
22
using System;
33
using System.Drawing;
44
using System.Drawing.Imaging;
55
using System.IO;
6+
using System.Runtime.InteropServices;
67
using static QRCoder.Base64QRCode;
78
using static QRCoder.QRCodeGenerator;
89

910
namespace QRCoder
1011
{
11-
#if NET6_0_WINDOWS
12-
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
13-
#endif
1412
public class Base64QRCode : AbstractQRCode, IDisposable
1513
{
16-
private QRCode qr;
17-
1814
/// <summary>
1915
/// Constructor without params to be used in COM Objects connections
2016
/// </summary>
2117
public Base64QRCode() {
22-
qr = new QRCode();
2318
}
2419

2520
public Base64QRCode(QRCodeData data) : base(data) {
26-
qr = new QRCode(data);
27-
}
28-
29-
public override void SetQRCodeData(QRCodeData data) {
30-
this.qr.SetQRCodeData(data);
3121
}
3222

3323
public string GetGraphic(int pixelsPerModule)
@@ -43,25 +33,70 @@ public string GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string li
4333

4434
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
4535
{
36+
if (imgType == ImageType.Png)
37+
{
38+
var pngCoder = new PngByteQRCode(QrCodeData);
39+
40+
byte[] pngData;
41+
if (darkColor == Color.Black && lightColor == Color.White)
42+
{
43+
pngData = pngCoder.GetGraphic(pixelsPerModule, drawQuietZones);
44+
}
45+
else
46+
{
47+
byte[] darkColorBytes;
48+
byte[] lightColorBytes;
49+
if (darkColor.A != 255 || lightColor.A != 255)
50+
{
51+
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A };
52+
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A };
53+
}
54+
else
55+
{
56+
darkColorBytes = new byte[] { darkColor.R, darkColor.G, darkColor.B };
57+
lightColorBytes = new byte[] { lightColor.R, lightColor.G, lightColor.B };
58+
}
59+
pngData = pngCoder.GetGraphic(pixelsPerModule, darkColorBytes, lightColorBytes, drawQuietZones);
60+
}
61+
62+
return Convert.ToBase64String(pngData, Base64FormattingOptions.None);
63+
}
64+
65+
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
66+
#pragma warning disable CA1416 // Validate platform compatibility
67+
var qr = new QRCode(QrCodeData);
4668
var base64 = string.Empty;
4769
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones))
4870
{
4971
base64 = BitmapToBase64(bmp, imgType);
5072
}
5173
return base64;
74+
#pragma warning restore CA1416 // Validate platform compatibility
75+
#else
76+
throw new PlatformNotSupportedException("Only the PNG image type is supported on this platform.");
77+
#endif
5278
}
5379

80+
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
81+
#if NET6_0_OR_GREATER
82+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
83+
#endif
5484
public string GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon, int iconSizePercent = 15, int iconBorderWidth = 6, bool drawQuietZones = true, ImageType imgType = ImageType.Png)
5585
{
86+
var qr = new QRCode(QrCodeData);
5687
var base64 = string.Empty;
5788
using (Bitmap bmp = qr.GetGraphic(pixelsPerModule, darkColor, lightColor, icon, iconSizePercent, iconBorderWidth, drawQuietZones))
5889
{
5990
base64 = BitmapToBase64(bmp, imgType);
6091
}
6192
return base64;
6293
}
94+
#endif
6395

64-
96+
#if NETFRAMEWORK || NETSTANDARD2_0 || NET5_0 || NET6_0_WINDOWS
97+
#if NET6_0_OR_GREATER
98+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
99+
#endif
65100
private string BitmapToBase64(Bitmap bmp, ImageType imgType)
66101
{
67102
var base64 = string.Empty;
@@ -87,19 +122,23 @@ private string BitmapToBase64(Bitmap bmp, ImageType imgType)
87122
}
88123
return base64;
89124
}
125+
#endif
90126

91127
public enum ImageType
92128
{
129+
#if NET6_0_OR_GREATER
130+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
131+
#endif
93132
Gif,
133+
#if NET6_0_OR_GREATER
134+
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
135+
#endif
94136
Jpeg,
95137
Png
96138
}
97139

98140
}
99141

100-
#if NET6_0_WINDOWS
101-
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
102-
#endif
103142
public static class Base64QRCodeHelper
104143
{
105144
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)

0 commit comments

Comments
 (0)