Skip to content

Commit

Permalink
Added support for payload by parameter and image formats
Browse files Browse the repository at this point in the history
Added parameters p for payload and f for different output image formats.
  • Loading branch information
codebude committed May 14, 2016
1 parent e1d5fd0 commit a727b82
Showing 1 changed file with 72 additions and 31 deletions.
103 changes: 72 additions & 31 deletions QRCoderConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@ public static void Main (string[] args)
var newLine = Environment.NewLine;
var setter = new OptionSetter ();

String fileName = null, outputFileName = null;
String fileName = null, outputFileName = null, payload = null;

QRCodeGenerator.ECCLevel eccLevel = QRCodeGenerator.ECCLevel.L;
ImageFormat imageFormat = ImageFormat.Png;

var showHelp = false;

var showHelp = false;


var optionSet = new OptionSet {
{ "e|ecc-level=",
"error correction level",
value => eccLevel = setter.GetECCLevel(value)
},
{
{ "f|output-format=",
"Image format for outputfile. Possible values: png, jpg, gif, bmp, tiff (default: png)",
value => imageFormat = setter.GetImageFormat(value)
},
{
"i|in=",
"input file",
"input file | alternative to parameter -p",
value => fileName = value
},
{
{
"p|payload=",
"payload string | alternative to parameter -i",
value => payload = value
},
{
"o|out=",
"output file",
value => outputFileName = value
Expand All @@ -56,39 +67,31 @@ public static void Main (string[] args)
Environment.Exit(0);
}

var fileInfo = new FileInfo(fileName);

if (fileInfo.Exists)
if (fileName != null)
{
var buffer = new byte[fileInfo.Length];

using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open))
var fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
{
fileStream.Read(buffer, 0, buffer.Length);
}
var buffer = new byte[fileInfo.Length];

var text = Encoding.UTF8.GetString(buffer);

using (var generator = new QRCodeGenerator())
{
using (var data = generator.CreateQrCode(text, eccLevel))
using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open))
{
using (var code = new QRCode(data))
{
using (var bitmap = code.GetGraphic(20))
{
bitmap.Save(outputFileName, ImageFormat.Png);
}
}
fileStream.Read(buffer, 0, buffer.Length);
}
}

var text = Encoding.UTF8.GetString(buffer);

GenerateQRCode(text, eccLevel, outputFileName, imageFormat);
}
else
{
Console.WriteLine($"{friendlyName}: {fileName}: No such file or directory");
}
}
else
{
Console.WriteLine($"{friendlyName}: {fileName}: No such file or directory");
}
else if (payload != null)
{
GenerateQRCode(payload, eccLevel, outputFileName, imageFormat);
}
}
catch (Exception oe)
{
Expand All @@ -97,6 +100,23 @@ public static void Main (string[] args)
Environment.Exit(-1);
}
}

private static void GenerateQRCode(string payloadString, QRCodeGenerator.ECCLevel eccLevel, string outputFileName, ImageFormat imgFormat)

This comment has been minimized.

Copy link
@mishfit

mishfit May 14, 2016

Contributor

👍 I should've totally refactored this out of the argument parsing

{
using (var generator = new QRCodeGenerator())
{
using (var data = generator.CreateQrCode(payloadString, eccLevel))
{
using (var code = new QRCode(data))
{
using (var bitmap = code.GetGraphic(20))
{
bitmap.Save(outputFileName, imgFormat);
}
}
}
}
}
}

public class OptionSetter
Expand All @@ -109,6 +129,27 @@ public QRCodeGenerator.ECCLevel GetECCLevel(string value)

return level;
}
}

public ImageFormat GetImageFormat(string value)
{
switch (value.ToLower())
{
case "png":
return ImageFormat.Png;
case "jpg":
return ImageFormat.Jpeg;
case "jpeg":
return ImageFormat.Jpeg;
case "gif":
return ImageFormat.Gif;
case "bmp":
return ImageFormat.Bmp;
case "tiff":
return ImageFormat.Tiff;
default:
return ImageFormat.Png;
}
}
}

}

0 comments on commit a727b82

Please sign in to comment.