2014-04-07 03:22:11 +08:00
|
|
|
|
using NTwain;
|
|
|
|
|
using NTwain.Data;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
2015-03-16 05:36:17 +08:00
|
|
|
|
namespace Sample
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2014-11-14 10:49:12 +08:00
|
|
|
|
if (PlatformInfo.Current.IsApp64Bit)
|
2014-09-18 08:15:05 +08:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[64bit]");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[32bit]");
|
|
|
|
|
}
|
2014-04-15 07:04:48 +08:00
|
|
|
|
// just an amusing example to do twain in console without UI
|
2014-04-20 09:20:50 +08:00
|
|
|
|
ThreadPool.QueueUserWorkItem(o =>
|
|
|
|
|
{
|
|
|
|
|
DoTwainWork();
|
|
|
|
|
});
|
2014-04-15 07:51:36 +08:00
|
|
|
|
Console.WriteLine("Test started, press Enter to exit.");
|
2014-04-07 03:22:11 +08:00
|
|
|
|
Console.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static readonly TwainSession twain = InitTwain();
|
|
|
|
|
private static TwainSession InitTwain()
|
|
|
|
|
{
|
|
|
|
|
var twain = new TwainSession(TWIdentity.CreateFromAssembly(DataGroups.Image, Assembly.GetExecutingAssembly()));
|
2014-09-18 08:15:05 +08:00
|
|
|
|
twain.TransferReady += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Got xfer ready on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
};
|
|
|
|
|
twain.DataTransferred += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (e.NativeData != IntPtr.Zero)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("SUCCESS! Got twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("BUMMER! No twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
twain.SourceDisabled += (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Source disabled on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
|
|
|
|
var rc = twain.CurrentSource.Close();
|
|
|
|
|
rc = twain.Close();
|
|
|
|
|
};
|
2014-04-07 03:22:11 +08:00
|
|
|
|
return twain;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-18 08:15:05 +08:00
|
|
|
|
const string SAMPLE_SOURCE = "TWAIN2 FreeImage Software Scanner";
|
2014-04-15 07:04:48 +08:00
|
|
|
|
static void DoTwainWork()
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
2014-09-18 08:15:05 +08:00
|
|
|
|
Console.WriteLine("Getting ready to do twain stuff on thread {0}...", Thread.CurrentThread.ManagedThreadId);
|
2014-04-07 03:22:11 +08:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
|
2014-05-20 09:26:44 +08:00
|
|
|
|
var rc = twain.Open();
|
2014-04-07 03:22:11 +08:00
|
|
|
|
|
|
|
|
|
if (rc == ReturnCode.Success)
|
|
|
|
|
{
|
2014-09-18 08:15:05 +08:00
|
|
|
|
var hit = twain.FirstOrDefault(s => string.Equals(s.Name, SAMPLE_SOURCE));
|
2014-04-19 21:10:15 +08:00
|
|
|
|
if (hit == null)
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
2014-09-18 08:15:05 +08:00
|
|
|
|
Console.WriteLine("The sample source \"" + SAMPLE_SOURCE + "\" is not installed.");
|
2014-05-20 09:26:44 +08:00
|
|
|
|
twain.Close();
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-20 08:48:21 +08:00
|
|
|
|
rc = hit.Open();
|
2014-04-19 21:10:15 +08:00
|
|
|
|
|
|
|
|
|
if (rc == ReturnCode.Success)
|
|
|
|
|
{
|
2014-09-18 08:15:05 +08:00
|
|
|
|
Console.WriteLine("Starting capture from the sample source...");
|
2014-09-12 09:14:41 +08:00
|
|
|
|
rc = hit.Enable(SourceEnableMode.NoUI, false, IntPtr.Zero);
|
2014-04-19 21:10:15 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-05-20 09:26:44 +08:00
|
|
|
|
twain.Close();
|
2014-04-19 21:10:15 +08:00
|
|
|
|
}
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-20 09:20:50 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed to open dsm with rc={0}!", rc);
|
|
|
|
|
}
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|