How to get the extended information about image acquired from TWAIN scanner?
In This Topic
VintaSoft Twain .NET SDK allows to retrieve the extended information about acquired image. The extended image information can contain information about document/page number, image frame, barcodes or patch codes found on a page, information about despeckle, deskew process and more.
You should proceed with the following steps if you want to retrieve the extended information about image that is acquired from TWAIN device:
- Check that the retrieving of the extended image information is supported by TWAIN device.
- Choose which information you want to retrieve.
- Retrieve the extended image information about TWAIN acquired image right after the image is acquired from TWAIN device.
Here is an example that demonstrates how to acquire images from TWAIN device and retrieve from TWAIN device an extended information (document number, page number, camera, frame number, frame, pixel flavor) about acquired images:
static bool _isScanningFinished;
/// <summary>
/// Asynchronously acquires images from TWAIN device and retrieves from TWAIN device an extended information about acquired images.
/// </summary>
public static void AsynchronouslyAcquireImageFromTwainDeviceAndGetExtendedInfoAboutAcquiredImages()
{
_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.ScanFinished += new System.EventHandler(device_ScanFinished);
// set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
device.ShowUI = false;
device.DisableAfterAcquire = true;
// open the device
device.Open();
// specify that extended information about acquired image is necessary
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.DocumentNumber));
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PageNumber));
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Camera));
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.FrameNumber));
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Frame));
device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PixelFlavor));
// 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)
{
// output an extended information about the acquired image
System.Console.WriteLine("IMAGE IS ACQUIRED");
System.Console.WriteLine();
Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
for (int i = 0; i < device.ExtendedImageInfo.Count; i++)
{
AddExtendedImageInfoToResultTextBox(i, device.ExtendedImageInfo[i]);
}
System.Console.WriteLine();
// dispose the acquired image
e.Image.Dispose();
}
/// <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.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;
}
/// <summary>
/// Add an extended image info to the result text box.
/// </summary>
private static void AddExtendedImageInfoToResultTextBox(int index, Vintasoft.Twain.ExtendedImageInfo info)
{
if (!info.IsValueValid)
return;
System.Console.WriteLine(string.Format("Extended image info {0}", index));
System.Console.WriteLine(string.Format(" Name={0}", System.Enum.GetName(typeof(Vintasoft.Twain.ExtendedImageInfoId), info.InfoId)));
System.Console.WriteLine(string.Format(" Id={0}", info.InfoId));
System.Console.WriteLine(string.Format(" Value type={0}", info.ValueType));
Vintasoft.Twain.TwainOneValueContainer oneDeviceCapabilityValue = info.Value as Vintasoft.Twain.TwainOneValueContainer;
if (oneDeviceCapabilityValue != null)
{
System.Console.WriteLine(string.Format(" Value={0}", oneDeviceCapabilityValue.Value));
}
else
{
Vintasoft.Twain.TwainArrayValueContainer arrayDeviceCapabilityValue = info.Value as Vintasoft.Twain.TwainArrayValueContainer;
if (arrayDeviceCapabilityValue != null)
{
string values = " Values: ";
if (arrayDeviceCapabilityValue.Values.GetType() == typeof(byte[]))
{
values += string.Format("byte[{0}]", arrayDeviceCapabilityValue.Values.Length);
}
else
{
for (int i = 0; i < arrayDeviceCapabilityValue.Values.Length; i++)
values += string.Format("{0}, ", arrayDeviceCapabilityValue.Values.GetValue(i));
}
System.Console.WriteLine(values);
}
}
}
Shared _isScanningFinished As Boolean
''' <summary>
''' Asynchronously acquires images from TWAIN device and retrieves from TWAIN device an extended information about acquired images.
''' </summary>
Public Shared Sub AsynchronouslyAcquireImageFromTwainDeviceAndGetExtendedInfoAboutAcquiredImages()
_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.ScanFinished, New System.EventHandler(AddressOf device_ScanFinished)
' set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory
device.ShowUI = False
device.DisableAfterAcquire = True
' open the device
device.Open()
' specify that extended information about acquired image is necessary
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.DocumentNumber))
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PageNumber))
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Camera))
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.FrameNumber))
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Frame))
device.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PixelFlavor))
' 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)
' output an extended information about the acquired image
System.Console.WriteLine("IMAGE IS ACQUIRED")
System.Console.WriteLine()
Dim device As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
For i As Integer = 0 To device.ExtendedImageInfo.Count - 1
AddExtendedImageInfoToResultTextBox(i, device.ExtendedImageInfo(i))
Next
System.Console.WriteLine()
' dispose the acquired image
e.Image.Dispose()
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.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
''' <summary>
''' Add an extended image info to the result text box.
''' </summary>
Private Shared Sub AddExtendedImageInfoToResultTextBox(index As Integer, info As Vintasoft.Twain.ExtendedImageInfo)
If Not info.IsValueValid Then
Return
End If
System.Console.WriteLine(String.Format("Extended image info {0}", index))
System.Console.WriteLine(String.Format(" Name={0}", System.[Enum].GetName(GetType(Vintasoft.Twain.ExtendedImageInfoId), info.InfoId)))
System.Console.WriteLine(String.Format(" Id={0}", info.InfoId))
System.Console.WriteLine(String.Format(" Value type={0}", info.ValueType))
Dim oneDeviceCapabilityValue As Vintasoft.Twain.TwainOneValueContainer = TryCast(info.Value, Vintasoft.Twain.TwainOneValueContainer)
If oneDeviceCapabilityValue IsNot Nothing Then
System.Console.WriteLine(String.Format(" Value={0}", oneDeviceCapabilityValue.Value))
Else
Dim arrayDeviceCapabilityValue As Vintasoft.Twain.TwainArrayValueContainer = TryCast(info.Value, Vintasoft.Twain.TwainArrayValueContainer)
If arrayDeviceCapabilityValue IsNot Nothing Then
Dim values As String = " Values: "
If arrayDeviceCapabilityValue.Values.[GetType]() = GetType(Byte()) Then
values += String.Format("byte[{0}]", arrayDeviceCapabilityValue.Values.Length)
Else
For i As Integer = 0 To arrayDeviceCapabilityValue.Values.Length - 1
values += String.Format("{0}, ", arrayDeviceCapabilityValue.Values.GetValue(i))
Next
End If
System.Console.WriteLine(values)
End If
End If
End Sub
Here is an example that demonstrates how to acquire images from TWAIN device and retrieve from TWAIN device information about barcodes recognized on acquired images:
/// <summary>
/// Synchronously acquires images from TWAIN device and retrieves from TWAIN device information about barcodes recognized on acquired images.
/// </summary>
public void SynchronouslyAcquireImageFromTwainDeviceAndRecognizeBarcodeInAcquiredImages()
{
using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
{
try
{
// open the device manager
deviceManager.Open();
deviceManager.ShowDefaultDeviceSelectionDialog();
// get reference to the default device
Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
// specify that device UI must not be used
device.ShowUI = false;
// specify that device must be closed after scan
device.DisableAfterAcquire = true;
// open the device
device.Open();
// specify that device should recognize barcodes in acquired images
device.BarcodeRecognizer.IsEnabled = true;
// acquire images from device
Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// get information about recognized barcodes
Vintasoft.Twain.TwainBarcodeInfo[] recognizedBarcodes = device.BarcodeRecognizer.GetRecognitionResult();
// if barcodes are NOT found
if (recognizedBarcodes.Length == 0)
{
System.Console.WriteLine("Barcodes are NOT found.");
}
// if barcodes are found
else
{
System.Console.WriteLine(string.Format("{0} barcodes are found.", recognizedBarcodes.Length));
// for each recognized barcode
for (int i = 0; i < recognizedBarcodes.Length; i++)
{
// output barcode type
System.Console.Write(string.Format("BarcodeType={0} ", recognizedBarcodes[i].Type));
// output barcode text
System.Console.WriteLine(string.Format("BarcodeText='{0}'", recognizedBarcodes[i].Value));
}
}
// if acquired image is present (Native or Memory transfer mode is used)
if (device.AcquiredImage != null)
// dispose the acquired image
device.AcquiredImage.Dispose();
break;
}
}
while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
// close the device
device.Close();
// close the device manager
deviceManager.Close();
}
catch (Vintasoft.Twain.TwainException ex)
{
System.Console.WriteLine("Error: " + ex.Message);
System.Console.ReadLine();
}
}
}
''' <summary>
''' Synchronously acquires images from TWAIN device and retrieves from TWAIN device information about barcodes recognized on acquired images.
''' </summary>
Public Sub SynchronouslyAcquireImageFromTwainDeviceAndRecognizeBarcodeInAcquiredImages()
Using deviceManager As New Vintasoft.Twain.DeviceManager()
Try
' open the device manager
deviceManager.Open()
deviceManager.ShowDefaultDeviceSelectionDialog()
' get reference to the default device
Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
' specify that device UI must not be used
device.ShowUI = False
' specify that device must be closed after scan
device.DisableAfterAcquire = True
' open the device
device.Open()
' specify that device should recognize barcodes in acquired images
device.BarcodeRecognizer.IsEnabled = True
' acquire images from device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' get information about recognized barcodes
Dim recognizedBarcodes As Vintasoft.Twain.TwainBarcodeInfo() = device.BarcodeRecognizer.GetRecognitionResult()
' if barcodes are NOT found
If recognizedBarcodes.Length = 0 Then
System.Console.WriteLine("Barcodes are NOT found.")
Else
' if barcodes are found
System.Console.WriteLine(String.Format("{0} barcodes are found.", recognizedBarcodes.Length))
' for each recognized barcode
For i As Integer = 0 To recognizedBarcodes.Length - 1
' output barcode type
System.Console.Write(String.Format("BarcodeType={0} ", recognizedBarcodes(i).Type))
' output barcode text
System.Console.WriteLine(String.Format("BarcodeText='{0}'", recognizedBarcodes(i).Value))
Next
End If
' if acquired image is present (Native or Memory transfer mode is used)
If device.AcquiredImage IsNot Nothing Then
' dispose the acquired image
device.AcquiredImage.Dispose()
End If
Exit Select
End Select
Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
' close the device
device.Close()
' close the device manager
deviceManager.Close()
Catch ex As Vintasoft.Twain.TwainException
System.Console.WriteLine("Error: " + ex.Message)
System.Console.ReadLine()
End Try
End Using
End Sub