-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
1,297 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Payloads | ||
{ | ||
public interface IConfigurableEccLevel | ||
{ | ||
QRCodeGenerator.ECCLevel EccLevel { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Payloads | ||
{ | ||
public interface IConfigurableEciMode | ||
{ | ||
QRCodeGenerator.EciMode EciMode { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Payloads | ||
{ | ||
public interface IConfigurableVersion | ||
{ | ||
int Version { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Payloads | ||
{ | ||
public interface IPayload | ||
{ | ||
QRCodeData ToMatrix(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
|
||
namespace QRCoder.Builders.Payloads.Implementations | ||
{ | ||
public class EmailPayload : PayloadBase | ||
{ | ||
public EmailPayload(string address) | ||
{ | ||
_address = address; | ||
} | ||
|
||
private string _address { get; set; } | ||
private string _subject { get; set; } | ||
private string _body { get; set; } | ||
private PayloadGenerator.Mail.MailEncoding _encoding { get; set; } = PayloadGenerator.Mail.MailEncoding.MAILTO; | ||
|
||
public EmailPayload WithSubject(string subject) | ||
{ | ||
_subject = subject; | ||
return this; | ||
} | ||
|
||
public EmailPayload WithBody(string body) | ||
{ | ||
_body = body; | ||
return this; | ||
} | ||
|
||
public EmailPayload WithEncoding(PayloadGenerator.Mail.MailEncoding encoding) | ||
{ | ||
if (encoding != PayloadGenerator.Mail.MailEncoding.MAILTO && encoding != PayloadGenerator.Mail.MailEncoding.SMTP && encoding != PayloadGenerator.Mail.MailEncoding.MATMSG) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(encoding)); | ||
} | ||
_encoding = encoding; | ||
return this; | ||
} | ||
|
||
protected override string Value => new PayloadGenerator.Mail(_address, _subject, _body, _encoding).ToString(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
QRCoder/Builders/Payloads/Implementations/StringPayload.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace QRCoder.Builders.Payloads.Implementations | ||
{ | ||
public class StringPayload : PayloadBase | ||
{ | ||
private string _data; | ||
|
||
public StringPayload(string data) | ||
{ | ||
_data = data; | ||
} | ||
|
||
protected override string Value => _data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace QRCoder.Builders.Payloads.Implementations | ||
{ | ||
public class WiFiPayload : PayloadBase | ||
{ | ||
private string _ssid { get; set; } | ||
private string _password { get; set; } | ||
private PayloadGenerator.WiFi.Authentication _authentication { get; set; } | ||
private bool _isHiddenSSID { get; set; } | ||
private bool _isHexStrings { get; set; } | ||
|
||
public WiFiPayload(string ssid) | ||
{ | ||
_ssid = ssid; | ||
_password = ""; | ||
_authentication = PayloadGenerator.WiFi.Authentication.nopass; | ||
} | ||
|
||
public WiFiPayload(string ssid, string password, PayloadGenerator.WiFi.Authentication authentication) | ||
{ | ||
_ssid = ssid; | ||
_password = password; | ||
_authentication = authentication; | ||
} | ||
|
||
public WiFiPayload WithHiddenSSID() | ||
{ | ||
_isHiddenSSID = true; | ||
return this; | ||
} | ||
|
||
public WiFiPayload WithHexStrings() | ||
{ | ||
_isHexStrings = true; | ||
return this; | ||
} | ||
|
||
protected override string Value | ||
{ | ||
get | ||
{ | ||
var wifi = new PayloadGenerator.WiFi(_ssid, _password, _authentication, _isHiddenSSID, _isHexStrings); | ||
return wifi.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace QRCoder.Builders.Payloads | ||
{ | ||
public abstract class PayloadBase : IPayload, IConfigurableEccLevel, IConfigurableEciMode, IConfigurableVersion | ||
{ | ||
protected virtual QRCodeGenerator.ECCLevel EccLevel { get; set; } = QRCodeGenerator.ECCLevel.Default; | ||
QRCodeGenerator.ECCLevel IConfigurableEccLevel.EccLevel { get => EccLevel; set => EccLevel = value; } | ||
|
||
protected virtual QRCodeGenerator.EciMode EciMode { get; set; } = QRCodeGenerator.EciMode.Default; | ||
QRCodeGenerator.EciMode IConfigurableEciMode.EciMode { get => EciMode; set => EciMode = value; } | ||
|
||
protected virtual int Version { get; set; } = -1; | ||
int IConfigurableVersion.Version { get => Version; set => Version = value; } | ||
|
||
protected abstract string Value { get; } | ||
|
||
public virtual QRCodeData ToMatrix() | ||
{ | ||
return QRCodeGenerator.GenerateQrCode(Value, EccLevel, false, false, EciMode, Version); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using QRCoder.Builders.Payloads; | ||
using QRCoder.Builders.Renderers; | ||
using QRCoder.Builders.Renderers.Implementations; | ||
|
||
namespace QRCoder | ||
{ | ||
public static class PayloadExtensions | ||
{ | ||
public static T WithErrorCorrection<T>(this T payload, QRCodeGenerator.ECCLevel eccLevel) | ||
where T : IConfigurableEccLevel | ||
{ | ||
payload.EccLevel = eccLevel; | ||
return payload; | ||
} | ||
|
||
public static T WithEciMode<T>(this T payload, QRCodeGenerator.EciMode eciMode) | ||
where T : IConfigurableEciMode | ||
{ | ||
payload.EciMode = eciMode; | ||
return payload; | ||
} | ||
|
||
public static T WithVersion<T>(this T payload, int version) | ||
where T : IConfigurableVersion | ||
{ | ||
payload.Version = version; | ||
return payload; | ||
} | ||
|
||
public static T RenderWith<T>(this IPayload payload) | ||
where T : IRenderer, new() | ||
{ | ||
var renderer = new T(); | ||
renderer.Payload = payload; | ||
return renderer; | ||
} | ||
|
||
public static T RenderWith<T>(this IPayload payload, int pixelsPerModule) | ||
where T : IRenderer, IConfigurablePixelsPerModule, new() | ||
{ | ||
var renderer = new T(); | ||
renderer.Payload = payload; | ||
renderer.PixelsPerModule = pixelsPerModule; | ||
return renderer; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Renderers | ||
{ | ||
public interface IConfigurablePixelsPerModule | ||
{ | ||
int PixelsPerModule { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace QRCoder.Builders.Renderers | ||
{ | ||
public interface IConfigurableQuietZones | ||
{ | ||
bool QuietZone { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using QRCoder.Builders.Payloads; | ||
|
||
namespace QRCoder.Builders.Renderers | ||
{ | ||
public interface IRenderer | ||
{ | ||
IPayload Payload { set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.IO; | ||
|
||
namespace QRCoder.Builders.Renderers | ||
{ | ||
public interface IStreamRenderer | ||
{ | ||
MemoryStream ToStream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.IO; | ||
|
||
namespace QRCoder.Builders.Renderers | ||
{ | ||
public interface ITextRenderer | ||
{ | ||
string ToString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
QRCoder/Builders/Renderers/Implementations/AsciiRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace QRCoder.Builders.Renderers.Implementations | ||
{ | ||
public class AsciiRenderer : RendererBase, ITextRenderer | ||
{ | ||
private string _darkString = "██"; | ||
private string _lightString = " "; | ||
private int _repeatPerModule = 1; | ||
private string _endOfLine = System.Environment.NewLine; | ||
private bool _inverseDarkLight = false; | ||
|
||
public AsciiRenderer WithText(string darkString, string lightString) | ||
{ | ||
_darkString = darkString; | ||
_lightString = lightString; | ||
return this; | ||
} | ||
|
||
public AsciiRenderer WithRepeatPerModule(int repeatPerModule) | ||
{ | ||
_repeatPerModule = repeatPerModule; | ||
return this; | ||
} | ||
|
||
public AsciiRenderer WithEndOfLine(string endOfLine) | ||
{ | ||
_endOfLine = endOfLine; | ||
return this; | ||
} | ||
|
||
public AsciiRenderer WithInverseDarkLight() | ||
{ | ||
_inverseDarkLight = true; | ||
return this; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return new AsciiQRCode(QrCodeData).GetGraphic( | ||
_repeatPerModule, | ||
_inverseDarkLight ? _lightString : _darkString, | ||
_inverseDarkLight ? _darkString : _lightString, | ||
QuietZone, | ||
_endOfLine); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.IO; | ||
using QRCoder.Builders.Payloads; | ||
|
||
namespace QRCoder.Builders.Renderers.Implementations | ||
{ | ||
public class PngRenderer : RendererBase, IConfigurablePixelsPerModule, IStreamRenderer | ||
{ | ||
private int _pixelsPerModule = 10; | ||
private byte[] _darkColor; | ||
private byte[] _lightColor; | ||
|
||
int IConfigurablePixelsPerModule.PixelsPerModule { get => _pixelsPerModule; set => _pixelsPerModule = value; } | ||
|
||
#if !NETSTANDARD1_3 | ||
public PngRenderer WithColors(System.Drawing.Color darkColor, System.Drawing.Color lightColor) | ||
{ | ||
_darkColor = new byte[] { darkColor.R, darkColor.G, darkColor.B, darkColor.A }; | ||
_lightColor = new byte[] { lightColor.R, lightColor.G, lightColor.B, lightColor.A }; | ||
return this; | ||
} | ||
#endif | ||
|
||
public PngRenderer WithColors(byte[] darkColor, byte[] lightColor) | ||
{ | ||
_darkColor = darkColor; | ||
_lightColor = lightColor; | ||
return this; | ||
} | ||
|
||
public byte[] ToArray() | ||
{ | ||
if (_darkColor == null && _lightColor == null) | ||
return new PngByteQRCode(QrCodeData).GetGraphic(_pixelsPerModule, QuietZone); | ||
return new PngByteQRCode(QrCodeData).GetGraphic(_pixelsPerModule, _darkColor, _lightColor, QuietZone); | ||
} | ||
|
||
public MemoryStream ToStream() | ||
{ | ||
var arr = ToArray(); | ||
return new MemoryStream(arr, 0, arr.Length, false, true); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#if !NETSTANDARD1_3 | ||
using System.Drawing; | ||
|
||
namespace QRCoder.Builders.Renderers.Implementations | ||
{ | ||
public class SvgRenderer : RendererBase, IConfigurablePixelsPerModule, ITextRenderer | ||
{ | ||
private int _pixelsPerModule = 10; | ||
private Color _darkColor; | ||
private Color _lightColor; | ||
private SvgQRCode.SvgLogo _logo; | ||
private SvgQRCode.SizingMode _sizingMode = SvgQRCode.SizingMode.WidthHeightAttribute; | ||
|
||
int IConfigurablePixelsPerModule.PixelsPerModule { get => _pixelsPerModule; set => _pixelsPerModule = value; } | ||
|
||
public SvgRenderer WithColors(Color darkColor, Color lightColor) | ||
{ | ||
_darkColor = darkColor; | ||
_lightColor = lightColor; | ||
return this; | ||
} | ||
|
||
public SvgRenderer WithLogo(SvgQRCode.SvgLogo logo) | ||
{ | ||
_logo = logo; | ||
return this; | ||
} | ||
|
||
public SvgRenderer WithSizingMode(SvgQRCode.SizingMode sizingMode) | ||
{ | ||
_sizingMode = sizingMode; | ||
return this; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return new SvgQRCode(QrCodeData).GetGraphic( | ||
_pixelsPerModule, _darkColor, _lightColor, QuietZone, _sizingMode, _logo); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.