by vensanto » Wed Aug 18, 2010 7:57 am
Hello Dutch
i use this class with windows 7 without problem
--------------
#include "FiveWin.ch"
#define TWAIN_DLL "EZTW32.DLL"
CLASS Scanner
DATA hWnd AS NUMERIC // Handle of the window
DATA hDll AS NUMERIC // Handle of the DLL
DATA lLoad AS LOGICAL // .T. DLL & Driver Loaded
DATA hDib AS NUMERIC // Current Dib handle
METHOD New() CONSTRUCTOR
METHOD End()
METHOD Set() // Acquiring Dialog ON/OFF
METHOD Choose() // Select Image Device Source
METHOD DigiToFile() // Acquire Image and save to a file
METHOD DigiToClip() // Acquire Image and copy to ClipBoard
METHOD SetResolution() // Set Dpi for the scanner
PROTECTED :
METHOD Free() // Release Dib's handle
METHOD IsActive() // Twain Driver Loaded
METHOD Register() // Register my application into Twain application
METHOD DibToFile() // Write to file Dib's handle in BMP format
END CLASS
METHOD New(hWnd)
::hWnd := iif( ValType( hWnd ) == "N" , hWnd , 0 )
::lLoad := .T.
::hDLL := LoadLibrary( TWAIN_DLL )
::hDib := 0
IF ::hDll <= 21
::lLoad := .F.
MsgAlert( BuildError(::hDll) , TWAIN_DLL )
Return Self
Endif
IF ( ::lLoad := ::IsActive() )
::Register()
Endif
Return Self
METHOD End()
IF ::hDib != 0
::Free( ::hDib )
Endif
FreeLibrary( ::hDll )
Return .T.
METHOD DigiToFile(cFILE,nRES)
LOCAL nPixType := 0
LOCAL cFarProc
DEFAULT nRes := 100
::SetResolution( nRes )
IF ::lLoad
cFarProc := GetProcAddress( ::hDLL, "TWAIN_AcquireNative",.T., WORD,WORD,_INT )
::hDib := CallDLL( cFarProc,::hWnd,nPixType )
IF ::hDib == 0
Else
::DibToFile(::hDib,cFile)
::Free( ::hDib )
Endif
Endif
Return Self
METHOD DigiToClip(nRES)
LOCAL nPixType := 0
LOCAL cFarProc
LOCAL nResult
DEFAULT nRes := 100
::SetResolution( nRes )
IF ::lLoad
cFarProc := GetProcAddress( ::hDLL, "TWAIN_AcquireToClipBoard",.T., _INT,WORD,_INT )
nResult := CallDLL( cFarProc,::hWnd,nPixType )
Endif
Return Self
METHOD SetResolution( nDpi )
LOCAL cFarProc
LOCAL uResult
DEFAULT nDpi := 100
IF ::lLoad
cFarProc := GetProcAddress( ::hDLL, "TWAIN_SetResolution",.T., VOID,_DOUBLE )
uResult := CallDLL( cFarProc,nDpi )
Endif
Return Self
METHOD Set(lShow)
LOCAL nHide := 0 // Default: Shows Scanner's Dialog Box
LOCAL cFarProc
LOCAL uResult
DEFAULT lShow := .T.
IF ::lLoad
nHide := IIF(lShow,0,1)
cFarProc := GetProcAddress( ::hDLL, "TWAIN_SetHideUI",.T., VOID,_INT )
uResult := CallDLL( cFarProc,nHide )
Endif
Return uResult
METHOD Choose()
LOCAL cFarProc
LOCAL nResult
IF ::lLoad
cFarProc := GetProcAddress( ::hDLL, "TWAIN_SelectImageSource",.T., _INT,WORD )
nResult := CallDLL( cFarProc,::hWnd )
Endif
Return nResult
//-----------------------------------------
//---------- Protected Methods
// Release Dib's Handle
METHOD Free(hDib)
LOCAL cFarProc
LOCAL uResult
cFarProc := GetProcAddress( ::hDLL, "TWAIN_FreeNative",.T., VOID,WORD )
uResult := CallDLL( cFarProc,hDib )
Return NIL
// Write to File From DIB's handle
METHOD DibToFile(hDib,cFile)
LOCAL cFarProc
LOCAL nResult
LOCAL lRet
cFarProc := GetProcAddress( ::hDLL, "TWAIN_WriteNativeToFilename",.T., _INT,WORD,LPSTR)
lRet := ( (nResult:=CallDLL( cFarProc,hDib,cFile ))==0 )
DO CASE
CASE nResult == -1
MsgInfo("Annullato dall'utente","File non registrato")
CASE nResult == -2
MsgInfo("Errore durante la scrittura sul file "+cFile,"File non registrato")
CASE nResult == -3
MsgInfo("Errore interno sul file DIB","File non registrato")
CASE nResult == -4
MsgInfo("Errore durante la scrittura sul file "+cFile+", probabile spazio insufficiente sul disco !","File non registrato")
ENDCASE
Return lRet
// Is Twain driver loaded ?
METHOD IsActive()
LOCAL cFarProc
LOCAL nResult
cFarProc := GetProcAddress( ::hDLL, "TWAIN_IsAvailable",.T., _INT )
IF !(nResult := CallDLL( cFarProc )) == 1
MsgAlert("Nessun driver per apparecchi TWAIN compatibili risulta disponibile !","Errore hardware")
// Messaggio inviato direttamente da TWAIN.DLL
Endif
Return IIF(nResult==1,.T.,.F.)
// Register my application into Twain application
METHOD Register()
LOCAL nMaiorNum := 1
LOCAL nMinorNum := 0 // Result -> 1.0
LOCAL nLanguage := 0
LOCAL nCountry := 0
LOCAL cVersion := "1.0"
LOCAL cManifact := "The Genius"
LOCAL cFamily := "Digitizer"
LOCAL cProduct := StrTran(cFileName(GetModuleFileName(GetInstance())),".EXE","")
LOCAL cFarProc
LOCAL uResult
cFarProc := GetProcAddress( ::hDLL, "TWAIN_RegisterApp",.T., VOID,_INT,_INT,_INT,_INT,LPSTR,LPSTR,LPSTR,LPSTR )
uResult := CallDLL( cFarProc,nMaiorNum,nMinorNum,nLanguage,nCountry,cVersion,cManifact,cFamily,cProduct )
Return NIL
//---------- END Protected Methods
STATIC FUNCTION BuildError(nError)
LOCAL cRet := "Errore nella libreria dinamica"
DO CASE
CASE nError == 0
cRet := "Memoria insufficiente ad eseguire il programma"
CASE nError == 2
cRet := "File non trovato"
CASE nError == 3
cRet := "Percorso non trovato"
CASE nError == 5
cRet := "Tentantivo di collegarsi dinamicamente ad un task o errore di condivisione"
CASE nError == 6
cRet := "La libreria richiede un segemento separato per ogni task"
CASE nError == 8
cRet := "Memoria insufficiente ad avviare l'applicazione"
CASE nError == 10
cRet := "Versione di MS Windows non corretta"
CASE nError == 11
cRet := "Libreria non valida oppure non Š un'applicazione MS Windows"
CASE nError == 12
cRet := "Applicazione disegnata per un sistema operativo diverso"
CASE nError == 13
cRet := "Applicazione disegnata per MS-DOS 4.0"
CASE nError == 14
cRet := "Tipo di file eseguibile sconosciuto"
CASE nError == 15
cRet := "Tentativo di caricare un'applicazione disegnata per funzionare in modalit… reale"
CASE nError == 16
cRet := "Tentativo di caricare una seconda istanza dell'applicazione contenente segmenti di dati multipli non marcati per la sola lettura"
ENDCASE
Return OemToAnsi( cRet + "!" )
//Declare Function TWAIN_AcquireToClipboard Lib "EZTW32.DLL" Alias "TWAIN_AcquireToClipboard" _
// ( ByVal hwndApp as Long, ByVal wPixTypes as Dword ) as Long
DLL32 FUNCTION TWAIN_AcquireToClipboard (hwndApp As LONG, wPixTypes as CHAR );
AS LONG PASCAL FROM TWAIN_AcquireToClipboard Lib "EZTW32.DLL"
/*
' Like AcquireNative, but puts the resulting image, if any, into the system
' clipboard. Under Windows, this will put a CF_DIB item in the clipboard
' if successful. If this call fails, the clipboard is either empty or
' contains the old contents.
' A return value of 1 indicates success, 0 indicates failure.
'
' Useful for environments like Visual Basic where it is hard to make direct
' use of a DIB handle. In fact, TWAIN_AcquireToClipboard uses
' TWAIN_AcquireNative for all the hard work.
*/