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.
This example sets a scan resolution of 200 dpi, but this can be changed and the horizontal and vertical resolutions may use different values if necessary.
To scan and save, the steps are:
This example sets all parameters and automatically scans and saves. To invoke the user interface either set showUI to TRUE or use the TWscanimage function (commented out).
This is a stand alone function that will initialize the scanner each time it is called. If your app will scan more than one page then for faster scanning use
Public Function TWscan_and_save(fname As String) As Long Dim image1 As imgdes Dim rcode As Long Dim compression As Long Dim showUI As Long Dim pixel_data As TWAIN_CAP_DATA Dim reso_data As TWAIN_CAP_DATA Dim brightness As TWAIN_CAP_DATA Dim contrast As TWAIN_CAP_DATA Dim unit_data As TWAIN_CAP_DATA Dim srect As RECT rcode = TWopen(hWnd) If (rcode <> NO_ERROR) Then TWscan_and_save = rcode Return End If unit_data.oneValue.val = TWUN_INCHES unit_data.conType = TWON_ONEVALUE reso_data.oneValue.val = 150 ' 150 dpi reso_data.conType = TWON_ONEVALUE pixel_data.oneValue.val = TWPT_BW '1-bit b/w pixel_data.conType = TWON_ONEVALUE brightness.oneValue.val = 0 ' Range is usually -1000 to 1000 brightness.conType = TWON_ONEVALUE contrast.oneValue.val = 0 'Range is usually -1000 to 1000 contrast.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, brightness) ' Set brightness rcode = TWsetcontrast(hWnd, contrast) ' 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 = 8000 ' 8 inches wide srect.bottom = 10000 ' 10 inches high showUI = 0 ' Don't show User Interface pageno = 0 ' To hide the user interface and capture a single image rcode = TWscanimageex(hWnd, image1, srect, showUI) If (rcode = NO_ERROR) Then rcode = savetif(fname, image1, compression) freeimage image1 End If TWclose TWscan_and_save = rcode End Function ........... 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 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 Function savetif Lib "VIC32.DLL" (ByVal Fname As String, srcimg As imgdes, ByVal cmp As Long) As Long Declare Function TWopen Lib "VICTW32.DLL" (ByVal hWnd As Long) As Long Declare Sub TWclose Lib "VICTW32.DLL" () 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
// Scan and save a document as a TIFF image int scanTW_and_save(char far *fname) { imgdes image1; int rcode; int compression = 0; int showUI = FALSE; // Hide user interface TWAIN_CAP_DATA pixeltype; TWAIN_CAP_DATA brightness; TWAIN_CAP_DATA contrast; TWAIN_CAP_DATA resolution; TWAIN_CAP_DATA resunits; HWND hwnd; RECT scanrect; hwnd = GetActiveWindow(); if(rcode = TWopen(hwnd) != NO_ERROR) return (rcode); resunits.oneValue.val = TWUN_INCHES; resunits.conType = TWON_ONEVALUE; resolution.oneValue.val = 200; // 200 dpi resolution.conType = TWON_ONEVALUE; // For grayscale use: pixeltype.oneValue.val = TWPT_GRAY; pixeltype.oneValue.val = TWPT_BW; 1-bit b/w pixeltype.conType = TWON_ONEVALUE; brightness.oneValue.val = 0; // Range is usually -1000 to 1000 brightness.conType = TWON_ONEVALUE; contrast.oneValue.val = 0; // Range is usually -1000 to 1000 contrast.conType = TWON_ONEVALUE; // We're setting parameters here, but the scanner may choose to ignore our settings TWsetmeasureunit(hwnd, &resunits); // Set the device units TWsetxresolution(hwnd, &resolution); // Set the device resolution TWsetyresolution(hwnd, &resolution); // Set the device resolution TWsetbrightness(hwnd, &brightness); // Set brightness TWsetcontrast(hwnd, &contrast); // Set contrast TWsetpixeltype(hwnd, &pixeltype); // Set the pixel type scanrect.left = 0; // Scan bed coordinates in 1000th inch scanrect.top = 0; scanrect.right = 8000; // 8 inches wide scanrect.bottom = 10000; // 10 inches high // rcode = TWscanimage(hwnd, &image1); // For TWAIN user interface rcode = TWscanimageex(hwnd, &image1, &scanrect, showUI); // No user interface if(rcode == NO_ERROR) { rcode = savetif(fname, &image1, compression); } freeimage(&image1); TWclose(); return(rcode); }
Victor Image Processing Library homepage | Victor Product Summary | more source code