2021-04-22 01:06:02 +08:00
|
|
|
|
using NTwain;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2021-04-22 10:43:52 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using TWAINWorkingGroup;
|
2021-04-22 01:06:02 +08:00
|
|
|
|
|
|
|
|
|
namespace Net5Console
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-04-25 04:53:54 +08:00
|
|
|
|
[STAThread]
|
2021-04-22 01:06:02 +08:00
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2021-04-22 10:43:52 +08:00
|
|
|
|
Console.WriteLine("Starting twain test in console...");
|
|
|
|
|
|
2021-04-22 19:29:03 +08:00
|
|
|
|
using (var session = new TwainSession(Assembly.GetExecutingAssembly(), null, IntPtr.Zero))
|
2021-04-22 10:43:52 +08:00
|
|
|
|
using (var hold = new ManualResetEventSlim())
|
2021-04-22 01:06:02 +08:00
|
|
|
|
{
|
2021-04-22 19:29:03 +08:00
|
|
|
|
session.DeviceEvent += (sender, e) =>
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Got device event " + (TWDE)e.Event);
|
|
|
|
|
};
|
2021-04-22 19:29:03 +08:00
|
|
|
|
session.ScanEvent += (sender, closing) =>
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Got scan event " + closing);
|
|
|
|
|
|
2021-04-22 19:29:03 +08:00
|
|
|
|
// don't care, just end it
|
|
|
|
|
TW_PENDINGXFERS pending = default;
|
|
|
|
|
var sts = session.TWAIN.DatPendingxfers(DG.CONTROL, MSG.RESET, ref pending);
|
|
|
|
|
|
2021-04-22 10:43:52 +08:00
|
|
|
|
TW_USERINTERFACE twuserinterface = default;
|
2021-04-22 19:29:03 +08:00
|
|
|
|
if (session.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS)
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Disabled device.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Failed to disabled device.");
|
|
|
|
|
}
|
|
|
|
|
hold.Set();
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-22 19:29:03 +08:00
|
|
|
|
if (session.Open() == TWAINWorkingGroup.STS.SUCCESS)
|
2021-04-22 01:06:02 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Opened DSM");
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-22 01:06:02 +08:00
|
|
|
|
Console.WriteLine("Default device:");
|
2021-04-22 19:29:03 +08:00
|
|
|
|
Console.WriteLine($"\t{session.DefaultDevice}");
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-22 01:06:02 +08:00
|
|
|
|
Console.WriteLine("All devices:");
|
2021-04-22 10:43:52 +08:00
|
|
|
|
TW_IDENTITY dsToUse = default;
|
2021-04-22 19:29:03 +08:00
|
|
|
|
foreach (var dev in session.GetDevices())
|
2021-04-22 01:06:02 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"\t{dev}");
|
2021-04-22 10:43:52 +08:00
|
|
|
|
if (dev.ProductName == "TWAIN2 FreeImage Software Scanner")
|
|
|
|
|
{
|
|
|
|
|
dsToUse = dev;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-22 19:29:03 +08:00
|
|
|
|
session.CurrentDevice = dsToUse;
|
|
|
|
|
if (session.CurrentDevice.HasValue)
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Current device after opening attempt:");
|
2021-04-22 19:29:03 +08:00
|
|
|
|
Console.WriteLine($"\t{session.CurrentDevice}");
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-22 19:29:03 +08:00
|
|
|
|
var sts = session.StartCapture(false);
|
2021-04-22 10:43:52 +08:00
|
|
|
|
if (sts == STS.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Waiting for capture to complete.");
|
|
|
|
|
|
|
|
|
|
while (!hold.IsSet) Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Failed to start capture: " + sts);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("No devices opened.");
|
2021-04-22 01:06:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Failed to open DSM");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine("Test Ended");
|
|
|
|
|
}
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-22 01:06:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|