Victor Image Processing Library How-to Tips

Control a TWAIN device with Visual Basic

The Victor Library TWAIN Support Module is very easy to use from Visual Basic. The simplest implementation is to scan a single image using the device's user interface . . .
     Dim new_image as imgdes    ' Image descriptor
     Dim rcode as long          ' Return code

        rcode = TWscanimage(hWnd, new_image)

And to save the image in a TIFF file . . .


        rcode = savetif("newimage.tif", new_image, 0)

In addition to being easy, the Victor Library control of the TWAIN device is also powerful. You can hide the user interface, control the scan conditions, and capture multiple images.

The example shown here sets the device resolution and capture area then scans up to three images from a scanner with automatic document feeder or from a digital camera and saves the images in TIFF files. All of the available capture functions are shown, (but are in comment statements), and can easily be implemented.

Capturing multiple images requires a callback function, that part of the example uses the VB AddressOf operator available in VB v 5 or greater.

Capture an Image - the Source Code for VB

Requires Victor Image Processing Library for 32-bit Windows v 5.1 or higher.
............ Add the do_the_scans function to your .BAS module, not to a .FRM module ...........

Public Function do_the_scans(hWnd As Long, gsimage As imgdes) As Long
Dim srect As RECT
Dim showUI As Long
Dim rcode 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

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
srect.top = 0
srect.right = 3000  ' 3 inches wide
srect.bottom = 1000 ' 1 inch high

showUI = 0      ' Don't show User Interface
pageno = 0

' To invoke the device user interface and capture a single image
'do_the_scans = TWscanimage(hWnd, gsimage)

' To hide the user interface and capture a single image
'do_the_scans = TWscanimageex(hWnd, gsimage, srect, showUI)

' To invoke the user interface and capture multiple images
'do_the_scans = TWscanmultipleimages(hWnd, gsimage, AddressOf saveScannedImage)

' To hide the user interface and capture multiple images
do_the_scans = TWscanmultipleimagesex(hWnd, gsimage, srect, showUI, AddressOf saveScannedImage)

End Function

............ Add the saveScannedImage function to your .BAS module, not to a .FRM module ...........

Public Function saveScannedImage(jimage As imgdes) As Long
' TWscanmultipleimages() callback function
Dim rcode As Long
Dim Filename As String

' New name for each image captured
Filename = LCase$("test" & pageno & ".tif")
pageno = pageno + 1

'    To save all the pages in a single multipage TIFF file
'    use the savetifpage function instead of savetif
    
' Save the image
rcode = savetif(Filename, jimage, 0)

' Stop after 3 pages, for this demonstration
If (pageno = 3) Then
    rcode = TWAIN_STOP_SCAN
Else
    rcode = NO_ERROR
End If

saveScannedImage = rcode
End Function

............ Add this menu selection handler to your .FRM ..................

Private Sub mnuAcquire_Click()
Dim rcode As Long, j As Long
Dim gsimage As imgdes

    On Error Resume Next

    Screen.MousePointer = 11   ' Change mouse pointer to hourglass
   ' Store App name to display in Source dialog box. String may contain up to 32 chars.
   TWsetproductname ("Victor Library")
   ' Acquire image(s) from selected/default source
   rcode = do_the_scans(hWnd, gsimage) ' This function has to reside in a .BAS module
   
   ' Last image captured becomes the one to display
   If (rcode = NO_ERROR) Then
        ' Success, free previous image
        freeimage vimage
        ' Assign temp image to vimage
        copyimgdes gsimage, vimage
        MainWnd.BackColor = &HFFFFFF  ' Erase previous image
        ' Repaint the screen to display the new image
        MainWnd.form_paint
  End If
  Screen.MousePointer = 0   ' Reset the mouse pointer.

    ' Handle any errors
    If rcode <> NO_ERROR Then
        MainWnd.error_handler rcode, ""
    End If
End Sub

........... Add these defines and declarations to your Global module ...........

