ntwain/samples/Net5Console/Program.cs

139 lines
5.3 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.CAP_SUPPORTEDCAPS.GetValues())
{
WriteCapInfo(caps, cap);
}
Console.WriteLine();
2021-04-26 03:13:25 +08:00
var pxWrapper = caps.ICAP_PIXELTYPE;
Console.WriteLine($"Details on {pxWrapper.Cap}:");
Console.WriteLine($"\tDefault: {pxWrapper.GetDefault()}");
Console.WriteLine($"\tCurrent: {pxWrapper.GetCurrent()}");
Console.WriteLine($"\tValues:");
foreach (var val in pxWrapper.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
}
private static void WriteCapInfo(Capabilities caps, CAP cap)
{
// use reflection due to generics
var propInfo = typeof(Capabilities).GetProperty(cap.ToString());
if (propInfo == null) return;
var capWrapper = propInfo.GetValue(caps);
var label = (string)capWrapper.GetType().GetMethod(nameof(CapWrapper<int>.GetLabel)).Invoke(capWrapper, null);
var supports = (TWQC)capWrapper.GetType().GetMethod(nameof(CapWrapper<int>.QuerySupport)).Invoke(capWrapper, null);
Console.WriteLine($"\t{label ?? cap.ToString()}: {supports}");
}
2021-04-22 01:06:02 +08:00
}
}