ntwain/samples/Net5Console/Program.cs

128 lines
4.9 KiB
C#
Raw Normal View History

2021-04-22 01:06:02 +08:00
using NTwain;
using System;
2021-04-25 10:24:24 +08:00
using System.Linq;
2021-04-22 01:06:02 +08:00
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
{
[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-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +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
{
session.DeviceEvent += (sender, e) =>
2021-04-22 10:43:52 +08:00
{
Console.WriteLine($"Got device event " + (TWDE)e.Event);
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
};
session.ScanEvent += (sender, closing) =>
2021-04-22 10:43:52 +08:00
{
Console.WriteLine($"Got scan event " + closing);
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +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;
if (session.TWAIN.DatUserinterface(DG.CONTROL, MSG.DISABLEDS, ref twuserinterface) == STS.SUCCESS)
2021-04-22 10:43:52 +08:00
{
Console.WriteLine("Disabled device.");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
}
else
{
Console.Error.WriteLine("Failed to disabled device.");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
}
hold.Set();
};
if (session.Open() == TWAINWorkingGroup.STS.SUCCESS)
2021-04-22 01:06:02 +08:00
{
Console.WriteLine("Opened DSM");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
2021-04-22 01:06:02 +08:00
Console.WriteLine("Default device:");
Console.WriteLine($"\t{session.DefaultDevice}");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
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;
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-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
session.CurrentDevice = dsToUse;
if (session.CurrentDevice.HasValue)
2021-04-22 10:43:52 +08:00
{
Console.WriteLine("Current device after opening attempt:");
Console.WriteLine($"\t{session.CurrentDevice}");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
var caps = session.Capabilities;
2021-04-25 10:24:24 +08:00
Console.WriteLine("Device supports these caps:");
//foreach (var cap in caps.Keys.OrderBy(c => c))
//{
// Console.WriteLine($"\t{cap}: {caps[cap].Supports}");
//}
Console.WriteLine();
//if (caps.TryGetValue(CAP.ICAP_PIXELTYPE, out CapWrapper wrapper))
//{
// Console.WriteLine($"Details on {wrapper.Cap}:");
// Console.WriteLine($"\tDefault: {wrapper.GetDefault()}");
// Console.WriteLine($"\tCurrent: {wrapper.GetCurrent()}");
// Console.WriteLine($"\tValues:");
// foreach (var val in wrapper.GetValues())
// {
// Console.WriteLine($"\t\t{val}");
// }
//}
//Console.WriteLine();
2021-04-22 10:43:52 +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.");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
while (!hold.IsSet) Thread.Sleep(100);
}
else
{
Console.Error.WriteLine("Failed to start capture: " + sts);
2021-04-25 10:24:24 +08:00
Console.WriteLine();
2021-04-22 10:43:52 +08:00
}
}
else
{
Console.WriteLine("No devices opened.");
2021-04-25 10:24:24 +08:00
Console.WriteLine();
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
}
}
}