From 83de37ddec0912632cfd8d59facd53e46807ca63 Mon Sep 17 00:00:00 2001 From: mishfit Date: Sat, 14 May 2016 00:21:03 -0600 Subject: [PATCH] add a console demo --- .gitignore | 2 +- QRCoder/QRCoder.csproj | 2 - QRCoderConsole/Program.cs | 111 ++++++++++++++++++++++ QRCoderConsole/Properties/AssemblyInfo.cs | 27 ++++++ QRCoderConsole/QRCoderConsole.csproj | 52 ++++++++++ QRCoderConsole/packages.config | 4 + QRCoderDemo/QRCoderDemo.csproj | 2 - QRCoderProject.sln | 16 +++- 8 files changed, 210 insertions(+), 6 deletions(-) create mode 100644 QRCoderConsole/Program.cs create mode 100644 QRCoderConsole/Properties/AssemblyInfo.cs create mode 100644 QRCoderConsole/QRCoderConsole.csproj create mode 100644 QRCoderConsole/packages.config diff --git a/.gitignore b/.gitignore index b9d6bd92..4ead7663 100644 --- a/.gitignore +++ b/.gitignore @@ -130,7 +130,7 @@ publish/ # NuGet Packages Directory ## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ +packages/ # Windows Azure Build Output csx diff --git a/QRCoder/QRCoder.csproj b/QRCoder/QRCoder.csproj index 60e259b4..d38bfebe 100644 --- a/QRCoder/QRCoder.csproj +++ b/QRCoder/QRCoder.csproj @@ -3,8 +3,6 @@ Debug AnyCPU - 8.0.30703 - 2.0 {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD} Library Properties diff --git a/QRCoderConsole/Program.cs b/QRCoderConsole/Program.cs new file mode 100644 index 00000000..1b9b42d1 --- /dev/null +++ b/QRCoderConsole/Program.cs @@ -0,0 +1,111 @@ +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using NDesk.Options; +using System.Reflection; +using QRCoder; +using System.Text; + +namespace QRCoderConsole +{ + class MainClass + { + public static void Main (string[] args) + { + var friendlyName = AppDomain.CurrentDomain.FriendlyName; + var newLine = Environment.NewLine; + var setter = new OptionSetter (); + + String fileName = null, outputFileName = null; + + QRCodeGenerator.ECCLevel eccLevel = QRCodeGenerator.ECCLevel.L; + + var showHelp = false; + + + var optionSet = new OptionSet { + { "e|ecc-level=", + "error correction level", + value => eccLevel = setter.GetECCLevel(value) + }, + { + "i|in=", + "input file", + value => fileName = value + }, + { + "o|out=", + "output file", + value => outputFileName = value + }, + { "h|help", + "show this message and exit.", + value => showHelp = value != null + } + }; + + try + { + + var settings = optionSet.Parse(args); + + if (showHelp) { + optionSet.WriteOptionDescriptions(Console.Out); + Environment.Exit(0); + } + + var fileInfo = new FileInfo(fileName); + + if (fileInfo.Exists) + { + var buffer = new byte[fileInfo.Length]; + + using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open)) + { + fileStream.Read(buffer, 0, buffer.Length); + } + + var text = Encoding.UTF8.GetString(buffer); + + using (var generator = new QRCodeGenerator()) + { + using (var data = generator.CreateQrCode(text, eccLevel)) + { + using (var code = new QRCode(data)) + { + using (var bitmap = code.GetGraphic(20)) + { + bitmap.Save(outputFileName, ImageFormat.Png); + } + } + } + } + + + } + else { + Console.WriteLine($"{friendlyName}: {fileName}: No such file or directory"); + } + } + catch (OptionException oe) { + Console.Error.WriteLine($"{friendlyName}:{newLine}{oe.Message}{newLine}Try '{friendlyName} --help' for more information"); + Environment.Exit (-1); + } + + } + } + + public class OptionSetter + { + public QRCodeGenerator.ECCLevel GetECCLevel(string value) + { + QRCodeGenerator.ECCLevel level; + + Enum.TryParse (value, out level); + + return level; + } + } + +} diff --git a/QRCoderConsole/Properties/AssemblyInfo.cs b/QRCoderConsole/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..84b3205f --- /dev/null +++ b/QRCoderConsole/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("QRCoderConsole")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("mishochu")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/QRCoderConsole/QRCoderConsole.csproj b/QRCoderConsole/QRCoderConsole.csproj new file mode 100644 index 00000000..11eb3d62 --- /dev/null +++ b/QRCoderConsole/QRCoderConsole.csproj @@ -0,0 +1,52 @@ + + + + Debug + AnyCPU + {014F04C6-6099-4552-9A4F-D09C6E39D576} + Exe + QRCoderConsole + QRCoderConsole + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + --in foo --out fooQR.png + + + full + true + bin\Release + prompt + 4 + true + + + + + ..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll + + + + + + + + + + + + + + {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD} + QRCoder + + + \ No newline at end of file diff --git a/QRCoderConsole/packages.config b/QRCoderConsole/packages.config new file mode 100644 index 00000000..da45c27f --- /dev/null +++ b/QRCoderConsole/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/QRCoderDemo/QRCoderDemo.csproj b/QRCoderDemo/QRCoderDemo.csproj index 6b3b7428..887761e0 100644 --- a/QRCoderDemo/QRCoderDemo.csproj +++ b/QRCoderDemo/QRCoderDemo.csproj @@ -3,8 +3,6 @@ Debug x86 - 8.0.30703 - 2.0 {CA6DB0F5-DB6C-4DDD-8B5F-EF82FBB963E9} WinExe Properties diff --git a/QRCoderProject.sln b/QRCoderProject.sln index 4f53322c..b15987ae 100644 --- a/QRCoderProject.sln +++ b/QRCoderProject.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 +# Visual Studio 2012 VisualStudioVersion = 12.0.21005.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoder", "QRCoder\QRCoder.csproj", "{AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}" @@ -10,6 +10,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderDemo", "QRCoderDemo\ {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD} = {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD} EndProjectSection EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderConsole", "QRCoderConsole\QRCoderConsole.csproj", "{014F04C6-6099-4552-9A4F-D09C6E39D576}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -20,6 +22,18 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|Any CPU.Build.0 = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|x86.ActiveCfg = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Debug|x86.Build.0 = Debug|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|Any CPU.ActiveCfg = Release|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|Any CPU.Build.0 = Release|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|x86.ActiveCfg = Release|Any CPU + {014F04C6-6099-4552-9A4F-D09C6E39D576}.Release|x86.Build.0 = Release|Any CPU {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}.Debug|Any CPU.Build.0 = Debug|Any CPU {AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU