-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWIAScanner.cs
More file actions
464 lines (407 loc) · 18 KB
/
WIAScanner.cs
File metadata and controls
464 lines (407 loc) · 18 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Drawing;
using WIA;
namespace ServerScan
{
class WIAScanner
{
/*
* Many thanks to Miljenko Barbir for his source code which this class is widely based on.
* http://miljenkobarbir.com/using-a-scanner-without-dialogs-in-net/
* And to iCopy project and their source code that helped me to debug most problems.
* http://icopy.svn.sourceforge.net/viewvc/icopy/trunk/iCopy/Classes/Scanner.vb
*/
const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
const string wiaFormatJPG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
class WIA_DPS_DOCUMENT_HANDLING_SELECT
{
public const int FEEDER = 0x001;
public const int FLATBED = 0x002;
public const int DUPLEX = 0x004;
public const int FRONT_FIRST = 0x008;
public const int BACK_FIRST = 0x010;
public const int FRONT_ONLY = 0x020;
public const int BACK_ONLY = 0x040;
public const int NEXT_PAGE = 0x080;
public const int PREFEED = 0x100;
public const int AUTO_ADVANCE = 0x200;
}
class WIA_DPS_DOCUMENT_HANDLING_STATUS
{
public const int FEED_READY = 0x01;
public const int FLAT_READY = 0x02;
public const int DUP_READY = 0x04;
public const int FLAT_COVER_UP = 0x08;
public const int PATH_COVER_UP = 0x10;
public const int PAPER_JAM = 0x20;
}
class WIA_ERRORS
{
public const uint BASE_VAL_WIA_ERROR = 0x80210000;
public const uint WIA_ERROR_GENERAL_ERROR = BASE_VAL_WIA_ERROR + 1;
public const uint WIA_ERROR_PAPER_JAM = BASE_VAL_WIA_ERROR + 2;
public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
public const uint WIA_ERROR_BUSY = BASE_VAL_WIA_ERROR + 6;
}
class WIA_PROPERTIES
{
public const int WIA_RESERVED_FOR_NEW_PROPS = 1024;
public const int WIA_DIP_FIRST = 2;
public const int WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const int WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
// Scanner only device properties (DPS)
public const int WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
public const int WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + 13;
public const int WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + 14;
public const int WIA_DPS_PAGES = WIA_DPS_FIRST + 22;
}
/// <summary>
/// Use scanner to scan an image (with user selecting the scanner from a dialog).
/// </summary>
/// <returns>Scanned images.</returns>
public static List<Image> Scan(ScanSettings settings)
{
ICommonDialog dialog = new CommonDialog();
Device device = null;
try
{
device = dialog.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
}
catch (Exception)
{
throw new Exception("Cannot initialize scanner selection window. No WIA scanner installed?");
}
if (device != null)
{
return Acquire(device, settings);
}
else
{
throw new Exception("You must first select a WIA scanner.");
}
}
/// <summary>
/// Use scanner provided scanner id to scan an image.
/// </summary>
/// <returns>Scanned images.</returns>
public static List<Image> Scan(String scannerId, ScanSettings settings)
{
// select the correct scanner using the provided scannerId parameter
DeviceManager manager = new DeviceManager();
Device device = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == scannerId)
{
try
{ //Check connection to scanner
device = info.Connect();
}
catch (Exception)
{
throw new Exception("Cannot connect to scanner, please check your device and try again.");
}
break;
}
}
// device was not found
if (device == null)
{
throw new Exception("The provided scanner device could not be found.");
}
return Acquire(device, settings);
}
/// <summary>
/// Gets the list of available WIA devices.
/// </summary>
/// <returns></returns>
public static Hashtable GetDevices()
{
Hashtable devices = new Hashtable();
DeviceManager manager = null;
try
{
manager = new DeviceManager();
}
catch (Exception)
{
throw new Exception("Cannot initialize WIA device manager.\nMake sure wiaaut.dll is present in your system32 directory and that it is registered (run 'regsvr32 wiaaut.dll').");
}
foreach (DeviceInfo info in manager.DeviceInfos)
{
String name = info.Properties["Name"].get_Value().ToString();
devices.Add(info.DeviceID, name);
}
return devices;
}
/*===================
* Scanning routines
*===================*/
private static void SetDeviceHandling(ref Device device, ScanSettings settings)
{
Logger.Log("Setup device handling");
try
{ //Setup ADF vs Flatbed
if (settings.adf)
SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);
else
SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FLATBED);
}
catch (Exception ex)
{
Logger.Log("Cannot configure scanner mode properly\n " + ex);
}
}
private static void SetDeviceProperties(ref Device device, ScanSettings settings)
{
Logger.Log("Setup device properties");
// configure scanning properties
Item scan = device.Items[1] as Item;
foreach (Property prop in scan.Properties)
{
switch (prop.PropertyID)
{
case 6146: //4 is Black-white, gray is 2, color 1
SetProperty(prop, settings.color);
break;
case 6147: //dots per inch/horizontal
SetProperty(prop, settings.dpi);
break;
case 6148: //dots per inch/vertical
SetProperty(prop, settings.dpi);
break;
case 6149: //x point where to start scan
SetProperty(prop, 0);
break;
case 6150: //y-point where to start scan
SetProperty(prop, 0);
break;
case 6151: //horizontal exent
SetProperty(prop, (int)(8.5 * settings.dpi));
break;
case 6152: //vertical extent
SetProperty(prop, 11 * settings.dpi);
break;
}
}
}
/// <summary>
/// Use scanner to scan an image (scanner is selected by its unique id).
/// </summary>
/// <param name="scannerId"></param>
/// <returns>List of scanned images.</returns>
private static List<Image> Acquire(Device device, ScanSettings settings)
{
String description = device.Properties["Name"].get_Value().ToString();
if (description.ToLower().Contains("brother") || description.Contains("Canon MF4500"))
{
Logger.Log("Starting acquisition (Brother)");
return AcquireBrother(device, settings);
}
else
{
Logger.Log("Starting acquisition (Normal)");
return AcquireNormal(device, settings);
}
}
private static List<Image> AcquireBrother(Device device, ScanSettings settings)
{
List<Image> images = new List<Image>();
bool hasMorePages = true;
Item scan = null;
SetDeviceHandling(ref device, settings);
SetDeviceProperties(ref device, settings);
try
{ //Connect to scanner
scan = device.Items[1] as Item;
}
catch (Exception)
{
throw new Exception("Cannot connect to scanner, please check your device and try again.");
}
//Acquisition iteration
ICommonDialog wiaCommonDialog = new CommonDialog();
while (hasMorePages)
{
Logger.Log("DEBUG: document handling " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT));
Logger.Log("DEBUG: feeder status " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS));
try
{
//Some scanner need WIA_DPS_PAGES to be set to 1, otherwise all pages are acquired but only one is returned as ImageFile
SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_PAGES, 1);
//Scan image
ImageFile image = (ImageFile)wiaCommonDialog.ShowTransfer(scan, wiaFormatBMP, false);
if (image != null)
{
// convert to byte array
Byte[] imageBytes = (byte[])image.FileData.get_BinaryData();
// add file to output list
images.Add(Image.FromStream(new MemoryStream(imageBytes)));
//Cleanup
image = null;
imageBytes = null;
}
else
{
Logger.Log("Scan cancelled");
break;
}
// assume there are no more pages
hasMorePages = false;
if (settings.adf)
{
try
{ //try to read feed ready property (some scanners report ready even if no more pages)
int status = GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS);
hasMorePages = (status & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0;
}
catch { }
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
switch ((uint)ex.ErrorCode)
{
case WIA_ERRORS.WIA_ERROR_PAPER_EMPTY:
Logger.Log("Paper feed empty");
if (images.Count == 0 && settings.adf && settings.tryFlatbed)
{ //if no page scanned try flatbed instead
SetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT, WIA_DPS_DOCUMENT_HANDLING_SELECT.FLATBED);
settings.adf = false;
}
else
hasMorePages = false;
break;
case WIA_ERRORS.WIA_ERROR_PAPER_JAM:
Program.ShowError("Paper jam inside the scanner feeder");
break;
case WIA_ERRORS.WIA_ERROR_BUSY:
Logger.Log("Device is busy, retrying in 2s...");
System.Threading.Thread.Sleep(2000);
break;
default:
throw ex;
}
}
}
device = null;
return images;
}
private static List<Image> AcquireNormal(Device device, ScanSettings settings)
{
DeviceManager manager = new DeviceManager();
List<Image> images = new List<Image>();
bool hasMorePages = true;
Item scan = null;
//Acquisition iteration
ICommonDialog wiaCommonDialog = new CommonDialog();
while (hasMorePages)
{
try
{ //Looks like these need to be done for each iteration
SetDeviceHandling(ref device, settings);
scan = device.Items[1] as Item;
SetDeviceProperties(ref device, settings);
}
catch (Exception)
{
throw new Exception("Cannot connect to scanner, please check your device and try again.");
}
Logger.Log("DEBUG: document handling " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT));
Logger.Log("DEBUG: feeder status " + GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS));
try
{
//Scan image
ImageFile image = (ImageFile)wiaCommonDialog.ShowTransfer(scan, wiaFormatBMP, false);
if (image != null)
{
// convert to byte array
Byte[] imageBytes = (byte[])image.FileData.get_BinaryData();
// add file to output list
images.Add(Image.FromStream(new MemoryStream(imageBytes)));
//Cleanup
image = null;
imageBytes = null;
}
else
{
Logger.Log("Scan cancelled");
break;
}
// assume there are no more pages
hasMorePages = false;
if (settings.adf)
{
try
{ //try to read feed ready property (some scanners report ready even if no more pages)
int status = GetDeviceIntProperty(ref device, WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS);
hasMorePages = (status & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0;
Logger.Log("ADF has more pages: " + (hasMorePages ? "Yes" : "No"));
}
catch { }
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
switch ((uint)ex.ErrorCode)
{
case WIA_ERRORS.WIA_ERROR_PAPER_EMPTY:
Logger.Log("Paper feed empty");
if (images.Count == 0 && settings.adf && settings.tryFlatbed)
{ //if no page scanned try try flatbed
settings.adf = false;
}
else
hasMorePages = false;
break;
case WIA_ERRORS.WIA_ERROR_PAPER_JAM:
Program.ShowError("Paper jam inside the scanner feeder");
break;
case WIA_ERRORS.WIA_ERROR_BUSY:
Logger.Log("Device is busy, retrying in 2s...");
System.Threading.Thread.Sleep(2000);
break;
default:
throw ex;
}
}
}
device = null;
return images;
}
private static void SetProperty(Property property, int value)
{
IProperty x = (IProperty)property;
Object val = value;
x.set_Value(ref val);
}
private static void SetDeviceIntProperty(ref Device device, int propertyID, int propertyValue)
{
foreach (Property p in device.Properties)
{
if (p.PropertyID == propertyID)
{
object value = propertyValue;
p.set_Value(ref value);
break;
}
}
}
private static int GetDeviceIntProperty(ref Device device, int propertyID)
{
int ret = -1;
foreach (Property p in device.Properties)
{
if (p.PropertyID == propertyID)
{
ret = (int)p.get_Value();
break;
}
}
return ret;
}
}
}