Skip to content

Commit

Permalink
Cleaned up initial project, changed a few public fields to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Stipps authored and Fredrik Stipps committed May 8, 2016
1 parent 349b37d commit 805cda0
Show file tree
Hide file tree
Showing 8 changed files with 478 additions and 461 deletions.
4 changes: 2 additions & 2 deletions QRCoder/AbstractQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{
public abstract class AbstractQRCode<T>
{
protected QRCodeData qrCodeData;
protected QRCodeData QrCodeData { get; set; }

protected AbstractQRCode(QRCodeData data) {
qrCodeData = data;
this.QrCodeData = data;
}

public abstract T GetGraphic(int pixelsPerModule);
Expand Down
93 changes: 46 additions & 47 deletions QRCoder/PayloadGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QRCoder
namespace QRCoder
{
public static class PayloadGenerator
{

public class WiFi
{
private string SSID, password, authenticationMode;
private bool isHiddenSSID;
public WiFi(string SSID, string password, Authentication authenticationMode, bool isHiddenSSID = false)
private readonly string ssid, password, authenticationMode;
private readonly bool isHiddenSsid;
public WiFi(string ssid, string password, Authentication authenticationMode, bool isHiddenSSID = false)
{
this.SSID = escapeInput(SSID);
this.SSID = isHexStyle(this.SSID) ? "\"" + this.SSID + "\"" : this.SSID;
this.password = escapeInput(password);
this.ssid = EscapeInput(ssid);
this.ssid = isHexStyle(this.ssid) ? "\"" + this.ssid + "\"" : this.ssid;
this.password = EscapeInput(password);
this.password = isHexStyle(this.password) ? "\"" + this.password + "\"" : this.password;
this.authenticationMode = authenticationMode.ToString();
this.isHiddenSSID = isHiddenSSID;
this.isHiddenSsid = isHiddenSSID;
}

public override string ToString()
{
return String.Format("WIFI:T:{0};S:{1};P:{2};{3};", authenticationMode, SSID, password, (isHiddenSSID ? "H:true" : string.Empty));
return
$"WIFI:T:{this.authenticationMode};S:{this.ssid};P:{this.password};{(this.isHiddenSsid ? "H:true" : string.Empty)};";
}

public enum Authentication
Expand All @@ -38,8 +34,8 @@ public enum Authentication

public class Mail
{
private string mailReceiver, subject, message;
private MailEncoding encoding;
private readonly string mailReceiver, subject, message;
private readonly MailEncoding encoding;

public Mail(string mailReceiver, MailEncoding encoding = MailEncoding.MAILTO)
{
Expand Down Expand Up @@ -67,16 +63,19 @@ public Mail(string mailReceiver, string subject, string message, MailEncoding en

public override string ToString()
{
switch (encoding)
switch (this.encoding)
{
case MailEncoding.MAILTO:
return String.Format("mailto:{0}?subject={1}&body={2}", mailReceiver, System.Uri.EscapeDataString(subject), System.Uri.EscapeDataString(message));
return
$"mailto:{this.mailReceiver}?subject={System.Uri.EscapeDataString(this.subject)}&body={System.Uri.EscapeDataString(this.message)}";
case MailEncoding.MATMSG:
return String.Format("MATMSG:TO:{0};SUB:{1};BODY:{2};;", mailReceiver, escapeInput(subject), escapeInput(message));
return
$"MATMSG:TO:{this.mailReceiver};SUB:{EscapeInput(this.subject)};BODY:{EscapeInput(this.message)};;";
case MailEncoding.SMTP:
return String.Format("SMTP:{0}:{1}:{2}", mailReceiver, escapeInput(subject, true), escapeInput(message, true));
return
$"SMTP:{this.mailReceiver}:{EscapeInput(this.subject, true)}:{EscapeInput(this.message, true)}";
default:
return mailReceiver;
return this.mailReceiver;
}
}

Expand All @@ -90,8 +89,8 @@ public enum MailEncoding

public class SMS
{
private string number, subject;
private SMSEncoding encoding;
private readonly string number, subject;
private readonly SMSEncoding encoding;

public SMS(string number, SMSEncoding encoding = SMSEncoding.SMS)
{
Expand All @@ -110,14 +109,14 @@ public SMS(string number, string subject, SMSEncoding encoding = SMSEncoding.SMS

public override string ToString()
{
switch (encoding)
switch (this.encoding)
{
case SMSEncoding.SMS:
return String.Format("sms:{0}?body={1}", number, System.Uri.EscapeDataString(subject));
return $"sms:{this.number}?body={System.Uri.EscapeDataString(this.subject)}";
case SMSEncoding.SMS_iOS:
return String.Format("sms:{0};body={1}", number, System.Uri.EscapeDataString(subject));
return $"sms:{this.number};body={System.Uri.EscapeDataString(this.subject)}";
case SMSEncoding.SMSTO:
return String.Format("SMSTO:{0}:{1}", number, subject);
return $"SMSTO:{this.number}:{this.subject}";
default:
return "sms:";
}
Expand All @@ -133,8 +132,8 @@ public enum SMSEncoding

public class MMS
{
private string number, subject;
private MMSEncoding encoding;
private readonly string number, subject;
private readonly MMSEncoding encoding;

public MMS(string number, MMSEncoding encoding = MMSEncoding.MMS)
{
Expand All @@ -152,12 +151,12 @@ public MMS(string number, string subject, MMSEncoding encoding = MMSEncoding.MMS

public override string ToString()
{
switch (encoding)
switch (this.encoding)
{
case MMSEncoding.MMSTO:
return String.Format("mmsto:{0}?subject={1}", number, System.Uri.EscapeDataString(subject));
return $"mmsto:{this.number}?subject={System.Uri.EscapeDataString(this.subject)}";
case MMSEncoding.MMS:
return String.Format("mms:{0}?body={1}", number, System.Uri.EscapeDataString(subject));
return $"mms:{this.number}?body={System.Uri.EscapeDataString(this.subject)}";
default:
return "mms:";
}
Expand All @@ -174,8 +173,8 @@ public enum MMSEncoding

public class Geolocation
{
private string latitude, longitude;
private GeolocationEncoding encoding;
private readonly string latitude, longitude;
private readonly GeolocationEncoding encoding;
public Geolocation(string latitude, string longitude, GeolocationEncoding encoding = GeolocationEncoding.GEO)
{
this.latitude = latitude.Replace(",",".");
Expand All @@ -185,12 +184,12 @@ public Geolocation(string latitude, string longitude, GeolocationEncoding encodi

public override string ToString()
{
switch (encoding)
switch (this.encoding)
{
case GeolocationEncoding.GEO:
return String.Format("geo:{0},{1}", latitude, longitude);
return $"geo:{this.latitude},{this.longitude}";
case GeolocationEncoding.GoogleMaps:
return String.Format("http://maps.google.com/maps?q={0},{1}", latitude, longitude);
return $"http://maps.google.com/maps?q={this.latitude},{this.longitude}";
default:
return "geo:";
}
Expand All @@ -205,52 +204,52 @@ public enum GeolocationEncoding

public class PhoneNumber
{
private string number;
private readonly string number;
public PhoneNumber(string number)
{
this.number = number;
}

public override string ToString()
{
return "tel:"+number;
return "tel:"+ this.number;
}
}

public class Url
{
private string url;
private readonly string url;
public Url(string url)
{
this.url = url;
}

public override string ToString()
{
return (!url.StartsWith("http") ? "http://" + url : url);
return (!this.url.StartsWith("http") ? "http://" + this.url : this.url);
}
}

public class Bookmark
{
private string url, title;
private readonly string url, title;
public Bookmark(string url, string title)
{
this.url = escapeInput(url);
this.title = escapeInput(title);
this.url = EscapeInput(url);
this.title = EscapeInput(title);
}

public override string ToString()
{
return String.Format("MEBKM:TITLE:{0};URL:{1};;", title, url);
return $"MEBKM:TITLE:{this.title};URL:{this.url};;";
}
}

private static string escapeInput(string inp, bool simple = false)
private static string EscapeInput(string inp, bool simple = false)
{
char[] forbiddenChars = { '\\', ';', ',', ':' };
if (simple) { forbiddenChars = new char[1] { ':' }; }
foreach (char c in forbiddenChars)
foreach (var c in forbiddenChars)
{
inp = inp.Replace(c.ToString(), "\\" + c);
}
Expand Down
52 changes: 26 additions & 26 deletions QRCoder/QRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ public QRCode(QRCodeData data) : base(data) {}

public override Bitmap GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
return this.GetGraphic(pixelsPerModule, Color.Black, Color.White, true);
}

public Bitmap GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true)
{
return GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), true);
return this.GetGraphic(pixelsPerModule, ColorTranslator.FromHtml(darkColorHtmlHex), ColorTranslator.FromHtml(lightColorHtmlHex), true);
}

public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true)
{
var size = (qrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
int offset = drawQuietZones ? 0 : 4 * pixelsPerModule;
var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
var offset = drawQuietZones ? 0 : 4 * pixelsPerModule;

Bitmap bmp = new Bitmap(size, size);
Graphics gfx = Graphics.FromImage(bmp);
for (int x = 0; x < size + offset; x = x + pixelsPerModule)
var bmp = new Bitmap(size, size);
var gfx = Graphics.FromImage(bmp);
for (var x = 0; x < size + offset; x = x + pixelsPerModule)
{
for (int y = 0; y < size + offset; y = y + pixelsPerModule)
for (var y = 0; y < size + offset; y = y + pixelsPerModule)
{
var module = qrCodeData.ModuleMatrix[(y + pixelsPerModule)/pixelsPerModule - 1][(x + pixelsPerModule)/pixelsPerModule - 1];
var module = this.QrCodeData.ModuleMatrix[(y + pixelsPerModule)/pixelsPerModule - 1][(x + pixelsPerModule)/pixelsPerModule - 1];
if (module)
{
gfx.FillRectangle(new SolidBrush(darkColor), new Rectangle(x - offset, y - offset, pixelsPerModule, pixelsPerModule));
Expand All @@ -48,16 +48,16 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,

public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Bitmap icon=null, int iconSizePercent=15, int iconBorderWidth = 6, bool drawQuietZones = true)
{
var size = (qrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
int offset = drawQuietZones ? 0 : 4 * pixelsPerModule;
var size = (this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8)) * pixelsPerModule;
var offset = drawQuietZones ? 0 : 4 * pixelsPerModule;

Bitmap bmp = new Bitmap(size, size, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics gfx = Graphics.FromImage(bmp);
var bmp = new Bitmap(size, size, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
var gfx = Graphics.FromImage(bmp);
gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
gfx.CompositingQuality = CompositingQuality.HighQuality;
gfx.Clear(lightColor);

bool drawIconFlag = icon != null && iconSizePercent>0 && iconSizePercent<=100;
var drawIconFlag = icon != null && iconSizePercent>0 && iconSizePercent<=100;

GraphicsPath iconPath = null;
float iconDestWidth=0, iconDestHeight=0, iconX=0, iconY=0;
Expand All @@ -69,27 +69,27 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,
iconX = (bmp.Width - iconDestWidth) / 2;
iconY = (bmp.Height - iconDestHeight) / 2;

RectangleF centerDest = new RectangleF(iconX - iconBorderWidth, iconY - iconBorderWidth, iconDestWidth + iconBorderWidth * 2, iconDestHeight + iconBorderWidth * 2);
iconPath = CreateRoundedRectanglePath(centerDest, iconBorderWidth * 2);
var centerDest = new RectangleF(iconX - iconBorderWidth, iconY - iconBorderWidth, iconDestWidth + iconBorderWidth * 2, iconDestHeight + iconBorderWidth * 2);
iconPath = this.CreateRoundedRectanglePath(centerDest, iconBorderWidth * 2);
}

SolidBrush lightBrush = new SolidBrush(lightColor);
SolidBrush darkBrush = new SolidBrush(darkColor);
var lightBrush = new SolidBrush(lightColor);
var darkBrush = new SolidBrush(darkColor);


for (int x = 0; x < size+offset; x = x + pixelsPerModule)
for (var x = 0; x < size+offset; x = x + pixelsPerModule)
{
for (int y = 0; y < size + offset; y = y + pixelsPerModule)
for (var y = 0; y < size + offset; y = y + pixelsPerModule)
{

var module = qrCodeData.ModuleMatrix[(y + pixelsPerModule)/pixelsPerModule - 1][(x + pixelsPerModule)/pixelsPerModule - 1];
var module = this.QrCodeData.ModuleMatrix[(y + pixelsPerModule)/pixelsPerModule - 1][(x + pixelsPerModule)/pixelsPerModule - 1];
if (module)
{
Rectangle r = new Rectangle(x-offset, y-offset, pixelsPerModule, pixelsPerModule);
var r = new Rectangle(x-offset, y-offset, pixelsPerModule, pixelsPerModule);

if (drawIconFlag)
{
Region region = new Region(r);
var region = new Region(r);
region.Exclude(iconPath);
gfx.FillRegion(darkBrush, region);
}
Expand All @@ -106,7 +106,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,

if (drawIconFlag)
{
RectangleF iconDestRect = new RectangleF(iconX, iconY, iconDestWidth, iconDestHeight);
var iconDestRect = new RectangleF(iconX, iconY, iconDestWidth, iconDestHeight);
gfx.DrawImage(icon, iconDestRect, new RectangleF(0, 0, icon.Width, icon.Height), GraphicsUnit.Pixel);
}

Expand All @@ -116,7 +116,7 @@ public Bitmap GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor,

internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadius)
{
GraphicsPath roundedRect = new GraphicsPath();
var roundedRect = new GraphicsPath();
roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
Expand All @@ -131,7 +131,7 @@ internal GraphicsPath CreateRoundedRectanglePath(RectangleF rect, int cornerRadi

public void Dispose()
{
this.qrCodeData = null;
this.QrCodeData = null;
}
}
}
19 changes: 7 additions & 12 deletions QRCoder/QRCodeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,27 @@ namespace QRCoder
public class QRCodeData : IDisposable
{
public List<BitArray> ModuleMatrix { get; set; }
private int _version;

public QRCodeData(int version)
{
this._version = version;
this.Version = version;
var size = ModulesPerSideFromVersion(version);
ModuleMatrix = new List<BitArray>();
for (int i = 0; i < size; i++)
ModuleMatrix.Add(new BitArray(size));
this.ModuleMatrix = new List<BitArray>();
for (var i = 0; i < size; i++)
this.ModuleMatrix.Add(new BitArray(size));
}

public int version {
get {
return _version;
}
}
public int Version { get; private set; }

private int ModulesPerSideFromVersion(int version)
private static int ModulesPerSideFromVersion(int version)
{
return 21 + (version - 1) * 4;
}

public void Dispose()
{
this.ModuleMatrix = null;
this._version = 0;
this.Version = 0;

}
}
Expand Down
Loading

0 comments on commit 805cda0

Please sign in to comment.