Skip to content

Commit

Permalink
add a console demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mishfit committed May 14, 2016
1 parent 349b37d commit 83de37d
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions QRCoder/QRCoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
Expand Down
111 changes: 111 additions & 0 deletions QRCoderConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}

}
27 changes: 27 additions & 0 deletions QRCoderConsole/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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("")]

52 changes: 52 additions & 0 deletions QRCoderConsole/QRCoderConsole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{014F04C6-6099-4552-9A4F-D09C6E39D576}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>QRCoderConsole</RootNamespace>
<AssemblyName>QRCoderConsole</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<Commandlineparameters>--in foo --out fooQR.png</Commandlineparameters>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="NDesk.Options">
<HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QRCoder\QRCoder.csproj">
<Project>{AECE5DF3-C379-46CA-BB7D-7BDBE59011FD}</Project>
<Name>QRCoder</Name>
</ProjectReference>
</ItemGroup>
</Project>
4 changes: 4 additions & 0 deletions QRCoderConsole/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NDesk.Options" version="0.2.1" targetFramework="net45" />
</packages>
2 changes: 0 additions & 2 deletions QRCoderDemo/QRCoderDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CA6DB0F5-DB6C-4DDD-8B5F-EF82FBB963E9}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
Expand Down
16 changes: 15 additions & 1 deletion QRCoderProject.sln
Original file line number Diff line number Diff line change
@@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 83de37d

Please sign in to comment.