Skip to content

Commit 6ec439e

Browse files
authored
Merge pull request #495 from Shane32/support_base64_linux
Support Base64QRCode on .NET6+ on Linux
2 parents c9c9f1b + e61e4bf commit 6ec439e

File tree

5 files changed

+136
-19
lines changed

5 files changed

+136
-19
lines changed

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)

QRCoder/QRCoder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net35;net40;netstandard1.3;netstandard2.0;net5.0;net5.0-windows;net6.0;net6.0-windows</TargetFrameworks>
55
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
66
<DefineConstants Condition="'$(TargetFramework)' == 'net5.0-windows'">$(DefineConstants);NET5_0_WINDOWS</DefineConstants>
7-
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
7+
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
88
<CheckEolTargetFramework>false</CheckEolTargetFramework>
99
<DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
1010
</PropertyGroup>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#if !NETCOREAPP1_1
2+
using QRCoder;
3+
using QRCoderTests.Helpers.XUnitExtenstions;
4+
using Shouldly;
5+
using System;
6+
using System.Drawing;
7+
using System.IO;
8+
using Xunit;
9+
10+
namespace QRCoderTests
11+
{
12+
public class Base64QRCodeRendererTests
13+
{
14+
private readonly QRCodeData data;
15+
16+
public Base64QRCodeRendererTests()
17+
{
18+
var gen = new QRCodeGenerator();
19+
data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L);
20+
}
21+
22+
[Fact]
23+
[Category("QRRenderer/Base64QRCode")]
24+
public void can_render_base64_qrcode_blackwhite()
25+
{
26+
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5);
27+
var base64QRCode = new Base64QRCode(data).GetGraphic(5);
28+
base64QRCode.ShouldBe(Convert.ToBase64String(pngCodeGfx));
29+
}
30+
31+
[Fact]
32+
[Category("QRRenderer/Base64QRCode")]
33+
public void can_render_base64_qrcode_noquietzones()
34+
{
35+
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, false);
36+
var base64QRCode = new Base64QRCode(data).GetGraphic(5, Color.Black, Color.White, false);
37+
base64QRCode.ShouldBe(Convert.ToBase64String(pngCodeGfx));
38+
}
39+
40+
[Fact]
41+
[Category("QRRenderer/Base64QRCode")]
42+
public void can_render_base64_qrcode_color()
43+
{
44+
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 0, 0 }, new byte[] { 0, 0, 255 });
45+
var base64QRCode = new Base64QRCode(data).GetGraphic(5, Color.Red, Color.Blue);
46+
base64QRCode.ShouldBe(Convert.ToBase64String(pngCodeGfx));
47+
}
48+
49+
[Fact]
50+
[Category("QRRenderer/Base64QRCode")]
51+
public void can_render_base64_qrcode_transparent()
52+
{
53+
var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 0, 255, 0, 255 }, new byte[] { 255, 255, 255, 0 });
54+
var base64QRCode = new Base64QRCode(data).GetGraphic(5, Color.Lime, Color.Transparent);
55+
base64QRCode.ShouldBe(Convert.ToBase64String(pngCodeGfx));
56+
}
57+
58+
#if NETFRAMEWORK || NETCOREAPP2_0 || NET5_0 || NET6_0_WINDOWS
59+
[Fact]
60+
[Category("QRRenderer/Base64QRCode")]
61+
public void can_render_base64_qrcode_jpeg()
62+
{
63+
var ms = new MemoryStream();
64+
using (var bitmap = new QRCode(data).GetGraphic(5, Color.Black, Color.White, true))
65+
{
66+
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
67+
}
68+
ms.Position = 0;
69+
var jpgString = Convert.ToBase64String(ms.ToArray());
70+
var base64QRCode = new Base64QRCode(data).GetGraphic(5, Color.Black, Color.White, true, Base64QRCode.ImageType.Jpeg);
71+
base64QRCode.ShouldBe(jpgString);
72+
}
73+
#endif
74+
}
75+
}
76+
77+
#endif

QRCoderTests/Helpers/HelperFunctions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#if !NETCOREAPP1_1
66
using System.Drawing;
77
#endif
8-
#if NETFRAMEWORK || NET5_0_WINDOWS
8+
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
99
using SW = System.Windows;
1010
using System.Windows.Media;
1111
using System.Windows.Media.Imaging;
@@ -17,7 +17,7 @@ namespace QRCoderTests.Helpers
1717
public static class HelperFunctions
1818
{
1919

20-
#if NETFRAMEWORK || NET5_0_WINDOWS
20+
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
2121
public static BitmapSource ToBitmapSource(DrawingImage source)
2222
{
2323
DrawingVisual drawingVisual = new DrawingVisual();

QRCoderTests/QRCoderTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<UseWindowsForms Condition="'$(TargetFramework)' == 'net5.0-windows'">true</UseWindowsForms>
55
<UseWPF Condition="'$(TargetFramework)' == 'net5.0-windows'">true</UseWPF>
66
<DefineConstants Condition="'$(TargetFramework)' == 'net5.0-windows'">$(DefineConstants);NET5_0_WINDOWS</DefineConstants>
7+
<DefineConstants Condition="'$(TargetFramework)' == 'net6.0-windows'">$(DefineConstants);NET6_0_WINDOWS</DefineConstants>
78
<IsPackable>false</IsPackable>
89
<IsTestProject>true</IsTestProject>
910
<CheckEolTargetFramework>false</CheckEolTargetFramework>

0 commit comments

Comments
 (0)