VintaSoft Twain .NET SDK 15.3: Руководство для .NET разработчика
В этом разделе
    Как пропустить пустые страницы при TWAIN сканировании?
    В этом разделе
    Пустые страницы при TWAIN сканировании можно пропускать 2 способами:
    1. используя возможности TWAIN сканера изображений
    2. используя возможности библиотеки (SDK)

    1. Пропуск пустых страниц при использовании возможностей TWAIN сканера изображений

    Для начала необходимо проверить, поддерживает ли TWAIN сканер изображений возможность пропуска пустых страниц - проверьте, поддерживает ли TWAIN сканер возможность ICAP_AUTODISCARDBLANKPAGES.

    Если TWAIN сканер поддерживает возможность ICAP_AUTODISCARDBLANKPAGES, то вам нужно установить значение возможности таким образом, чтобы определить нужно ли пропускать пустые страницы.
    Допустимые значения возможности ICAP_AUTODISCARDBLANKPAGES:
    Вот C#/VB.NET код, который демонстрирует, как пропускать пустые страницы, если TWAIN сканер изображений поддерживает эту возможность:
    /// <summary>
    /// Synchronously acquires images from TWAIN device and skips blank pages using TWAIN capability.
    /// </summary>
    public void SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingTwainCapability()
    {
        try
        {
            using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
            {
                // open the device manager
                deviceManager.Open();
    
                // get reference to the default device
                Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
    
                // set scanning settings
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                device.ShowUI = false;
    
                // open the device
                device.Open();
    
                // get a reference to the IAutoDiscardBlankPages capability
                Vintasoft.Twain.DeviceCapability autoDiscardBlankPagesCap = device.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IAutoDiscardBlankPages);
                // if discarding of blank pages is not supported
                if (autoDiscardBlankPagesCap == null)
                    System.Windows.Forms.MessageBox.Show("Device cannot automatically discard blank pages.");
                else
                    // enable discarding of blank pages
                    autoDiscardBlankPagesCap.SetValue(true);
    
                // synchronously acquire image(s) from TWAIN device
                Vintasoft.Twain.AcquireModalState acquireModalState;
                do
                {
                    acquireModalState = device.AcquireModal();
                    switch (acquireModalState)
                    {
                        case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                            // add the acquired image to a TIFF file
                            device.AcquiredImage.Save(@"d:\test.tif");
    
                            // dispose the acquired image
                            device.AcquiredImage.Dispose();
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                            // output current state
                            System.Console.WriteLine("Scan completed.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                            // output current state
                            System.Console.WriteLine("Scan canceled.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanFailed:
                            // output current state
                            System.Console.WriteLine(string.Format("Scan failed: {0}", device.ErrorString));
                            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 skips blank pages using TWAIN capability.
    ''' </summary>
    Public Sub SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingTwainCapability()
        Try
            Using deviceManager As New Vintasoft.Twain.DeviceManager()
                ' open the device manager
                deviceManager.Open()
    
                ' get reference to the default device
                Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
    
                ' set scanning settings
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device.ShowUI = False
    
                ' open the device
                device.Open()
    
                ' get a reference to the IAutoDiscardBlankPages capability
                Dim autoDiscardBlankPagesCap As Vintasoft.Twain.DeviceCapability = device.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IAutoDiscardBlankPages)
                ' if discarding of blank pages is not supported
                If autoDiscardBlankPagesCap Is Nothing Then
                    System.Windows.Forms.MessageBox.Show("Device cannot automatically discard blank pages.")
                Else
                    ' enable discarding of blank pages
                    autoDiscardBlankPagesCap.SetValue(True)
                End If
    
                ' synchronously acquire image(s) from TWAIN device
                Dim acquireModalState As Vintasoft.Twain.AcquireModalState
                Do
                    acquireModalState = device.AcquireModal()
                    Select Case acquireModalState
                        Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                            ' add the acquired image to a TIFF file
                            device.AcquiredImage.Save("d:\test.tif")
    
                            ' dispose the acquired image
                            device.AcquiredImage.Dispose()
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                            ' output current state
                            System.Console.WriteLine("Scan completed.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                            ' output current state
                            System.Console.WriteLine("Scan canceled.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanFailed
                            ' output current state
                            System.Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
    
                ' close the device
                device.Close()
    
                ' close the device manager
                deviceManager.Close()
            End Using
        Catch ex As Vintasoft.Twain.TwainException
            System.Console.WriteLine("Error: " + ex.Message)
        End Try
    
        System.Console.ReadLine()
    End Sub
    


    Преимущества этой техники:
    Недостатки этой техники:


    2. Пропуск пустых страниц с использованием возможностей библиотеки (SDK)

    Алгоритм этой техники довольно прост. TWAIN сканер отправляет в библиотеку все изображения отсканированных страниц, а библиотека проверяет, является ли изображение пустым, используя метод Vintasoft.Twain.AcquiredImage.IsBlank.

    Вот C#/VB.NET код, который демонстрирует, как пропускать пустые страницы с помощью метода IsBlank:
    /// <summary>
    /// Synchronously acquires images from TWAIN device and skips blank pages using AcquiredImage.IsBlank method.
    /// </summary>
    public void SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingIsBlankMethod()
    {
        try
        {
            using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
            {
                // open the device manager
                deviceManager.Open();
    
                // get reference to the default device
                Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
    
                // set scanning settings
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                device.ShowUI = false;
    
                // open the device
                device.Open();
    
                // synchronously acquire image(s) from TWAIN device
                Vintasoft.Twain.AcquireModalState acquireModalState;
                do
                {
                    acquireModalState = device.AcquireModal();
                    switch (acquireModalState)
                    {
                        case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                            // if acquired image is not blank
                            if (!device.AcquiredImage.IsBlank(0.01f))
                                // add the acquired image to a TIFF file
                                device.AcquiredImage.Save(@"d:\test.tif");
    
                            // dispose the acquired image
                            device.AcquiredImage.Dispose();
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                            // output current state
                            System.Console.WriteLine("Scan completed.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                            // output current state
                            System.Console.WriteLine("Scan canceled.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanFailed:
                            // output current state
                            System.Console.WriteLine(string.Format("Scan failed: {0}", device.ErrorString));
                            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 skips blank pages using AcquiredImage.IsBlank method.
    ''' </summary>
    Public Sub SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingIsBlankMethod()
        Try
            Using deviceManager As New Vintasoft.Twain.DeviceManager()
                ' open the device manager
                deviceManager.Open()
    
                ' get reference to the default device
                Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
    
                ' set scanning settings
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device.ShowUI = False
    
                ' open the device
                device.Open()
    
                ' synchronously acquire image(s) from TWAIN device
                Dim acquireModalState As Vintasoft.Twain.AcquireModalState
                Do
                    acquireModalState = device.AcquireModal()
                    Select Case acquireModalState
                        Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                            ' if acquired image is not blank
                            If Not device.AcquiredImage.IsBlank(0.01F) Then
                                ' add the acquired image to a TIFF file
                                device.AcquiredImage.Save("d:\test.tif")
                            End If
    
                            ' dispose the acquired image
                            device.AcquiredImage.Dispose()
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                            ' output current state
                            System.Console.WriteLine("Scan completed.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                            ' output current state
                            System.Console.WriteLine("Scan canceled.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanFailed
                            ' output current state
                            System.Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
    
                ' close the device
                device.Close()
    
                ' close the device manager
                deviceManager.Close()
            End Using
        Catch ex As Vintasoft.Twain.TwainException
            System.Console.WriteLine("Error: " + ex.Message)
        End Try
    
        System.Console.ReadLine()
    End Sub
    


    Преимущества этой техники:
    Недостатки этой техники: