Skip to content

Commit 48ecb2e

Browse files
committed
QrCodeBuilder experimentation
1 parent ba16519 commit 48ecb2e

22 files changed

+692
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableEccLevel
4+
{
5+
QRCodeGenerator.ECCLevel EccLevel { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableEciMode
4+
{
5+
QRCodeGenerator.EciMode EciMode { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IConfigurableVersion
4+
{
5+
int Version { get; set; }
6+
}
7+
}

QRCoder/Builders/Payloads/IPayload.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public interface IPayload
4+
{
5+
QRCodeData ToMatrix();
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
3+
namespace QRCoder.Builders.Payloads.Implementations
4+
{
5+
public class EmailPayload : PayloadBase
6+
{
7+
public EmailPayload(string address)
8+
{
9+
_address = address;
10+
}
11+
12+
private string _address { get; set; }
13+
private string _subject { get; set; }
14+
private string _body { get; set; }
15+
private PayloadGenerator.Mail.MailEncoding _encoding { get; set; } = PayloadGenerator.Mail.MailEncoding.MAILTO;
16+
17+
public EmailPayload WithSubject(string subject)
18+
{
19+
_subject = subject;
20+
return this;
21+
}
22+
23+
public EmailPayload WithBody(string body)
24+
{
25+
_body = body;
26+
return this;
27+
}
28+
29+
public EmailPayload WithEncoding(PayloadGenerator.Mail.MailEncoding encoding)
30+
{
31+
if (encoding != PayloadGenerator.Mail.MailEncoding.MAILTO && encoding != PayloadGenerator.Mail.MailEncoding.SMTP && encoding != PayloadGenerator.Mail.MailEncoding.MATMSG)
32+
{
33+
throw new ArgumentOutOfRangeException(nameof(encoding));
34+
}
35+
_encoding = encoding;
36+
return this;
37+
}
38+
39+
protected override string Value => new PayloadGenerator.Mail(_address, _subject, _body, _encoding).ToString();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace QRCoder.Builders.Payloads.Implementations
2+
{
3+
public class StringPayload : PayloadBase
4+
{
5+
private string _data;
6+
7+
public StringPayload(string data)
8+
{
9+
_data = data;
10+
}
11+
12+
protected override string Value => _data;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace QRCoder.Builders.Payloads.Implementations
2+
{
3+
public class WiFiPayload : PayloadBase
4+
{
5+
private string _ssid { get; set; }
6+
private string _password { get; set; }
7+
private PayloadGenerator.WiFi.Authentication _authentication { get; set; }
8+
private bool _isHiddenSSID { get; set; }
9+
private bool _isHexStrings { get; set; }
10+
11+
public WiFiPayload(string ssid)
12+
{
13+
_ssid = ssid;
14+
_password = "";
15+
_authentication = PayloadGenerator.WiFi.Authentication.nopass;
16+
}
17+
18+
public WiFiPayload(string ssid, string password, PayloadGenerator.WiFi.Authentication authentication)
19+
{
20+
_ssid = ssid;
21+
_password = password;
22+
_authentication = authentication;
23+
}
24+
25+
public WiFiPayload WithHiddenSSID()
26+
{
27+
_isHiddenSSID = true;
28+
return this;
29+
}
30+
31+
public WiFiPayload WithHexStrings()
32+
{
33+
_isHexStrings = true;
34+
return this;
35+
}
36+
37+
protected override string Value
38+
{
39+
get
40+
{
41+
var wifi = new PayloadGenerator.WiFi(_ssid, _password, _authentication, _isHiddenSSID, _isHexStrings);
42+
return wifi.ToString();
43+
}
44+
}
45+
}
46+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace QRCoder.Builders.Payloads
2+
{
3+
public abstract class PayloadBase : IPayload, IConfigurableEccLevel, IConfigurableEciMode, IConfigurableVersion
4+
{
5+
protected virtual QRCodeGenerator.ECCLevel EccLevel { get; set; } = QRCodeGenerator.ECCLevel.Default;
6+
QRCodeGenerator.ECCLevel IConfigurableEccLevel.EccLevel { get => EccLevel; set => EccLevel = value; }
7+
8+
protected virtual QRCodeGenerator.EciMode EciMode { get; set; } = QRCodeGenerator.EciMode.Default;
9+
QRCodeGenerator.EciMode IConfigurableEciMode.EciMode { get => EciMode; set => EciMode = value; }
10+
11+
protected virtual int Version { get; set; } = -1;
12+
int IConfigurableVersion.Version { get => Version; set => Version = value; }
13+
14+
protected abstract string Value { get; }
15+
16+
public virtual QRCodeData ToMatrix()
17+
{
18+
return QRCodeGenerator.GenerateQrCode(Value, EccLevel, false, false, EciMode, Version);
19+
}
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using QRCoder.Builders.Payloads;
2+
using QRCoder.Builders.Renderers;
3+
using QRCoder.Builders.Renderers.Implementations;
4+
5+
namespace QRCoder
6+
{
7+
public static class PayloadExtensions
8+
{
9+
public static T WithErrorCorrection<T>(this T payload, QRCodeGenerator.ECCLevel eccLevel)
10+
where T : IConfigurableEccLevel
11+
{
12+
payload.EccLevel = eccLevel;
13+
return payload;
14+
}
15+
16+
public static T WithEciMode<T>(this T payload, QRCodeGenerator.EciMode eciMode)
17+
where T : IConfigurableEciMode
18+
{
19+
payload.EciMode = eciMode;
20+
return payload;
21+
}
22+
23+
public static T WithVersion<T>(this T payload, int version)
24+
where T : IConfigurableVersion
25+
{
26+
payload.Version = version;
27+
return payload;
28+
}
29+
30+
public static T RenderWith<T>(this IPayload payload)
31+
where T : IRenderer, new()
32+
{
33+
var renderer = new T();
34+
renderer.Payload = payload;
35+
return renderer;
36+
}
37+
38+
public static T RenderWith<T>(this IPayload payload, int pixelsPerModule)
39+
where T : IRenderer, IConfigurablePixelsPerModule, new()
40+
{
41+
var renderer = new T();
42+
renderer.Payload = payload;
43+
renderer.PixelsPerModule = pixelsPerModule;
44+
return renderer;
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Renderers
2+
{
3+
public interface IConfigurablePixelsPerModule
4+
{
5+
int PixelsPerModule { get; set; }
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace QRCoder.Builders.Renderers
2+
{
3+
public interface IConfigurableQuietZones
4+
{
5+
bool QuietZone { get; set; }
6+
}
7+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using QRCoder.Builders.Payloads;
2+
3+
namespace QRCoder.Builders.Renderers
4+
{
5+
public interface IRenderer
6+
{
7+
IPayload Payload { set; }
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.IO;
2+
3+
namespace QRCoder.Builders.Renderers
4+
{
5+
public interface IStreamRenderer
6+
{
7+
MemoryStream ToStream();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.IO;
2+
3+
namespace QRCoder.Builders.Renderers
4+
{
5+
public interface ITextRenderer
6+
{
7+
string ToString();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace QRCoder.Builders.Renderers.Implementations
2+
{
3+
public class AsciiRenderer : RendererBase, ITextRenderer
4+
{
5+
private string _darkString = "██";
6+
private string _lightString = " ";
7+
private int _repeatPerModule = 1;
8+
private string _endOfLine = System.Environment.NewLine;
9+
private bool _inverseDarkLight = false;
10+
11+
public AsciiRenderer WithText(string darkString, string lightString)
12+
{
13+
_darkString = darkString;
14+
_lightString = lightString;
15+
return this;
16+
}
17+
18+
public AsciiRenderer WithRepeatPerModule(int repeatPerModule)
19+
{
20+
_repeatPerModule = repeatPerModule;
21+
return this;
22+
}
23+
24+
public AsciiRenderer WithEndOfLine(string endOfLine)
25+
{
26+
_endOfLine = endOfLine;
27+
return this;
28+
}
29+
30+
public AsciiRenderer WithInverseDarkLight()
31+
{
32+
_inverseDarkLight = true;
33+
return this;
34+
}
35+
36+
public override string ToString()
37+
{
38+
return new AsciiQRCode(QrCodeData).GetGraphic(
39+
_repeatPerModule,
40+
_inverseDarkLight ? _lightString : _darkString,
41+
_inverseDarkLight ? _darkString : _lightString,
42+
QuietZone,
43+
_endOfLine);
44+
}
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.IO;
2+
using QRCoder.Builders.Payloads;
3+
4+
namespace QRCoder.Builders.Renderers.Implementations
5+
{
6+
public class PngRenderer : RendererBase, IConfigurablePixelsPerModule, IStreamRenderer
7+
{
8+
private int _pixelsPerModule = 10;
9+
private byte[] _darkColor;
10+
private byte[] _lightColor;
11+
12+
int IConfigurablePixelsPerModule.PixelsPerModule { get => _pixelsPerModule; set => _pixelsPerModule = value; }
13+
14+
#if !NETSTANDARD1_3
15+
public PngRenderer WithColors(System.Drawing.Color darkColor, System.Drawing.Color lightColor)
16+
{
17+
_darkColor = new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A };
18+
_lightColor = new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A };
19+
return this;
20+
}
21+
#endif
22+
23+
public PngRenderer WithColors(byte[] darkColor, byte[] lightColor)
24+
{
25+
_darkColor = darkColor;
26+
_lightColor = lightColor;
27+
return this;
28+
}
29+
30+
public byte[] ToArray()
31+
{
32+
if (_darkColor == null && _lightColor == null)
33+
return new PngByteQRCode(QrCodeData).GetGraphic(_pixelsPerModule, QuietZone);
34+
return new PngByteQRCode(QrCodeData).GetGraphic(_pixelsPerModule, _darkColor, _lightColor, QuietZone);
35+
}
36+
37+
public MemoryStream ToStream()
38+
{
39+
var arr = ToArray();
40+
return new MemoryStream(arr, 0, arr.Length, false, true);
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#if !NETSTANDARD1_3
2+
using System.Drawing;
3+
4+
namespace QRCoder.Builders.Renderers.Implementations
5+
{
6+
public class SvgRenderer : RendererBase, IConfigurablePixelsPerModule, ITextRenderer
7+
{
8+
private int _pixelsPerModule = 10;
9+
private Color _darkColor;
10+
private Color _lightColor;
11+
private SvgQRCode.SvgLogo _logo;
12+
private SvgQRCode.SizingMode _sizingMode = SvgQRCode.SizingMode.WidthHeightAttribute;
13+
14+
int IConfigurablePixelsPerModule.PixelsPerModule { get => _pixelsPerModule; set => _pixelsPerModule = value; }
15+
16+
public SvgRenderer WithColors(Color darkColor, Color lightColor)
17+
{
18+
_darkColor = darkColor;
19+
_lightColor = lightColor;
20+
return this;
21+
}
22+
23+
public SvgRenderer WithLogo(SvgQRCode.SvgLogo logo)
24+
{
25+
_logo = logo;
26+
return this;
27+
}
28+
29+
public SvgRenderer WithSizingMode(SvgQRCode.SizingMode sizingMode)
30+
{
31+
_sizingMode = sizingMode;
32+
return this;
33+
}
34+
35+
public override string ToString()
36+
{
37+
return new SvgQRCode(QrCodeData).GetGraphic(
38+
_pixelsPerModule, _darkColor, _lightColor, QuietZone, _sizingMode, _logo);
39+
}
40+
}
41+
}
42+
#endif

0 commit comments

Comments
 (0)