Skip to content

Commit

Permalink
More code/style tidying. Reduce cruft from stuff that should have bee…
Browse files Browse the repository at this point in the history
…n static, and un-nest classes that didn't need to be nested.
  • Loading branch information
PhonicUK committed May 19, 2020
1 parent e166eeb commit 25833fb
Show file tree
Hide file tree
Showing 10 changed files with 2,379 additions and 2,440 deletions.
18 changes: 4 additions & 14 deletions QRCoder/ASCIIQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public AsciiQRCode(QRCodeData data) : base(data) { }
/// </summary>
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
/// <returns></returns>
public string GetGraphic(int repeatPerModule)
{
return string.Join("\n", GetLineByLineGraphic(repeatPerModule));
}
public string GetGraphic(int repeatPerModule) => string.Join("\n", GetLineByLineGraphic(repeatPerModule));


/// <summary>
Expand All @@ -33,21 +30,15 @@ public string GetGraphic(int repeatPerModule)
/// <param name="whiteSpaceString">String for use as white modules (whitespace). In case of string make sure darkColorString has the same length.</param>
/// <param name="endOfLine">End of line separator. (Default: \n)</param>
/// <returns></returns>
public string GetGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString, string endOfLine = "\n")
{
return string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString));
}
public string GetGraphic(int repeatPerModule, string darkColorString, string whiteSpaceString, string endOfLine = "\n") => string.Join(endOfLine, GetLineByLineGraphic(repeatPerModule, darkColorString, whiteSpaceString));


/// <summary>
/// Returns an array of strings that contains each line of the resulting QR code as ASCII chars.
/// </summary>
/// <param name="repeatPerModule">Number of repeated darkColorString/whiteSpaceString per module.</param>
/// <returns></returns>
public string[] GetLineByLineGraphic(int repeatPerModule)
{
return GetLineByLineGraphic(repeatPerModule, "██", " ");
}
public string[] GetLineByLineGraphic(int repeatPerModule) => GetLineByLineGraphic(repeatPerModule, "██", " ");


/// <summary>
Expand Down Expand Up @@ -98,8 +89,7 @@ public static class AsciiQRCodeHelper
{
public static string GetQRCode(string plainText, int pixelsPerModule, string darkColorString, string whiteSpaceString, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, string endOfLine = "\n")
{
using (var qrGenerator = new QRCodeGenerator())
using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
using (var qrCodeData = CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion))
using (var qrCode = new AsciiQRCode(qrCodeData))
return qrCode.GetGraphic(pixelsPerModule, darkColorString, whiteSpaceString, endOfLine);
}
Expand Down
18 changes: 5 additions & 13 deletions QRCoder/BitmapByteQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@ public BitmapByteQRCode() { }

public BitmapByteQRCode(QRCodeData data) : base(data) { }

public byte[] GetGraphic(int pixelsPerModule)
{
return GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF });
}
public byte[] GetGraphic(int pixelsPerModule) => GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF });

public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex)
{
return GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));
}
public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex) => GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex));

public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightColorRgb)
{
Expand Down Expand Up @@ -105,18 +99,16 @@ public static byte[] GetQRCode(string plainText, int pixelsPerModule, string dar
string lightColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false,
EciMode eciMode = EciMode.Default, int requestedVersion = -1)
{
using (var qrGenerator = new QRCodeGenerator())
using (
var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode,
var qrCodeData = CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode,
requestedVersion))
using (var qrCode = new BitmapByteQRCode(qrCodeData))
return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex);
}

public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size)
public static byte[] GetQRCode(string txt, ECCLevel eccLevel, int size)
{
using (var qrGen = new QRCodeGenerator())
using (var qrCode = qrGen.CreateQrCode(txt, eccLevel))
using (var qrCode = CreateQrCode(txt, eccLevel))
using (var qrBmp = new BitmapByteQRCode(qrCode))
return qrBmp.GetGraphic(size);

Expand Down
Loading

0 comments on commit 25833fb

Please sign in to comment.