2021-04-22 01:06:02 +08:00
|
|
|
|
using NTwain;
|
|
|
|
|
using System;
|
2021-04-26 05:33:54 +08:00
|
|
|
|
using System.Collections;
|
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;
|
2022-04-25 06:08:43 +08:00
|
|
|
|
using static TWAINWorkingGroup.TWAIN;
|
2021-04-22 01:06:02 +08:00
|
|
|
|
|
|
|
|
|
namespace Net5Console
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
2021-04-25 04:53:54 +08:00
|
|
|
|
[STAThread]
|
2021-04-28 19:19:02 +08:00
|
|
|
|
static void Main()
|
2021-04-22 01:06:02 +08:00
|
|
|
|
{
|
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
|
|
|
|
|
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
|
|
|
|
{
|
2021-04-28 19:19:02 +08:00
|
|
|
|
Console.WriteLine($"Got device event " + e.Event);
|
2021-04-25 10:24:24 +08:00
|
|
|
|
Console.WriteLine();
|
2021-04-22 10:43:52 +08:00
|
|
|
|
};
|
2021-04-28 19:19:02 +08:00
|
|
|
|
session.TransferError += (sender, e) =>
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
2021-04-28 19:19:02 +08:00
|
|
|
|
Console.WriteLine($"Transfer error {e.Code} {e.Exception}.");
|
|
|
|
|
};
|
|
|
|
|
session.TransferReady += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Transfer ready, count={e.PendingCount}.");
|
|
|
|
|
};
|
2021-04-28 19:44:34 +08:00
|
|
|
|
session.SourceDisabled += (sender, e) =>
|
2021-04-28 19:19:02 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Disabled device.");
|
2021-04-25 10:24:24 +08:00
|
|
|
|
Console.WriteLine();
|
2021-04-22 10:43:52 +08:00
|
|
|
|
hold.Set();
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-25 06:08:43 +08:00
|
|
|
|
if (session.Open() == 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:");
|
2021-04-28 08:59:52 +08:00
|
|
|
|
Console.WriteLine($"\t{session.DefaultDataSource}");
|
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-28 19:44:34 +08:00
|
|
|
|
TW_IDENTITY? dsToUse = null;
|
2021-04-28 08:59:52 +08:00
|
|
|
|
foreach (var dev in session.GetDataSources())
|
2021-04-22 01:06:02 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"\t{dev}");
|
2021-04-28 19:44:34 +08:00
|
|
|
|
if (dev.ProductName == "TWAIN2 Software Scanner")
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
|
|
|
|
dsToUse = dev;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 10:24:24 +08:00
|
|
|
|
Console.WriteLine();
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-28 19:44:34 +08:00
|
|
|
|
if (!dsToUse.HasValue)
|
2021-04-22 10:43:52 +08:00
|
|
|
|
{
|
2021-04-28 19:44:34 +08:00
|
|
|
|
Console.WriteLine("Sample scanner not found.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
session.CurrentDataSource = dsToUse.Value;
|
|
|
|
|
if (session.CurrentDataSource.HasValue)
|
2021-04-26 02:43:15 +08:00
|
|
|
|
{
|
2021-04-28 19:44:34 +08:00
|
|
|
|
Console.WriteLine("Current device after opening attempt:");
|
|
|
|
|
Console.WriteLine($"\t{session.CurrentDataSource}");
|
|
|
|
|
Console.WriteLine();
|
2021-04-25 11:05:38 +08:00
|
|
|
|
|
2021-04-28 19:44:34 +08:00
|
|
|
|
var caps = session.Capabilities;
|
|
|
|
|
Console.WriteLine("All device caps:");
|
|
|
|
|
foreach (var cap in caps.CAP_SUPPORTEDCAPS.GetValues())
|
|
|
|
|
{
|
|
|
|
|
WriteCapInfo(caps, cap);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine();
|
2021-04-26 08:54:25 +08:00
|
|
|
|
|
2021-04-28 19:44:34 +08:00
|
|
|
|
short count = 3;
|
|
|
|
|
var sts = caps.CAP_XFERCOUNT.SetOrConstraint(MSG.SET, count);
|
|
|
|
|
if (sts == STS.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Successfully set xfercount to {count}.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Failed set xfercount: {sts}.");
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine();
|
2021-04-26 09:11:52 +08:00
|
|
|
|
|
|
|
|
|
|
2021-04-28 19:44:34 +08:00
|
|
|
|
sts = caps.ICAP_PIXELTYPE.SetOrConstraint(MSG.SET, TWPT.GRAY);
|
|
|
|
|
if (sts == STS.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Successfully set pixel type to GRAY.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Failed set pixel type: {sts}.");
|
|
|
|
|
}
|
2021-04-25 10:24:24 +08:00
|
|
|
|
Console.WriteLine();
|
2021-04-22 10:43:52 +08:00
|
|
|
|
|
2021-04-28 19:44:34 +08:00
|
|
|
|
sts = session.StartCapture(false);
|
|
|
|
|
if (sts == STS.SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Waiting for capture to complete.");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
while (!hold.IsSet) Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.Error.WriteLine("Failed to start capture: " + sts);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
}
|
2021-04-22 10:43:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-28 19:44:34 +08:00
|
|
|
|
Console.WriteLine("No devices opened.");
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2021-04-26 02:43:15 +08:00
|
|
|
|
|
2021-04-28 19:19:02 +08:00
|
|
|
|
private static void Session_SourceDisabled(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 02:43:15 +08:00
|
|
|
|
private static void WriteCapInfo(Capabilities caps, CAP cap)
|
|
|
|
|
{
|
2021-04-26 05:33:54 +08:00
|
|
|
|
// use reflection due to unknown generics
|
2021-04-26 02:43:15 +08:00
|
|
|
|
var propInfo = typeof(Capabilities).GetProperty(cap.ToString());
|
|
|
|
|
if (propInfo == null) return;
|
|
|
|
|
|
|
|
|
|
var capWrapper = propInfo.GetValue(caps);
|
2021-04-26 05:33:54 +08:00
|
|
|
|
var wrapType = capWrapper.GetType();
|
|
|
|
|
var label = (string)wrapType.GetMethod(nameof(CapWrapper<int>.GetLabel)).Invoke(capWrapper, null);
|
|
|
|
|
var supports = (TWQC)wrapType.GetMethod(nameof(CapWrapper<int>.QuerySupport)).Invoke(capWrapper, null);
|
2021-04-26 02:43:15 +08:00
|
|
|
|
|
|
|
|
|
Console.WriteLine($"\t{label ?? cap.ToString()}: {supports}");
|
2021-04-26 05:33:54 +08:00
|
|
|
|
Console.WriteLine($"\t\tDefault: {wrapType.GetMethod(nameof(CapWrapper<int>.GetDefault)).Invoke(capWrapper, null)}");
|
|
|
|
|
Console.WriteLine($"\t\tCurrent: {wrapType.GetMethod(nameof(CapWrapper<int>.GetCurrent)).Invoke(capWrapper, null)}");
|
|
|
|
|
bool first = true;
|
|
|
|
|
foreach (var val in (IEnumerable)wrapType.GetMethod(nameof(CapWrapper<int>.GetValues)).Invoke(capWrapper, null))
|
|
|
|
|
{
|
|
|
|
|
if (first)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"\t\tValues:\t{val}");
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"\t\t\t{val}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-26 02:43:15 +08:00
|
|
|
|
}
|
2021-04-22 01:06:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|