⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagescanforgood.cpp

📁 图像扫描类的完整代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
完整的图像扫描类 
 
 
 
发布时间:2007-11-19 23:37:47.0   浏览人数:377 
 
// ctwain.h - application interface to TWAIN Protocol
//
#ifndef CTWAIN_H
#define CTWAIN_H



#include "twain.h"



//结构和用法:
// CTwain 最好作为 mainframe的第二个基类来使用,
// 这样,回调比较方便,程序也比较紧凑。
//
// RegisterApp 可以填入真实信息。
//
// 0. If !TwainAvailable(), 无twain,菜单被禁止,无须其他处理。
// 1. 在main frame window 建立后(on create),调用SetDefWindow(this), 
// 和 OpenSourceManager()。如果 OpenSourceManager() 失败,你可以
// 提示用户twain 出错(这需要你附加代码).
// 2. 当State() == SOURCE_MANAGER_OPEN 时,使 Acquire and Select Source 
// 命令可用。
// 3. 在 Acquire 命令响应中,调用 BeginAcquire.
// 4. Override CWnd::PreTranslateMessage(pMsg),加入:
// return (TwainMessageHook(pMsg) ||
// <base class>::PreTranslateMessage(pMsg);
// 5. Override CTwain::DibReceived 处理native transfers返回的dib句柄。
// 注意:在调用BeginAcquire后, 你可以传输多幅图象,data source 
// 始终打开着,若要关闭,调用 CloseSource.
// 可待扩展:Add support for Memory and File transfer(可能不标准).




typedef enum {
NO_TWAIN_STATE, // 0 internal use only
PRE_SESSION, // 1 ground state, nothing loaded
SOURCE_MANAGER_LOADED, // 2 DSM loaded but not open
SOURCE_MANAGER_OPEN, // 3 DSM open
SOURCE_OPEN, // 4 some Source open - Negotiation state!
SOURCE_ENABLED, // 5 acquisition started
TRANSFER_READY, // 6 data ready to transfer
TRANSFERRING // 7 transfer started
} TW_STATE;



typedef enum {
TWERR_OPEN_DSM, // unable to load or open Source Manager
TWERR_OPEN_SOURCE, // unable to open Datasource
TWERR_ENABLE_SOURCE, // unable to enable Datasource
} TW_ERR;



