/
Program.cs
221 lines (199 loc) · 9.07 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
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Saraff.Twain;
namespace tryTwain
{
class Program
{
static float dpi = 150;
static bool isSilenced = false;
static TwPixelType pt = TwPixelType.Gray;
static void Main(string[] args)
{
isSilenced = !(indexOf(args, "-s") >= 0);
var d = indexOf(args, "-d");
if (d >= 0 && args.Length > d + 1) float.TryParse(args[d + 1], out dpi);
var p = indexOf(args, "-p");
if (p >= 0 && args.Length > p + 1)
{
string val = args[p+1].ToUpper();
if (val == "BW") pt = (TwPixelType)0;
if (val == "Gray") pt = (TwPixelType)1;
if (val == "RGB") pt = (TwPixelType)2;
if (val == "Palette") pt = (TwPixelType)3;
if (val == "CMY") pt = (TwPixelType)4;
if (val == "CMYK") pt = (TwPixelType)5;
if (val == "YUV") pt = (TwPixelType)6;
if (val == "YUVK") pt = (TwPixelType)7;
if (val == "CIEXYZ") pt = (TwPixelType)8;
if (val == "LAB") pt = (TwPixelType)9;
if (val == "SRGB") pt = (TwPixelType)10;
if (val == "SCRGB") pt = (TwPixelType)11;
if (val == "INFRARED") pt = (TwPixelType)16;
}
try
{
using (Twain32 twain = new Twain32())
{
var _asm = twain.GetType().Assembly;
WriteMessage( "{1} {2}{0}{3}{0}", Environment.NewLine,
((AssemblyTitleAttribute)_asm.GetCustomAttributes(typeof(AssemblyTitleAttribute), false)[0]).Title,
((AssemblyFileVersionAttribute)_asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)[0]).Version,
((AssemblyCopyrightAttribute)_asm.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright);
twain.ShowUI = false;
WriteMessage("ShowUI: {0}", twain.ShowUI ? "true" : "false");
WriteMessage("IsTwain2Enable {0}: ", twain.IsTwain2Enable ? "[Y/n]" : "[y/N]");
if (isSilenced)
for (var _res = Console.ReadLine().Trim().ToUpper(); !string.IsNullOrEmpty(_res);)
{
twain.IsTwain2Enable = _res == "Y";
break;
}
else
twain.IsTwain2Enable = false;
WriteMessage("IsTwain2Enable: {0}", twain.IsTwain2Enable ? "true" : "false");
twain.OpenDSM();
WriteMessage("Select Data Source:");
if (isSilenced)
for (var i1 = 0; i1 < twain.SourcesCount; i1++)
{
WriteMessage("{0}: {1}{2}", i1, twain.GetSourceProductName(i1), twain.IsTwain2Supported && twain.GetIsSourceTwain2Compatible(i1) ? " (TWAIN 2.x)" : string.Empty);
}
WriteMessage("[{0}]: ", twain.SourceIndex);
if (isSilenced)
for (var _res = Console.ReadLine().Trim(); !string.IsNullOrEmpty(_res);)
{
twain.SourceIndex = Convert.ToInt32(_res);
break;
}
else
twain.SourceIndex = 0;
WriteMessage(string.Format("Data Source: {0}", twain.GetSourceProductName(twain.SourceIndex)));
twain.OpenDataSource();
WriteMessage("Select Resolution:");
var _resolutions = twain.Capabilities.XResolution.Get();
if (isSilenced)
for (var i1 = 0; i1 < _resolutions.Count; i1++)
{
WriteMessage(true, "{0}: {1} dpi", i1, _resolutions[i1]);
}
WriteMessage("[{0}]: ", _resolutions.CurrentIndex);
if (isSilenced)
for (var _res = Console.ReadLine().Trim(); !string.IsNullOrEmpty(_res);)
{
var _val = (float)_resolutions[Convert.ToInt32(_res)];
twain.Capabilities.XResolution.Set(_val);
twain.Capabilities.YResolution.Set(_val);
break;
}
else
{
twain.Capabilities.XResolution.Set(dpi);
twain.Capabilities.YResolution.Set(dpi);
}
WriteMessage("Resolution: {0}", twain.Capabilities.XResolution.GetCurrent());
WriteMessage("Select Pixel Type:");
var _pixels = twain.Capabilities.PixelType.Get();
if (isSilenced)
for (var i1 = 0; i1 < _pixels.Count; i1++)
{
WriteMessage("{0}: {1}", i1, _pixels[i1]);
}
WriteMessage("[{0}]: ", _pixels.CurrentIndex);
if (isSilenced)
for (var _res = Console.ReadLine().Trim(); !string.IsNullOrEmpty(_res);)
{
var _val = (TwPixelType)_pixels[Convert.ToInt32(_res)];
twain.Capabilities.PixelType.Set(_val);
break;
}
else
{
twain.Capabilities.PixelType.Set(pt);
}
WriteMessage(string.Format("Pixel Type: {0}", twain.Capabilities.PixelType.GetCurrent()));
twain.EndXfer += (object sender, Twain32.EndXferEventArgs e) =>
{
try
{
var _file = Path.Combine("", Path.ChangeExtension(Path.GetFileName(Path.GetTempFileName()), ".jpg"));
e.Image.Save(_file, ImageFormat.Jpeg);
WriteMessage(true, "Saved in: {0}", _file);
e.Image.Dispose();
}
catch (Exception ex)
{
WriteMessage("{0}: {1}{2}{3}{2}", ex.GetType().Name, ex.Message, Environment.NewLine, ex.StackTrace);
}
};
twain.AcquireCompleted += (sender, e) =>
{
try
{
WriteMessage("Acquire Completed.");
}
catch (Exception ex)
{
Program.WriteException(ex);
}
};
twain.AcquireError += (object sender, Twain32.AcquireErrorEventArgs e) =>
{
try
{
WriteMessage("Acquire Error: ReturnCode = {0}; ConditionCode = {1};", e.Exception.ReturnCode, e.Exception.ConditionCode);
Program.WriteException(e.Exception);
}
catch (Exception ex)
{
Program.WriteException(ex);
}
};
twain.Acquire();
}
}
catch (Exception ex)
{
WriteException(ex);
}
if (isSilenced)
{
Console.WriteLine("{0}{1}", Environment.NewLine, "Press any key to exit...");
Console.ReadKey();
}
}
private static void WriteException(Exception ex)
{
for (var _ex = ex; _ex != null; _ex = _ex.InnerException)
{
WriteMessage("{0}: {1}{2}{3}{2}", _ex.GetType().Name, _ex.Message, Environment.NewLine, _ex.StackTrace);
}
}
private static void WriteMessage(string message)
{
if (isSilenced) Console.WriteLine(message);
}
private static void WriteMessage(string message, params Object[] args)
{
if (isSilenced) Console.WriteLine(message, args);
}
private static void WriteMessage(bool forsed, string message, params Object[] args)
{
if (isSilenced || forsed) Console.WriteLine(message, args);
}
private static void WriteMessage(bool forsed, string message)
{
if (isSilenced || forsed) Console.WriteLine(message);
}
private static int indexOf(string[] args, string argKey)
{
return args.ToList().IndexOf(argKey);
}
}
}