' 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

Type RECT
    left As Long
    top As Long
    right As Long
    bottom As Long
End Type

' Global variables for this example
Global vimage As imgdes
Global pageno As Integer

Type TW_STR32
    items(33) As Byte ' Actually creates a 34-byte array
End Type

' Capability get/set struct
' Data for Twain ONEVALUE-type container 
Type TWAIN_ONEVALUE
   val As Integer          ' the value
End Type

' Data for Twain ENUM-type container
Type TWAIN_ENUMTYPE
   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
Type TWAIN_RANGE
   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
Type TWAIN_CAP_DATA
   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

' Container constants (4)
Global Const TWON_ARRAY = 3
Global Const TWON_ENUMERATION = 4
Global Const TWON_ONEVALUE = 5
Global Const TWON_RANGE = 6

' Pixel type constants (9)
Global Const TWPT_BW = 0
Global Const TWPT_GRAY = 1
Global Const TWPT_RGB = 2
Global Const TWPT_PALETTE = 3
Global Const TWPT_CMY = 4
Global Const TWPT_CMYK = 5
Global Const TWPT_YUV = 6
Global Const TWPT_YUVK = 7
Global Const TWPT_CIEXYZ = 8

' Units constants (6)
Global Const TWUN_INCHES = 0
Global Const TWUN_CENTIMETERS = 1
Global Const TWUN_PICAS = 2
Global Const TWUN_POINTS = 3
Global Const TWUN_TWIPS = 4
Global Const TWUN_PIXELS = 5

Global Const TWAIN_STOP_SCAN = -67

Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Sub copyimgdes Lib "VIC32.DLL" (srcimg As imgdes, desimg As imgdes)
Declare Function savetif Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal cmp As Long) As Long
Declare Function savetifpage Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal cmp As Long, ByVal page As Long) As Long

Declare Function TWdetecttwain Lib "VICTW32.DLL" (ByVal hWnd As Long) As Long
Declare Function TWgeterror Lib "VICTW32.DLL" () As Long
Declare Function TWgetmeasureunit Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef typeUnit As TWAIN_CAP_DATA) As Long
Declare Function TWgetpixeltype Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef pixelType As TWAIN_CAP_DATA) As Long
Declare Function TWgetbrightness Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef brightness As TWAIN_CAP_DATA) As Long
Declare Function TWgetcontrast Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef contrast As TWAIN_CAP_DATA) As Long
Declare Function TWgetsourcenames Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef namelist As TW_STR32, ByRef nameCount As Long) As Long
Declare Function TWgetsourcencount Lib "VICTW32.DLL" Alias "TWgetsourcenames" (ByVal hWnd As Long, ByVal nullval As Long, ByRef nameCount As Long) As Long
Declare Function TWgetxresolution Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef xres As TWAIN_CAP_DATA) As Long
Declare Function TWgetyresolution Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef yres As TWAIN_CAP_DATA) As Long
Declare Function TWscanimage Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef desimg As imgdes) As Long
Declare Function TWscanimageex Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef desimg As imgdes, pRect As RECT, ByVal showIU As Long) As Long
Declare Function TWscanmultipleimages Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef desimg As imgdes, ByVal saveScan As Long) As Long
Declare Function TWscanmultipleimagesex Lib "VICTW32.DLL" (ByVal hWnd As Long, ByRef desimg As imgdes, ByRef pRect As RECT, ByVal showIU As Long, ByVal saveScan As Long) As Long
Declare Function TWselectsource Lib "VICTW32.DLL" (ByVal hWnd As Long) As Long
Declare Function TWselectsourcebyname Lib "VICTW32.DLL" (ByVal hWnd As Long, ByVal dsname As String) 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 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
Declare Sub TWsetproductname Lib "VICTW32.DLL" (ByVal prodName As String)
Declare Function TWvicversion Lib "VICTW32.DLL" () As Integer
Victor Image Processing Library

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




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