class CTwain
{
public:



BOOL SetPixelType(TW_UINT32 pxlType);
CTwain(void);
~CTwain();



// General notes:
// 1. 一旦source manager 被装载, 直到对象被释放才被卸载。
// (或UnloadSourceManager)
// 第一次调用TwainAvailable时装载 source manager.



// attributes i.e. query functions
int TwainAvailable(void);
// TRUE ,如果 TWAIN Datasource Manager 存在并被装载。
// 不检查是否有 datasources!
// 可用IsAvailable 来 enable or disable menu items .



TW_STATE State(void);
// Return the current (presumed) state of the Twain connection.



unsigned ResultCode(void);
// last result code (see twain.h)



unsigned ConditionCode(void);
// retrieve condition code from last triplet - see twain.h.



// Top-level operations
void SetDefWindow(CWnd* pWnd);
//def window 是主窗口。
// If no default window is set, AfxGetMainWnd() is used.



int SelectSource(CWnd* pWnd = NULL);
// Post the standard Select Source dialog



void ModalAcquire(CWnd* pWnd = NULL);
// Acquire images from current or default source.
// By default, displays the u/i of the source and acquires images
// until a CLOSEDSREQ is received from the source.
// Acquire returns the Twain connection to the starting state
// or to SOURCE_ENABLED, whichever is lower.



int BeginAcquire(CWnd* pWnd = NULL);
// Open and enable the default source.
// TRUE if successful, FALSE if something goes wrong.
// If successful, State() == SOURCE_ENABLED.
// You must now pass all messages to MessageHook until the state
// drops below SOURCE_OPEN: In MFC, override PreTranslateMessage.
// If BeginAcquire fails, it returns TWAIN to the state it
// found it in.



void RegisterApp( // Record application information
int nMajorNum, int nMinorNum, // major and incremental revision of application. E.g.
// for version 2.1, nMajorNum == 2 and nMinorNum == 1
int nLanguage, // language of this version (use TWLG_xxx from TWAIN.H)
int nCountry, // country of this version (use TWCY_xxx from TWAIN.H)
LPSTR lpszVersion, // version info string e.g. "1.0b3 Beta release"
LPSTR lpszMfg, // name of manufacturer/developer e.g. "Crazbat Software"
LPSTR lpszFamily, // product family e.g. "BitStomper"
LPSTR lpszProduct); // specific product e.g. "BitStomper Deluxe Pro"
// This is not necessary for acquisition, but is required for full
// TWAIN compliance.



// 内部操作:
int OpenSourceManager(CWnd* pWnd = NULL);
// (Load and) open the Datasource Manager
// Loads the DSM if necessary, opens it if necessary.
// If State >= 3, does nothing and returns TRUE.



int OpenDefaultSource(void);
// Open the default datasource (last source selected in Select Source dialog.)
// Invalid (returns FALSE) if State > 3 (a source is already open)
// Invalid (returns FALSE) if State < 3 (DSM is not open)
// If successful, returns TRUE with State == 4.
// Otherwise, returns FALSE with State unchanged.



void SetShowUI(BOOL bShow = 1);
// Set flag for whether source should be enabled with
// user interface visible (bShow == TRUE) or not.
// At construction, ShowUI is set TRUE.



BOOL GetShowUI(void);
// Return state of ShowUI flag.



int EnableSource(CWnd* pWnd = NULL);
// Enable the open source, which allows image acquisition to begin.
// Invalid if State != 4 (source open).
// If successful, returns TRUE with State == 5.
// Otherwise, returns FALSE with State unchanged.



int DisableSource(void);
// Disable the current source.
// If State == 5, disables the current source and returns TRUE.
// If State < 5, does nothing and returns TRUE.
// If State > 5, does nothing and returns FALSE.



int CloseSource(void);
int CloseSourceManager(CWnd* pWnd = NULL);
int UnloadSourceManager(void);



int DropToState(int nS, CWnd* pWnd = NULL);



// 底层操作
int LoadSourceManager(void);
// loads the DSM (Datasource Manager) into process space



void ModalEventLoop(void);
// get and dispatch messages until source is disabled.



int TwainMessageHook(LPMSG lpmsg);



int EndXfer(void);
// In State 7, ends the current transfer by sending MSG_ENDXFER.
// If successful, goes to State 6 if there are more transfers
// available (pendingXfers != 0), or to State 5 if not.
// Returns TRUE if the resulting State < 7.



int CancelXfers(void);
// In State 6, cancels any pending transfers.
// (In State 7, does an EndXfer first)
// If successful, goes to State 5.
// Returns TRUE if the resulting State < 6.



// DS,SM
int DS(unsigned long dg, unsigned dat, unsigned msg, void FAR *pd);
// send a triplet to the current Datasource.
// returns TRUE if the result code (rc) == RC_SUCCESS, FALSE otherwise.
// Note that this is not meaningful with some triplets.
// Does ASSERT(nState < 4);



int SM(unsigned long dg, unsigned dat, unsigned msg, void FAR *pd);
// Send a triplet to the Source Manager.
// returns TRUE if the result code (rc) == RC_SUCCESS, FALSE otherwise.
// Note that this is not meaningful with some triplets.
// Does ASSERT(nState > 1);



// 回调函数
virtual void XferReady(LPMSG lpmsg);
// called when source has one or more xfers ready
// This default method assumes XferMech = Native,
// gets each native (DIB) image and calls back to DibReceived.



virtual void CloseDsRequest(void);
// called when the open source asks to be 'closed'
// It is sufficient to disable the source on this request,
// but this default handler calls CloseSource.



virtual void DibReceived(HGLOBAL hDib);
// called by the default XferReady handler
// when it has successfully transferred a DIB.
// This default handler just calls GlobalFree(hDib).



virtual void StateChange(int nState);
// called after each Twain State transition.
// nState is the new State. When this callback
// occurs, the state transition has already happened.
// Note - first call is the transition to State 1
// which occurs at construction.



virtual void TwainError(TW_ERR e);
// Called when an unexpected TWAIN malfunction occurs.
// See TW_ERR declaration at beginning of this file.



protected:



void SetState(TW_STATE nS); // assume Twain in State nS



CWnd* DefWnd(CWnd* pWnd = NULL);
// Returns pWnd if it's non-null, otherwise it
// finds a safe non-null default substitute.



BOOL bTrace; // enable TRACE output
TW_STATE nState; // current state
TW_STATE nStartState; // starting state for some operation
TW_IDENTITY AppId; // application identity structure
TW_INT16 rc; // last result code 
TW_USERINTERFACE twUI;
BOOL bShowUI;
TW_PENDINGXFERS pendingXfers;
HANDLE hDib; // bitmap returned by native transfer
CWnd* pDefWnd; // default window
};



#endif




// ctwain.cpp - implementation of a mfc TWAIN class



//进行扫描处理的类 



#include "stdafx.h"
#include "ctwain.h"



#ifdef _DEBUG

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -