2014-04-07 03:22:11 +08:00
|
|
|
|
using NTwain;
|
|
|
|
|
using NTwain.Data;
|
|
|
|
|
using NTwain.Values;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
|
|
|
|
|
namespace Tester
|
|
|
|
|
{
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2014-04-15 07:04:48 +08:00
|
|
|
|
// just an amusing example to do twain in console without UI
|
|
|
|
|
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()));
|
|
|
|
|
twain.DataTransferred += twain_DataTransferred;
|
|
|
|
|
twain.TransferReady += twain_TransferReady;
|
|
|
|
|
twain.SourceDisabled += twain_SourceDisabled;
|
|
|
|
|
return twain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-04-15 07:04:48 +08:00
|
|
|
|
static void DoTwainWork()
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
2014-04-19 21:10:15 +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-04-15 07:04:48 +08:00
|
|
|
|
var rc = twain.OpenManager();
|
2014-04-07 03:22:11 +08:00
|
|
|
|
|
|
|
|
|
if (rc == ReturnCode.Success)
|
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
var hit = twain.GetSources().Where(s => string.Equals(s.ProductName, "TWAIN2 FreeImage Software Scanner")).FirstOrDefault();
|
|
|
|
|
if (hit == null)
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
Console.WriteLine("The sample source \"TWAIN2 FreeImage Software Scanner\" is not installed.");
|
|
|
|
|
twain.CloseManager();
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
rc = twain.OpenSource(hit.ProductName);
|
|
|
|
|
|
|
|
|
|
if (rc == ReturnCode.Success)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Start capture from the sample source.");
|
|
|
|
|
rc = twain.EnableSource(SourceEnableMode.NoUI, false, IntPtr.Zero);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
twain.CloseManager();
|
|
|
|
|
}
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void twain_SourceDisabled(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
Console.WriteLine("Source disabled on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
2014-04-07 03:22:11 +08:00
|
|
|
|
var rc = twain.CloseSource();
|
|
|
|
|
rc = twain.CloseManager();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void twain_TransferReady(object sender, TransferReadyEventArgs e)
|
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
Console.WriteLine("Got xfer ready on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void twain_DataTransferred(object sender, DataTransferredEventArgs e)
|
|
|
|
|
{
|
2014-04-10 19:30:00 +08:00
|
|
|
|
if (e.NativeData != IntPtr.Zero)
|
2014-04-07 03:22:11 +08:00
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
Console.WriteLine("Got twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-04-19 21:10:15 +08:00
|
|
|
|
Console.WriteLine("No twain data on thread {0}.", Thread.CurrentThread.ManagedThreadId);
|
2014-04-07 03:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|