123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- #include "StdAfx.h"
- #include "SeparateThreadTwainFeeder.h"
- CSeparateThreadTwainFeeder::CSeparateThreadTwainFeeder(void)
- :m_current_task_type(STTF_TASK_TYPE_DEFAULT)
- ,m_connect_task_excute_state(STTF_TASK_EXCUTE_STATE_NOTSTART)
- ,m_hWnd(NULL)
- {
- InitializeCriticalSection(&m_lock);
- m_img =NULL;
- }
- CSeparateThreadTwainFeeder::~CSeparateThreadTwainFeeder(void)
- {
- DeleteCriticalSection(&m_lock);
- }
- ServiceState CSeparateThreadTwainFeeder::OnStarting( void )
- {
- EnterCriticalSection(&m_lock);
- if(m_current_task_type == STTF_TASK_TYPE_CONNECT){//扫描仪连接任务
- while(m_current_connect_task_count>0){
- int nId = m_current_connect_task[m_current_connect_task_first];
- LeaveCriticalSection(&m_lock);
- if(GetDSMState()>3&&nId != getDataSource()->Id){
- unloadDS();
- loadDS(nId);
- }
- if(GetDSMState()<3){
- connectDSM();
- }
- if(GetDSMState()<4){
- loadDS(nId);
- }
- if(m_hWnd!=NULL)PostMessage(m_hWnd,WM_IDENTIFOR_SANNER_CONNECT,nId,GetDSMState()>=4?TRUE:FALSE);
- EnterCriticalSection(&m_lock);
- m_current_connect_task_first =(m_current_connect_task_first+1)%MAX_CONNET_TASK_COUNT;
- m_current_connect_task_count--;
- if(m_current_connect_task_count==0)
- {
- m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_STOPING;
- LeaveCriticalSection(&m_lock);
- return stoping;
- }
- }
- }
- LeaveCriticalSection(&m_lock);
- m_img = NULL;
- pTW_IDENTITY pID = getDataSource();
- if(pID==NULL||pID->Id != m_scanner_index){
- if(GetDSMState()>4){
- disableDS();
- }
- if(GetDSMState()>3){
- unloadDS();
- }
- if(GetDSMState()<3){
- connectDSM();
- }
- loadDS(m_scanner_index);
- }
- TwainApp::enableDS(NULL,FALSE);
- if(GetDSMState()<=4){
- if(m_hwnd != NULL){
- IDENTIFOR_ERROR_MSG* msg =new IDENTIFOR_ERROR_MSG;
- msg->type=1;
- strcpy(msg->msg,"无法连接到扫描仪,请检测是否连接到扫描仪并打开电源开关.");
- PostMessage(m_hwnd,WM_IDENTIFOR_ERROR,(WPARAM)msg,0);
- }
- return stoping;
- }
- int ret =WaitCommand();
- if(ret==0) return running;
- return stoping;
- }
- ServiceState CSeparateThreadTwainFeeder::OnRunning( void )
- {
- StartScan();
- return stoping;
- }
- ServiceState CSeparateThreadTwainFeeder::OnStoping( void )
- {
- EnterCriticalSection(&m_lock);
- if(m_current_task_type == STTF_TASK_TYPE_CONNECT){
- m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_STOPED;
- m_current_task_type = STTF_TASK_TYPE_DEFAULT;
- LeaveCriticalSection(&m_lock);
- return IService::OnStoping();
- }
- LeaveCriticalSection(&m_lock);
- m_DSMessage = (TW_UINT16)-1;
- TwainApp::disableDS();
- TwainApp::unloadDS();
- return IService::OnStoping();
- }
- /************************************************************************/
- /* 等待扫描仪命令 */
- /************************************************************************/
- int CSeparateThreadTwainFeeder::WaitCommand()
- {
- while((TW_UINT16)-1 == m_DSMessage)
- {
- MSG Msg;
- if(!PeekMessage((LPMSG)&Msg, NULL, 0, 0,PM_REMOVE))
- {
- Sleep(1);
- continue;
- }
- if (Msg.message == WM_QUIT)
- break ;
- TW_EVENT twEvent = {0};
- twEvent.pEvent = (TW_MEMREF)&Msg;
- twEvent.TWMessage = MSG_NULL;
- TW_UINT16 twRC = TWRC_NOTDSEVENT;
- twRC = _DSM_Entry( getAppIdentity(),m_pDataSource,DG_CONTROL,DAT_EVENT, MSG_PROCESSEVENT,(TW_MEMREF)&twEvent);
- if(!gUSE_CALLBACKS && twRC==TWRC_DSEVENT)
- {
- switch (twEvent.TWMessage)
- {
- case MSG_XFERREADY:
- case MSG_CLOSEDSREQ:
- case MSG_NULL:
- m_DSMessage = twEvent.TWMessage;
- break;
- case MSG_CLOSEDSOK:
- TRACE("\nError - MSG_CLOSEDSOK in MSG_PROCESSEVENT loop for Scan\n");
- break;
- default:
- TRACE("\nError - Unknown message in MSG_PROCESSEVENT loop for Scan\n");
- break;
- }
- }
- if(twRC!=TWRC_DSEVENT)
- {
- TranslateMessage ((LPMSG)&Msg);
- DispatchMessage ((LPMSG)&Msg);
- }
- }
- if(m_DSMessage == MSG_XFERREADY)
- {
- // move to state 6 as a result of the data source. We can start a scan now.
- m_DSMState = 6;
- return 0;
- }
- return -1;
- }
- #include "./twain/FreeImage.h"
- #pragma comment(lib "FreeImage.lib")
- void CSeparateThreadTwainFeeder::navtive_graber( PBITMAPINFOHEADER pDIB )
- {
-
- ServiceState nextState =IService::OnRunning();
- if(nextState == stoping||nextState == stoping){
- return;
- }
- IplImage * img = cvCreateImage(cvSize(pDIB->biWidth,pDIB->biHeight),pDIB->biBitCount==1?IPL_DEPTH_8U : IPL_DEPTH_8U,3);
- int dib_width_step =((((pDIB->biWidth * pDIB->biBitCount) + 31) & ~31) / 8);
- int dib_index=0;
- int img_index=0;
- DWORD dwPaletteSize = 0;
- switch(pDIB->biBitCount)
- {
- case 1:
- dwPaletteSize = 2;
- break;
- case 8:
- dwPaletteSize = 256;
- break;
- case 24:
- break;
- default:
- assert(0); //Not going to work!
- break;
- }
- try{
- unsigned char * dib_data =(unsigned char *)pDIB+sizeof(BITMAPINFOHEADER)+(sizeof(RGBQUAD)*dwPaletteSize);
- unsigned char * img_data =(unsigned char *)img->imageData;
- RGBQUAD* rgb_quads =(RGBQUAD*)((unsigned char *)pDIB+sizeof(BITMAPINFOHEADER));
- if(pDIB->biBitCount ==1){
- const int shift[8]={1,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7};
- for (int y=0;y<pDIB->biHeight;y++)
- {
- dib_index =dib_width_step*(pDIB->biHeight-y-1);
- img_index =img->widthStep*y;
- for (int x =0;x<pDIB->biWidth;dib_index++)
- {
- for (int i=7;i>=0&&x<pDIB->biWidth;x++,i--,img_index+=3)
- {
- memcpy(&img_data[img_index],&rgb_quads[(dib_data[dib_index]&shift[i])>>i],3);
- }
- }
- }
- }else if(pDIB->biBitCount ==8){
- for (int y=0;y<pDIB->biHeight;y++)
- {
- dib_index =dib_width_step*(pDIB->biHeight-y-1);
- img_index =img->widthStep*y;
- for (int x =0;x<pDIB->biWidth;dib_index++,x++,img_index+=3)
- {
- memcpy(&img_data[img_index],&rgb_quads[dib_data[dib_index]],3);
- }
- }
- }else if(pDIB->biBitCount ==24){
- for (int y=0;y<pDIB->biHeight;y++)
- {
- dib_index =dib_width_step*(pDIB->biHeight-y-1);
- img_index =img->widthStep*y;
- for (int x =0;x<pDIB->biWidth;dib_index+=3,x++,img_index+=3)
- {
- memcpy(&img_data[img_index],&dib_data[dib_index],3);
- }
- }
- }
- }catch(...){}
- while(TRUE)
- {
- if(m_img == NULL){
- m_img =img;
- img =NULL;
- }
- if(img!=NULL){
- ServiceState nextState =IService::OnRunning();
- if(nextState == stoping||nextState == stoping){
- cvReleaseImage(&img);
- img = NULL;
- }
- Sleep(10);
- }
- else {break;}
- }
- }
- void CSeparateThreadTwainFeeder::StartScan()
- {
- TW_UINT32 mech;
- TW_CAPABILITY Cap;
- Cap.Cap = ICAP_XFERMECH;
- Cap.hContainer = 0;
- if( TWCC_SUCCESS!=get_CAP(Cap, MSG_GETCURRENT)
- ||!getCurrent(&Cap, mech) )
- {
- TRACE("Error: could not get the transfer mechanism");
- return;
- }
- if(Cap.hContainer)
- {
- _DSM_Free(Cap.hContainer);
- }
- switch ( (TW_UINT16)mech )
- {
- case TWSX_NATIVE:
- initiateTransfer_Native();
- break;
- case TWSX_FILE:
- {
- TW_UINT32 fileformat = TWFF_TIFF;
- Cap.Cap = ICAP_IMAGEFILEFORMAT;
- Cap.hContainer = 0;
- if(TWCC_SUCCESS==get_CAP(Cap, MSG_GETCURRENT))
- {
- getCurrent(&Cap, fileformat);
- }
- if(Cap.hContainer)
- {
- _DSM_Free(Cap.hContainer);
- }
- initiateTransfer_File((TW_UINT16)fileformat);
- }
- break;
- case TWSX_MEMORY:
- initiateTransfer_Memory();
- break;
- }
- return;
- }
- bool CSeparateThreadTwainFeeder::GetNext( BOOL wait,IplImage* & img, feeder_param& param )
- {
- do
- {
- BOOL isStoped = m_serviceState == stoped;
- if(m_img!=NULL){
- EnterCriticalSection(&m_lock);
- if(m_img!=NULL){
- img = m_img;
- m_img =NULL;
- param.type = identyfi_type_normal;
- param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
- LeaveCriticalSection(&m_lock);
- return true;
- }
- LeaveCriticalSection(&m_lock);
- }
- if(isStoped)return false;
- if(wait)Sleep(10);
- } while (wait);
- return false;
- }
- int CSeparateThreadTwainFeeder::Stop( void )
- {
- return IService::Stop();
- }
- void CSeparateThreadTwainFeeder::SetScannerIndex( const int scanner_index )
- {
- m_scanner_index =scanner_index;
- }
- BOOL CSeparateThreadTwainFeeder::HasStopCommand()
- {
- ServiceState nextState=IService::OnRunning();
- if(nextState == stoping) return TRUE;
- return FALSE;
- }
- int CSeparateThreadTwainFeeder::GetScannerList( vector<SCANNER_INFO> & scannerList )
- {
- if(GetDSMState()<3){connectDSM();}
- pTW_IDENTITY pID = NULL;
- int i=0;
- while(NULL !=(pID = getDataSource(i++))){
- SCANNER_INFO si;
- si.Id =pID->Id;
- strcpy(si.Manufacturer ,pID->Manufacturer);
- strcpy(si.ProductFamily ,pID->ProductFamily);
- strcpy(si.ProductName ,pID->ProductName);
- si.ProtocolMajor =pID->ProtocolMajor;
- si.ProtocolMinor =pID->ProtocolMinor;
- scannerList.push_back(si);
- }
- return i;
- }
- //获取默认扫描仪信息
- int CSeparateThreadTwainFeeder::GetDefaultScannerInfo( SCANNER_INFO &scannerInfo )
- {
- if(GetDSMState()<3){connectDSM();}
- pTW_IDENTITY pID = getDefaultDataSource();
- if(pID == NULL)return TRUE;
- scannerInfo.Id =pID->Id;
- strcpy(scannerInfo.Manufacturer ,pID->Manufacturer);
- strcpy(scannerInfo.ProductFamily ,pID->ProductFamily);
- strcpy(scannerInfo.ProductName ,pID->ProductName);
- scannerInfo.ProtocolMajor =pID->ProtocolMajor;
- scannerInfo.ProtocolMinor =pID->ProtocolMinor;
- return FALSE;
- }
- //显示扫描仪设置
- int CSeparateThreadTwainFeeder::ShowScannerSetting( const unsigned int nId )
- {
- if(GetDSMState()>3&&nId != getDataSource()->Id){
- unloadDS();
- loadDS(nId);
- }
- if(GetDSMState()<3){
- connectDSM();
- }
- if(GetDSMState()<4){
- loadDS(nId);
- }
- bool show_success =enableDSUIOnly(NULL);
- if(show_success){WaitCommand();}
- disableDS();
- return show_success?1:0;
- }
- //断开扫描仪
- int CSeparateThreadTwainFeeder::DisconnectScanner()
- {
- unloadDS();
- return TRUE;
- }
- //连接到扫描仪
- int CSeparateThreadTwainFeeder::ConnectScanner( const int nId )
- {
- EnterCriticalSection(&m_lock);
- if(m_current_task_type!=STTF_TASK_TYPE_DEFAULT&&m_current_task_type!=STTF_TASK_TYPE_CONNECT) {
- LeaveCriticalSection(&m_lock);
- return FALSE;
- }
- if(m_current_task_type!=STTF_TASK_TYPE_CONNECT){
- m_current_task_type = STTF_TASK_TYPE_CONNECT;
- m_current_connect_task_first =0;
- m_current_connect_task_count =0;
- }
- int index;
- //因为只能连接一个扫描仪,连接切换较快,直接忽略队列中的连接请求
- if(m_current_connect_task_count<MAX_CONNET_TASK_COUNT)
- {
- index =(m_current_connect_task_first+m_current_connect_task_count)%MAX_CONNET_TASK_COUNT;
- m_current_connect_task_count++;
- }
- else {
- index =(m_current_connect_task_first+m_current_connect_task_count-1)%MAX_CONNET_TASK_COUNT;
- }
- m_current_connect_task[index] = nId;
- BOOL is_stoping =FALSE;
- switch(m_connect_task_excute_state){
- case STTF_TASK_EXCUTE_STATE_STOPED:
- while(GetServiceSate()!= stoped){
- Sleep(1);
- }
- case STTF_TASK_EXCUTE_STATE_NOTSTART:
- Start();
- m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_EXCUTING;
- break;
- case STTF_TASK_EXCUTE_STATE_EXCUTING:
- break;
- case STTF_TASK_EXCUTE_STATE_STOPING:
- is_stoping =TRUE;
- break;
- }
- LeaveCriticalSection(&m_lock);
- if(is_stoping){
- while(GetServiceSate() != stoped){
- Sleep(1);
- }
- is_stoping =FALSE;
- Start();
- }
- return TRUE;
- }
- boost::shared_ptr<CSeparateThreadTwainFeeder> CSeparateThreadTwainFeeder::Get()
- {
- static boost::shared_ptr<CSeparateThreadTwainFeeder> instance(new CSeparateThreadTwainFeeder);
- return instance;
- }
|