ntwain/samples/Sample.Console/Program.cs

101 lines
3.1 KiB
C#
Raw Normal View History

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
{
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]");
}
// just an amusing example to do twain in console without UI
2014-04-20 09:20:50 +08:00
ThreadPool.QueueUserWorkItem(o =>
{
DoTwainWork();
});
Console.WriteLine("Test started, press Enter to exit.");
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();
};
return twain;
}
2014-09-18 08:15:05 +08:00
const string SAMPLE_SOURCE = "TWAIN2 FreeImage Software Scanner";
static void DoTwainWork()
{
2014-09-18 08:15:05 +08:00
Console.WriteLine("Getting ready to do twain stuff on thread {0}...", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
2014-05-20 09:26:44 +08:00
var rc = twain.Open();
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-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();
}
else
{
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-20 09:20:50 +08:00
else
{
Console.WriteLine("Failed to open dsm with rc={0}!", rc);
}
}
}
}