Обнаружение конца выполнения пакетного задания при TWAIN сканировании
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как обнаружить разделители пакетного задания во время TWAIN сканирования:
static bool _isScanningFinished;
/// <summary>
/// Job counter.
/// </summary>
static int _jobCounter = -1;
/// <summary>
/// Asynchronously acquires images from TWAIN device and detects end of batch job.
/// </summary>
public static void AsynchronouslyAcquireImageFromTwainDeviceAndDetectEndOfBatchJob()
{
_isScanningFinished = false;
// create the device manager
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
deviceManager.IsTwain2Compatible = true;
// open the device manager
deviceManager.Open();
// get the device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
if (device == null)
return;
// subscribe to the device events
device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished += new System.EventHandler(device_ScanFinished);
// set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
device.ShowUI = true;
// open the device
device.Open();
// specify that job separator must be searched and scanned
device.JobControl = Vintasoft.Twain.JobControl.DetectAndIncludeJobSeparatorAndContinueScanning;
// increment job count
_jobCounter = _jobCounter + 1;
// acquire images asynchronously
device.Acquire();
// wait while feeder will be stopped
while (!_isScanningFinished)
{
System.Windows.Forms.Application.DoEvents();
}
}
}
/// <summary>
/// Image is acquired.
/// </summary>
private static void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
{
Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
// if job separator is detected
if (device.EndOfJob)
// increment job count and do not save the job separator image
_jobCounter = _jobCounter + 1;
// if job separator is NOT detected
else
// add image to the multipage TIFF file for current job
e.Image.Save(string.Format(@"d:\job{0}.tif", _jobCounter));
// dispose the acquired image
e.Image.Dispose();
}
/// <summary>
/// Scan is canceled.
/// </summary>
private static void device_ScanCanceled(object sender, System.EventArgs e)
{
System.Console.WriteLine("Scan is canceled.");
}
/// <summary>
/// Scan is failed.
/// </summary>
private static void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
{
System.Console.WriteLine(string.Format("Scan is failed: {0}.", e.ErrorString));
}
/// <summary>
/// Scan is finished.
/// </summary>
private static void device_ScanFinished(object sender, System.EventArgs e)
{
Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
// unsubscribe from device events
device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
device.ScanFinished -= new System.EventHandler(device_ScanFinished);
// if device is not closed
if (device.State != Vintasoft.Twain.DeviceState.Closed)
// close the device
device.Close();
System.Console.WriteLine("Scan is finished.");
_isScanningFinished = true;
}
Shared _isScanningFinished As Boolean
''' <summary>
''' Job counter.
''' </summary>
Shared _jobCounter As Integer = -1
''' <summary>
''' Asynchronously acquires images from TWAIN device and detects end of batch job.
''' </summary>
Public Shared Sub AsynchronouslyAcquireImageFromTwainDeviceAndDetectEndOfBatchJob()
_isScanningFinished = False
' create the device manager
Using deviceManager As New Vintasoft.Twain.DeviceManager()
deviceManager.IsTwain2Compatible = True
' open the device manager
deviceManager.Open()
' get the device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
If device Is Nothing Then
Return
End If
' subscribe to the device events
AddHandler device.ImageAcquired, New System.EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
AddHandler device.ScanCanceled, New System.EventHandler(AddressOf device_ScanCanceled)
AddHandler device.ScanFailed, New System.EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
AddHandler device.ScanFinished, New System.EventHandler(AddressOf device_ScanFinished)
' set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory
device.ShowUI = True
' open the device
device.Open()
' specify that job separator must be searched and scanned
device.JobControl = Vintasoft.Twain.JobControl.DetectAndIncludeJobSeparatorAndContinueScanning
' increment job count
_jobCounter = _jobCounter + 1
' acquire images asynchronously
device.Acquire()
' wait while feeder will be stopped
While Not _isScanningFinished
System.Windows.Forms.Application.DoEvents()
End While
End Using
End Sub
''' <summary>
''' Image is acquired.
''' </summary>
Private Shared Sub device_ImageAcquired(sender As Object, e As Vintasoft.Twain.ImageAcquiredEventArgs)
Dim device As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
' if job separator is detected
If device.EndOfJob Then
' increment job count and do not save the job separator image
_jobCounter = _jobCounter + 1
Else
' if job separator is NOT detected
' add image to the multipage TIFF file for current job
e.Image.Save(String.Format("d:\job{0}.tif", _jobCounter))
End If
' dispose the acquired image
e.Image.Dispose()
End Sub
''' <summary>
''' Scan is canceled.
''' </summary>
Private Shared Sub device_ScanCanceled(sender As Object, e As System.EventArgs)
System.Console.WriteLine("Scan is canceled.")
End Sub
''' <summary>
''' Scan is failed.
''' </summary>
Private Shared Sub device_ScanFailed(sender As Object, e As Vintasoft.Twain.ScanFailedEventArgs)
System.Console.WriteLine(String.Format("Scan is failed: {0}.", e.ErrorString))
End Sub
''' <summary>
''' Scan is finished.
''' </summary>
Private Shared Sub device_ScanFinished(sender As Object, e As System.EventArgs)
Dim device As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
' unsubscribe from device events
RemoveHandler device.ImageAcquired, New System.EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
RemoveHandler device.ScanCanceled, New System.EventHandler(AddressOf device_ScanCanceled)
RemoveHandler device.ScanFailed, New System.EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
RemoveHandler device.ScanFinished, New System.EventHandler(AddressOf device_ScanFinished)
' if device is not closed
If device.State <> Vintasoft.Twain.DeviceState.Closed Then
' close the device
device.Close()
End If
System.Console.WriteLine("Scan is finished.")
_isScanningFinished = True
End Sub