forked from codebude/QRCoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
251 lines (221 loc) · 7.94 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Markup;
using NDesk.Options;
using QRCoder;
using QRCoderConsole.DataObjects;
namespace QRCoderConsole;
#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
internal 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, payload = null;
var eccLevel = QRCodeGenerator.ECCLevel.L;
var imageFormat = SupportedImageFormat.Png;
int pixelsPerModule = 20;
string foregroundColor = "#000000";
string backgroundColor = "#FFFFFF";
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: {string.Join(", ", Enum.GetNames(typeof(SupportedImageFormat)))} (default: png)",
value => Enum.TryParse(value, true, out imageFormat)
},
{
"i|in=",
"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
},
{ "s|pixel=",
"pixels per module",
value => {
if (int.TryParse(value, out pixelsPerModule))
{
if (pixelsPerModule < 1)
{
pixelsPerModule = 20;
}
}
else
{
pixelsPerModule = 20;
}
}
},
{ "l|background=",
"background color",
value => backgroundColor = value
},
{ "d|foreground=",
"foreground color",
value => foregroundColor = value
},
{ "h|help",
"show this message and exit.",
value => showHelp = value != null
}
};
try
{
optionSet.Parse(args);
if (showHelp)
{
ShowHelp(optionSet);
}
string text = null;
if (fileName != null)
{
var fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
{
text = GetTextFromFile(fileInfo);
}
else
{
Console.WriteLine($"{friendlyName}: {fileName}: No such file or directory");
}
}
else if (payload != null)
{
text = payload;
}
else
{
var stdin = Console.OpenStandardInput();
text = GetTextFromStream(stdin);
}
if (text != null)
{
GenerateQRCode(text, eccLevel, outputFileName, imageFormat, pixelsPerModule, foregroundColor, backgroundColor);
}
}
catch (Exception oe)
{
Console.Error.WriteLine(
$"{friendlyName}:{newLine}{oe.GetType().FullName}{newLine}{oe.Message}{newLine}{oe.StackTrace}{newLine}Try '{friendlyName} --help' for more information");
Environment.Exit(-1);
}
}
private static void GenerateQRCode(string payloadString, QRCodeGenerator.ECCLevel eccLevel, string outputFileName, SupportedImageFormat imgFormat, int pixelsPerModule, string foreground, string background)
{
using var generator = new QRCodeGenerator();
using var data = generator.CreateQrCode(payloadString, eccLevel);
switch (imgFormat)
{
case SupportedImageFormat.Png:
case SupportedImageFormat.Jpg:
case SupportedImageFormat.Gif:
case SupportedImageFormat.Bmp:
case SupportedImageFormat.Tiff:
using (var code = new QRCode(data))
{
using var bitmap = code.GetGraphic(pixelsPerModule, foreground, background, true);
var actualFormat = new OptionSetter().GetImageFormat(imgFormat.ToString());
bitmap.Save(outputFileName, actualFormat);
}
break;
case SupportedImageFormat.Svg:
using (var code = new SvgQRCode(data))
{
var test = code.GetGraphic(pixelsPerModule, foreground, background, true);
using var f = File.CreateText(outputFileName);
f.Write(test);
f.Flush();
}
break;
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
case SupportedImageFormat.Xaml:
using (var code = new QRCoder.Xaml.XamlQRCode(data))
{
var test = XamlWriter.Save(code.GetGraphic(pixelsPerModule, foreground, background, true));
using var f = File.CreateText(outputFileName);
f.Write(test);
f.Flush();
}
break;
#endif
case SupportedImageFormat.Ps:
case SupportedImageFormat.Eps:
using (var code = new PostscriptQRCode(data))
{
var test = code.GetGraphic(pixelsPerModule, foreground, background, true,
imgFormat == SupportedImageFormat.Eps);
using var f = File.CreateText(outputFileName);
f.Write(test);
f.Flush();
}
break;
default:
throw new ArgumentOutOfRangeException(nameof(imgFormat), imgFormat, null);
}
}
private static string GetTextFromFile(FileInfo fileInfo)
{
var buffer = new byte[fileInfo.Length];
using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Open))
{
fileStream.Read(buffer, 0, buffer.Length);
}
return Encoding.UTF8.GetString(buffer);
}
private static string GetTextFromStream(Stream stream)
{
var buffer = new byte[256];
var bytesRead = 0;
using var memoryStream = new MemoryStream();
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
var text = Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine($"text retrieved from input stream: {text}");
return text;
}
private static void ShowHelp(OptionSet optionSet)
{
optionSet.WriteOptionDescriptions(Console.Out);
Environment.Exit(0);
}
}
public class OptionSetter
{
public QRCodeGenerator.ECCLevel GetECCLevel(string value)
{
Enum.TryParse(value, out QRCodeGenerator.ECCLevel level);
return level;
}
#if NET6_0_WINDOWS
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#endif
public ImageFormat GetImageFormat(string value) => value.ToLower() switch
{
"jpg" => ImageFormat.Jpeg,
"jpeg" => ImageFormat.Jpeg,
"gif" => ImageFormat.Gif,
"bmp" => ImageFormat.Bmp,
"tiff" => ImageFormat.Tiff,
_ => ImageFormat.Png,
};
}