Official Website:http://www.twain.org
一、简介
二、使用步骤
1. 打开 DSM (Data Source Manager: 数据源管理器)
2. 选择 DS (Data Source: 数据源)
3. 打开 DS
4. 设置参数
5. 显示扫描界面
6. 获取图像
7. 关闭扫描界面
8. 关闭 DS
9. 关闭数据源
三、CTwainHelper 助手类
- 调用 CTwainHelper::Initialize() 确定是否有可用的设备。
- 在窗口消息循环中,调用 CTwainHelper::ProcessMessage() 处理 TWAIN 消息。
- 要获取图像时,调用 CTwainHelper::GetImage()。
- 如果图像已准备好 (如用户确定扫描图像),窗口会收到 WM_COMMAND 消息,wParam 为 IDC_TwainHelper。此时应用程序可以调用 CTwainHelper::TransferImage() 获取图像到文件中。
四、后话
Open the Source Manager (State 2 to 3)
Select the Source (during State 3)
Open the Source (State 3 to 4)
Negotiate Capabilities with the Source (during State 4)
Request the Acquisition of Data from the Source (State 4 to 5)
Recognize that the Data Transfer is Ready (State 5 to 6)
Start and Perform the Transfer (State 6 to 7)
Conclude the Transfer (State 7 to 6 to 5)
Disconnect the TWAIN Session (State 5 to 1 in sequence)
VC++ Call Twain
Header
#ifndef __TWAINCPP_ #define __TWAINCPP_ #include "Twain.h" #define TWCPP_ANYCOUNT (-1) #define TWCPP_CANCELTHIS (1) #define TWCPP_CANCELALL (2) #define TWCPP_DOTRANSFER (0) class CTwain { public: void Deskew(LPSTR lpFilename); CTwain(HWND hWnd = NULL); virtual ~CTwain(); BOOL InitTwain(HWND hWnd); void ReleaseTwain(); /* This routine must be implemented by the dervied class After setting the required values in the m_AppId structure, the derived class should call the parent class implementation Refer Pg: 51 of the Twain Specification version 1.8 */ virtual void GetIdentity(); virtual BOOL SelectSource(); virtual BOOL OpenSource(TW_IDENTITY *pSource=NULL); virtual int ShouldTransfer(TW_IMAGEINFO& info) { return TWCPP_DOTRANSFER;}; void SetPara(int nFileFormat,LPCTSTR lpFileName,int nAD,int nPage,int nStep,BOOL bDeskdw); void GetPara(UINT &nPage); BOOL ProcessMessage(MSG msg); BOOL SelectDefaultSource(); BOOL IsValidDriver() const; BOOL SourceSelected() const {return m_bSourceSelected;} ; BOOL DSMOpen() const; BOOL DSOpen() const; BOOL SourceEnabled() const { return m_bSourceEnabled;}; BOOL ModalUI() const { return m_bModalUI; }; TW_INT16 GetRC() const { return m_returnCode; }; TW_STATUS GetStatus() const { return m_Status; }; BOOL SetImageCount(TW_INT16 nCount = 1); BOOL Acquire(int numImages=1); void DoFileTransfer(); void CloseDSM(); protected: BOOL CallTwainProc(pTW_IDENTITY pOrigin,pTW_IDENTITY pDest, TW_UINT32 DG,TW_UINT16 DAT,TW_UINT16 MSG, TW_MEMREF pData); void CloseDS(); BOOL GetCapability(TW_CAPABILITY& twCap,TW_UINT16 cap,TW_UINT16 conType=TWON_DONTCARE16); BOOL GetCapability(TW_UINT16 cap,TW_UINT32& value); BOOL SetCapability(TW_UINT16 cap,TW_UINT16 value,BOOL sign=FALSE); BOOL SetCapability(TW_CAPABILITY& twCap); BOOL EnableSource(BOOL showUI = TRUE); BOOL GetImageInfo(TW_IMAGEINFO& info); virtual BOOL DisableSource(); virtual BOOL CanClose() { return TRUE; }; void TranslateMessage(TW_EVENT& twEvent); void TransferImage(); BOOL EndTransfer(); void CancelTransfer(); BOOL ShouldContinue(); BOOL GetImage(TW_IMAGEINFO& info); // virtual void CopyImage(HANDLE hBitmap,TW_IMAGEINFO& info)=0; protected: HINSTANCE m_hTwainDLL; DSMENTRYPROC m_pDSMProc; TW_IDENTITY m_AppId; TW_IDENTITY m_Source; TW_STATUS m_Status; TW_INT16 m_returnCode; HWND m_hMessageWnd; BOOL m_bSourceSelected; BOOL m_bDSMOpen; BOOL m_bDSOpen; BOOL m_bSourceEnabled; BOOL m_bModalUI;
Cpp
#include "stdafx.h" #include "twaincpp.h" #include "showpic.h" #include "tiff2pdfdll.h" CTwain::CTwain(HWND hWnd) { m_hTwainDLL = NULL; m_pDSMProc = NULL; m_bSourceSelected = FALSE; m_bDSOpen = m_bDSMOpen = FALSE; m_bSourceEnabled = FALSE; m_bModalUI = TRUE; m_nImageCount = TWCPP_ANYCOUNT; if(hWnd) { InitTwain(hWnd); } } CTwain::~CTwain() { ReleaseTwain(); } /* 初始化TWAIN 接口. 构造函数中已经调用,不过如果调用了ReleaseTwain的话就需要再次调用. hWnd是接受Twain消息的窗口句柄,通常应该是主应用程序的窗口句柄 */ BOOL CTwain::InitTwain(HWND hWnd) { char libName[512]; if(IsValidDriver()) { return TRUE; } memset(&m_AppId,0,sizeof(m_AppId)); if(!IsWindow(hWnd)) { return FALSE; } m_hMessageWnd = hWnd; strcpy(libName,"TWAIN_32.DLL"); m_hTwainDLL = LoadLibrary(libName); if(m_hTwainDLL != NULL) { if(!(m_pDSMProc = (DSMENTRYPROC)GetProcAddress(m_hTwainDLL,MAKEINTRESOURCE(1)))) { FreeLibrary(m_hTwainDLL); m_hTwainDLL = NULL; } } if(IsValidDriver()) { GetIdentity(); m_bDSMOpen= CallTwainProc(&m_AppId,NULL,DG_CONTROL,DAT_PARENT,MSG_OPENDSM,(TW_MEMREF)&m_hMessageWnd); return TRUE; } else { return FALSE; } } /* 释放Twain接口,除非不再使用Twain接口,否则就无需调用 */ void CTwain::ReleaseTwain() { if(IsValidDriver()) { CloseDSM(); FreeLibrary(m_hTwainDLL); m_hTwainDLL = NULL; m_pDSMProc = NULL; } } /* 如果正确的装载了驱动程序,就返回TRUE */ BOOL CTwain::IsValidDriver() const { return (m_hTwainDLL && m_pDSMProc); } BOOL CTwain::CallTwainProc(pTW_IDENTITY pOrigin,pTW_IDENTITY pDest, TW_UINT32 DG,TW_UINT16 DAT,TW_UINT16 MSG, TW_MEMREF pData) { if(IsValidDriver()) { USHORT ret_val; ret_val = (*m_pDSMProc)(pOrigin,pDest,DG,DAT,MSG,pData); m_returnCode = ret_val; if(ret_val != TWRC_SUCCESS) { (*m_pDSMProc)(pOrigin,pDest,DG_CONTROL,DAT_STATUS,MSG_GET,&m_Status); } return (ret_val == TWRC_SUCCESS); } else { m_returnCode = TWRC_FAILURE; return FALSE; } } /* 设置Twain的一些入口参数 */ void CTwain::GetIdentity() { // Expects all the fields in m_AppId to be set except for the id field. m_AppId.Id = 0; // Initialize to 0 (Source Manager m_AppId.Version.MajorNum = 1; //Your app's version number m_AppId.Version.MinorNum = 5; m_AppId.Version.Language = TWLG_USA; m_AppId.Version.Country = TWCY_USA; strcpy (m_AppId.Version.Info, "3.5"); m_AppId.ProtocolMajor = TWON_PROTOCOLMAJOR; m_AppId.ProtocolMinor = TWON_PROTOCOLMINOR; m_AppId.SupportedGroups = DG_IMAGE | DG_CONTROL; strcpy (m_AppId.Manufacturer, "MICSS"); strcpy (m_AppId.ProductFamily, "Generic"); strcpy (m_AppId.ProductName, "Twain Test"); } /* 调用此函数显示选择扫描设备窗口 */ BOOL CTwain::SelectSource() { memset(&m_Source,0,sizeof(m_Source)); if(!SourceSelected()) { SelectDefaultSource(); } if(CallTwainProc(&m_AppId,NULL,DG_CONTROL,DAT_IDENTITY,MSG_USERSELECT,&m_Source)) { m_bSourceSelected = TRUE; } return m_bSourceSelected; } /* 用于选择缺省扫描设备 */ BOOL CTwain::SelectDefaultSource() { m_bSourceSelected = CallTwainProc(&m_AppId,NULL,DG_CONTROL,DAT_IDENTITY,MSG_GETDEFAULT,&m_Source); return m_bSourceSelected; } /* 关闭数据源(即扫描仪) */ void CTwain::CloseDS() { if(DSOpen()) { DisableSource(); CallTwainProc(&m_AppId,NULL,DG_CONTROL,DAT_IDENTITY,MSG_CLOSEDS,(TW_MEMREF)&m_Source); m_bDSOpen = FALSE; } } /* 关闭数据源管理器 */ void CTwain::CloseDSM() { if(DSMOpen()) { CloseDS(); CallTwainProc(&m_AppId,NULL,DG_CONTROL,DAT_PARENT,MSG_CLOSEDSM,(TW_MEMREF)&m