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
| namespace Ztwain { using System; using System.Runtime.InteropServices; using System.Windows.Forms;
public enum TwainCommand { Not = -1, Null = 0, TransferReady = 1, CloseRequest = 2, CloseOk = 3, DeviceEvent = 4, }
public delegate void ProductNameHandler(string productName);
public class Twain { private const short COUNTRY_USA = 1; private const short LANGUAGE_USA = 13; private readonly ProductNameHandler _productNameHandler; private bool _isOpenDs;
public Twain(ProductNameHandler handler) { _appid = new TwIdentity(); _appid.Id = IntPtr.Zero; _appid.Version.MajorNum = 1; _appid.Version.MinorNum = 1; _appid.Version.Language = LANGUAGE_USA; _appid.Version.Country = COUNTRY_USA; _appid.Version.Info = "Hack 1"; _appid.ProtocolMajor = TwProtocol.MAJOR; _appid.ProtocolMinor = TwProtocol.MINOR; _appid.SupportedGroups = (int)(TwDg.Image | TwDg.Control); _appid.Manufacturer = "NETMaster"; _appid.ProductFamily = "Freeware"; _appid.ProductName = "Hack";
_srcds = new TwIdentity(); _srcds.Id = IntPtr.Zero;
_evtmsg.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(_winmsg)); _productNameHandler = handler; }
~Twain() { Marshal.FreeHGlobal(_evtmsg.EventPtr); }
public void Init(IntPtr hwndp) { Finish(); TwRc rc = DSMparent( _appid, IntPtr.Zero, TwDg.Control, TwDat.Parent, TwMsg.OpenDsm, ref hwndp ); if (rc == TwRc.Success) { rc = DSMident( _appid, IntPtr.Zero, TwDg.Control, TwDat.Identity, TwMsg.GetDefault, _srcds ); if (rc == TwRc.Success) _hwnd = hwndp; else DSMparent( _appid, IntPtr.Zero, TwDg.Control, TwDat.Parent, TwMsg.CloseDsm, ref hwndp ); }
if (_productNameHandler != null) { _productNameHandler(_srcds.ProductName); } }
public void Select() { CloseDs(); if (_appid.Id == IntPtr.Zero) { Init(_hwnd); if (_appid.Id == IntPtr.Zero) return; }
DSMident(_appid, IntPtr.Zero, TwDg.Control, TwDat.Identity, TwMsg.UserSelect, _srcds); if (_productNameHandler != null) { _productNameHandler(_srcds.ProductName); } }
public void Acquire() { Acquire(true); }
public void Acquire(bool showUi) { CloseDs(); if (_appid.Id == IntPtr.Zero) { Init(_hwnd); if (_appid.Id == IntPtr.Zero) return; } TwRc rc = DSMident( _appid, IntPtr.Zero, TwDg.Control, TwDat.Identity, TwMsg.OpenDs, _srcds ); if (rc != TwRc.Success) return;
_isOpenDs = true;
TwCapability cap2 = new TwCapability(TwCap.CapDuplexenabled, 1); TwRc rc2 = DScap(_appid, _srcds, TwDg.Control, TwDat.Capability, TwMsg.Set, cap2); Console.WriteLine(rc2 == TwRc.Success ? @"双面扫描 设置成功" : @"双面扫描 设置失败"); if (rc2 != TwRc.Success) { CloseDs(); return; }
short xyResolution = 200; TwCapability capXRes = new TwCapability(TwCap.XRes, xyResolution); TwRc rcXRes = DScap(_appid, _srcds, TwDg.Control, TwDat.Capability, TwMsg.Set, capXRes); TwCapability capYRes = new TwCapability(TwCap.YRes, xyResolution); TwRc rcYRes = DScap(_appid, _srcds, TwDg.Control, TwDat.Capability, TwMsg.Set, capYRes); if (rcXRes != TwRc.Success || rcYRes != TwRc.Success) { Console.WriteLine(@"分辨率 设置失败"); } else { Console.WriteLine(@"分辨率 设置成功"); } TwUserInterface guif = new TwUserInterface { ShowUI = (short)(showUi ? 1 : 0), ModalUI = 0, ParentHand = _hwnd, }; rc = DSuserif(_appid, _srcds, TwDg.Control, TwDat.UserInterface, TwMsg.EnableDs, guif); if (rc != TwRc.Success) { CloseDs(); } }
public void TransferPictures(Action<IntPtr> picAction) { if (_srcds.Id == IntPtr.Zero) return;
TwPendingXfers pxfr = new TwPendingXfers();
do { pxfr.Count = 0; IntPtr hbitmap = IntPtr.Zero; TwImageInfo iinf = new TwImageInfo(); TwRc rc = DSiinf(_appid, _srcds, TwDg.Image, TwDat.ImageInfo, TwMsg.Get, iinf); if (rc != TwRc.Success) { CloseDs(); return; } rc = DSixfer( _appid, _srcds, TwDg.Image, TwDat.ImageNativeXfer, TwMsg.Get, ref hbitmap ); if (rc != TwRc.XferDone) { CloseDs(); return; }
rc = DSpxfer(_appid, _srcds, TwDg.Control, TwDat.PendingXfers, TwMsg.EndXfer, pxfr); if (rc != TwRc.Success) { CloseDs(); return; }
picAction(hbitmap); } while (pxfr.Count != 0);
DSpxfer(_appid, _srcds, TwDg.Control, TwDat.PendingXfers, TwMsg.Reset, pxfr); }
public TwainCommand PassMessage(ref Message m) { if (_srcds.Id == IntPtr.Zero) return TwainCommand.Not;
int pos = TwainWin32.GetMessagePos();
_winmsg.hwnd = m.HWnd; _winmsg.message = m.Msg; _winmsg.wParam = m.WParam; _winmsg.lParam = m.LParam; _winmsg.time = TwainWin32.GetMessageTime(); _winmsg.x = (short)pos; _winmsg.y = (short)(pos >> 16);
Marshal.StructureToPtr(_winmsg, _evtmsg.EventPtr, false); _evtmsg.Message = 0; TwRc rc = DSevent( _appid, _srcds, TwDg.Control, TwDat.Event, TwMsg.ProcessEvent, ref _evtmsg ); if (rc == TwRc.NotDsEvent) return TwainCommand.Not; if (_evtmsg.Message == (short)TwMsg.XFerReady) return TwainCommand.TransferReady; if (_evtmsg.Message == (short)TwMsg.CloseDsReq) return TwainCommand.CloseRequest; if (_evtmsg.Message == (short)TwMsg.CloseDsok) return TwainCommand.CloseOk; if (_evtmsg.Message == (short)TwMsg.DeviceEvent) return TwainCommand.DeviceEvent;
return TwainCommand.Null; }
public void CloseDs() { if (_srcds.Id != IntPtr.Zero) { TwUserInterface guif = new TwUserInterface(); DSuserif(_appid, _srcds, TwDg.Control, TwDat.UserInterface, TwMsg.DisableDs, guif); if (_isOpenDs) { DSMident( _appid, IntPtr.Zero, TwDg.Control, TwDat.Identity, TwMsg.CloseDs, _srcds ); _isOpenDs = false; } } }
public void Finish() { CloseDs(); if (_appid.Id != IntPtr.Zero) DSMparent( _appid, IntPtr.Zero, TwDg.Control, TwDat.Parent, TwMsg.CloseDsm, ref _hwnd ); _appid.Id = IntPtr.Zero; }
private IntPtr _hwnd; private readonly TwIdentity _appid; private readonly TwIdentity _srcds; private TwEvent _evtmsg; private Winmsg _winmsg;
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSMparent( [In, Out] TwIdentity origin, IntPtr zeroptr, TwDg dg, TwDat dat, TwMsg msg, ref IntPtr refptr );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSMident( [In, Out] TwIdentity origin, IntPtr zeroptr, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwIdentity idds );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSMstatus( [In, Out] TwIdentity origin, IntPtr zeroptr, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwStatus dsmstat );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSuserif( [In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, TwUserInterface guif );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSevent( [In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, ref TwEvent evt );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSstatus( [In, Out] TwIdentity origin, [In] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwStatus dsmstat );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DScap( [In, Out] TwIdentity origin, [In] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwCapability capa );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSiinf( [In, Out] TwIdentity origin, [In] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwImageInfo imginf );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSixfer( [In, Out] TwIdentity origin, [In] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, ref IntPtr hbitmap );
[DllImport("twain_32.dll", EntryPoint = "#1")] private static extern TwRc DSpxfer( [In, Out] TwIdentity origin, [In] TwIdentity dest, TwDg dg, TwDat dat, TwMsg msg, [In, Out] TwPendingXfers pxfr );
[StructLayout(LayoutKind.Sequential, Pack = 4)] internal struct Winmsg { public IntPtr hwnd; public int message; public IntPtr wParam; public IntPtr lParam; public int time; public int x; public int y; } } }
|