Victor Image Processing Library How-to Tips

Control a TWAIN ADF Scanner

The control of the ADF scanner is straight forward.
In general, the steps are:
In this example we scan as many pages as are in the document feeder, but no more than five. Each image is saved as a separate page in a multipage TIFF file. Hide the TWAIN user interface and with our application we set the scanning conditions.

    Scan Condition     Setting
    pixel type     1-bit black and white
    brightness     0 (midrange)
    contrast     0 (midrange)
    horizontal resolution     200 dpi
    vertical resolution     200 dpi
    area to scan     3 inches wide x 2 inches high

scanwithADF - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5.3 or higher.
' Data type definitions ...............................................
' Image descriptor
Type imgdes
   ibuff As Long
   stx As Long
   sty As Long
   endx As Long
   endy As Long
   buffwidth As Long
   palette As Long
   colors As Long
   imgtype As Long
   bmh As Long
   hBitmap As Long
End Type
    left As Long
    top As Long
    right As Long
    bottom As Long
End Type
' Data for Twain ONEVALUE-type container
   val As Integer          ' the value
End Type
' Data for Twain ENUM-type container
   tarray(17) As Integer
   nelems As Integer       ' Number of valid elements in array()
   currentIndex As Integer ' Index to the value that is currently in effect
   defaultIndex As Integer ' Power-up value
End Type
' Data for Twain RANGE-type container
   min As Integer          ' Starting value in the range
   max As Integer          ' Final value in the range
   stepSize As Integer     ' Increment from min to max
   currentVal As Integer   ' The value that is currently in effect
   defaultVal As Integer   ' Power-up value
End Type
' Capability get/set struct
   conType As Integer    '  Container type, TWON_ONEVALUE, TWON_ENUMERATION, or TWON_RANGE,
   oneValue As TWAIN_ONEVALUE   ' Data if using ONEVALUE-type container
   enumType As TWAIN_ENUMTYPE   ' Data if using ENUM-type container
   range As TWAIN_RANGE         ' Data if using RANGE-type container
End Type
Global Const TWUN_INCHES = 0
Global Const TWON_ARRAY = 3
Global Const TWON_ONEVALUE = 5
Global Const TWON_RANGE = 6
' Pixel type constants 
Global Const TWPT_BW = 0
Global Const TWPT_GRAY = 1
Global Const TWPT_RGB = 2
' Global variable for counting the pages scanned 
Global pageno As Integer
' Function Declarations ........................................... 
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function savetifpage Lib "VIC32.DLL" (ByVal fname As String, srcimg As imgdes, ByVal cmp As Long, ByVal page As Long) As Long
Declare Sub TWclose Lib "VICTW32.DLL" ()
Declare Function TWgetfeeder Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef feederIsEnabled As Long, ByRef feederHasPaper As Long) As Long
Declare Function TWopen Lib "VICTW32.DLL" (ByVal hWnd As Long) As Long
Declare Function TWscancountimages Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef desimg As imgdes, ByRef pRect As RECT, ByVal showIU As Long, ByVal maxPages As Long, ByVal saveScan As Long) As Long
Declare Function TWsetbrightness Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef brightness As TWAIN_CAP_DATA) As Long
Declare Function TWsetcontrast Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef contrast As TWAIN_CAP_DATA) As Long
Declare Function TWsetfeeder Lib "VICTW32.DLL" (ByVal hWnd As Long, ByVal enableFeeder As Long) As Long
Declare Function TWsetmeasureunit Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef typeUnit As TWAIN_CAP_DATA) As Long
Declare Function TWsetpixeltype Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef pixelType As TWAIN_CAP_DATA) As Long
Declare Function TWsetxresolution Lib "VICTW32.DLL" (ByVal hWnd As Long, xres As TWAIN_CAP_DATA) As Long
Declare Function TWsetyresolution Lib "VICTW32.DLL" (ByVal hWnd As Long, yres As TWAIN_CAP_DATA) As Long
' The Function to control the TWAIN ADF scanner ................... 
Public Function scanwithADF(hWnd As Long) As Long
Dim rcode As Long
Dim gsimage As imgdes
Dim srect As RECT
Dim showUI As Long
Dim maxpages As Long
Dim unit_data As TWAIN_CAP_DATA
Dim reso_data As TWAIN_CAP_DATA
Dim pixel_data As TWAIN_CAP_DATA
Dim bright_data As TWAIN_CAP_DATA
Dim feederIsEnabled As Long
Dim feederHasPaper As Long
rcode = TWopen(hWnd)
If (rcode = NO_ERROR) Then
    unit_data.oneValue.val = TWUN_INCHES
    unit_data.conType = TWON_ONEVALUE
    reso_data.oneValue.val = 200  ' 200 dpi
    reso_data.conType = TWON_ONEVALUE
        'pixel_data.oneValue.val = TWPT_GRAY  'Grayscale
    pixel_data.oneValue.val = TWPT_BW  '1-bit b/w
    pixel_data.conType = TWON_ONEVALUE
    bright_data.oneValue.val = 0  ' Range is usually -1000 to 1000
    bright_data.conType = TWON_ONEVALUE
    rcode = TWsetmeasureunit(hWnd, unit_data)    ' To set the device units
    rcode = TWsetxresolution(hWnd, reso_data)    ' To set the device resolution
    rcode = TWsetyresolution(hWnd, reso_data)    ' To set the device resolution
    rcode = TWsetbrightness(hWnd, bright_data)   ' To set brightness
    rcode = TWsetcontrast(hWnd, bright_data)     ' To set contrast
    rcode = TWsetpixeltype(hWnd, pixel_data)     ' To set the pixel type
    srect.left = 0      ' Scan bed coordinates in 1000th inch = 0
    srect.right = 3000  ' 3 inches wide
    srect.bottom = 2000 ' 2 inch high
    showUI = 0      ' Don't show User Interface
    pageno = 0      ' Global variable to count pages scanned and saved
    maxpages = 5    ' Don't scan any more than 5 pages
    feederIsEnabled = 1  ' Enable automatic document feeder
    rcode = TWsetfeeder(hWnd, feederIsEnabled)
    rcode = TWgetfeeder(hWnd, feederIsEnabled, feederHasPaper)
    If (feederIsEnabled <> 0) Then
        If (feederHasPaper <> 0) Then
            rcode = TWscancountimages(hWnd, gsimage, srect, showUI, maxpages, AddressOf savescanedpagetotiff)
        End If
    End If
End If
scanwithADF = rcode
End Function
' The Function to save each image into a multipage TIFF file ...... 
Public Function savescanedpagetotiff(jimage As imgdes) As Long
' TWscancountimages callback function
Dim rcode As Long
Dim Filename As String
' Save the image
Filename = "testadf.tif"
rcode = savetifpage(Filename, jimage, 0, pageno)  ' pageno is global variable of type long
If (rcode = NO_ERROR) Then
    pageno = pageno + 1
End If
freeimage jimage    ' Done with image, release the memory
savescanedpagetotiff = rcode
End Function
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code

Copyright © 1995 - 2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.