SeparateThreadTwainFeeder.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #include "StdAfx.h"
  2. #include "SeparateThreadTwainFeeder.h"
  3. CSeparateThreadTwainFeeder::CSeparateThreadTwainFeeder(void)
  4. :m_current_task_type(STTF_TASK_TYPE_DEFAULT)
  5. ,m_connect_task_excute_state(STTF_TASK_EXCUTE_STATE_NOTSTART)
  6. ,m_hWnd(NULL)
  7. {
  8. InitializeCriticalSection(&m_lock);
  9. m_img =NULL;
  10. }
  11. CSeparateThreadTwainFeeder::~CSeparateThreadTwainFeeder(void)
  12. {
  13. DeleteCriticalSection(&m_lock);
  14. }
  15. ServiceState CSeparateThreadTwainFeeder::OnStarting( void )
  16. {
  17. EnterCriticalSection(&m_lock);
  18. if(m_current_task_type == STTF_TASK_TYPE_CONNECT){//扫描仪连接任务
  19. while(m_current_connect_task_count>0){
  20. int nId = m_current_connect_task[m_current_connect_task_first];
  21. LeaveCriticalSection(&m_lock);
  22. if(GetDSMState()>3&&nId != getDataSource()->Id){
  23. unloadDS();
  24. loadDS(nId);
  25. }
  26. if(GetDSMState()<3){
  27. connectDSM();
  28. }
  29. if(GetDSMState()<4){
  30. loadDS(nId);
  31. }
  32. if(m_hWnd!=NULL)PostMessage(m_hWnd,WM_IDENTIFOR_SANNER_CONNECT,nId,GetDSMState()>=4?TRUE:FALSE);
  33. EnterCriticalSection(&m_lock);
  34. m_current_connect_task_first =(m_current_connect_task_first+1)%MAX_CONNET_TASK_COUNT;
  35. m_current_connect_task_count--;
  36. if(m_current_connect_task_count==0)
  37. {
  38. m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_STOPING;
  39. LeaveCriticalSection(&m_lock);
  40. return stoping;
  41. }
  42. }
  43. }
  44. LeaveCriticalSection(&m_lock);
  45. m_img = NULL;
  46. pTW_IDENTITY pID = getDataSource();
  47. if(pID==NULL||pID->Id != m_scanner_index){
  48. if(GetDSMState()>4){
  49. disableDS();
  50. }
  51. if(GetDSMState()>3){
  52. unloadDS();
  53. }
  54. if(GetDSMState()<3){
  55. connectDSM();
  56. }
  57. loadDS(m_scanner_index);
  58. }
  59. TwainApp::enableDS(NULL,FALSE);
  60. if(GetDSMState()<=4){
  61. if(m_hwnd != NULL){
  62. IDENTIFOR_ERROR_MSG* msg =new IDENTIFOR_ERROR_MSG;
  63. msg->type=1;
  64. strcpy(msg->msg,"无法连接到扫描仪,请检测是否连接到扫描仪并打开电源开关.");
  65. PostMessage(m_hwnd,WM_IDENTIFOR_ERROR,(WPARAM)msg,0);
  66. }
  67. return stoping;
  68. }
  69. int ret =WaitCommand();
  70. if(ret==0) return running;
  71. return stoping;
  72. }
  73. ServiceState CSeparateThreadTwainFeeder::OnRunning( void )
  74. {
  75. StartScan();
  76. return stoping;
  77. }
  78. ServiceState CSeparateThreadTwainFeeder::OnStoping( void )
  79. {
  80. EnterCriticalSection(&m_lock);
  81. if(m_current_task_type == STTF_TASK_TYPE_CONNECT){
  82. m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_STOPED;
  83. m_current_task_type = STTF_TASK_TYPE_DEFAULT;
  84. LeaveCriticalSection(&m_lock);
  85. return IService::OnStoping();
  86. }
  87. LeaveCriticalSection(&m_lock);
  88. m_DSMessage = (TW_UINT16)-1;
  89. TwainApp::disableDS();
  90. TwainApp::unloadDS();
  91. return IService::OnStoping();
  92. }
  93. /************************************************************************/
  94. /* 等待扫描仪命令 */
  95. /************************************************************************/
  96. int CSeparateThreadTwainFeeder::WaitCommand()
  97. {
  98. while((TW_UINT16)-1 == m_DSMessage)
  99. {
  100. MSG Msg;
  101. if(!PeekMessage((LPMSG)&Msg, NULL, 0, 0,PM_REMOVE))
  102. {
  103. Sleep(1);
  104. continue;
  105. }
  106. if (Msg.message == WM_QUIT)
  107. break ;
  108. TW_EVENT twEvent = {0};
  109. twEvent.pEvent = (TW_MEMREF)&Msg;
  110. twEvent.TWMessage = MSG_NULL;
  111. TW_UINT16 twRC = TWRC_NOTDSEVENT;
  112. twRC = _DSM_Entry( getAppIdentity(),m_pDataSource,DG_CONTROL,DAT_EVENT, MSG_PROCESSEVENT,(TW_MEMREF)&twEvent);
  113. if(!gUSE_CALLBACKS && twRC==TWRC_DSEVENT)
  114. {
  115. switch (twEvent.TWMessage)
  116. {
  117. case MSG_XFERREADY:
  118. case MSG_CLOSEDSREQ:
  119. case MSG_NULL:
  120. m_DSMessage = twEvent.TWMessage;
  121. break;
  122. case MSG_CLOSEDSOK:
  123. TRACE("\nError - MSG_CLOSEDSOK in MSG_PROCESSEVENT loop for Scan\n");
  124. break;
  125. default:
  126. TRACE("\nError - Unknown message in MSG_PROCESSEVENT loop for Scan\n");
  127. break;
  128. }
  129. }
  130. if(twRC!=TWRC_DSEVENT)
  131. {
  132. TranslateMessage ((LPMSG)&Msg);
  133. DispatchMessage ((LPMSG)&Msg);
  134. }
  135. }
  136. if(m_DSMessage == MSG_XFERREADY)
  137. {
  138. // move to state 6 as a result of the data source. We can start a scan now.
  139. m_DSMState = 6;
  140. return 0;
  141. }
  142. return -1;
  143. }
  144. #include "./twain/FreeImage.h"
  145. #pragma comment(lib "FreeImage.lib")
  146. void CSeparateThreadTwainFeeder::navtive_graber( PBITMAPINFOHEADER pDIB )
  147. {
  148. ServiceState nextState =IService::OnRunning();
  149. if(nextState == stoping||nextState == stoping){
  150. return;
  151. }
  152. IplImage * img = cvCreateImage(cvSize(pDIB->biWidth,pDIB->biHeight),pDIB->biBitCount==1?IPL_DEPTH_8U : IPL_DEPTH_8U,3);
  153. int dib_width_step =((((pDIB->biWidth * pDIB->biBitCount) + 31) & ~31) / 8);
  154. int dib_index=0;
  155. int img_index=0;
  156. DWORD dwPaletteSize = 0;
  157. switch(pDIB->biBitCount)
  158. {
  159. case 1:
  160. dwPaletteSize = 2;
  161. break;
  162. case 8:
  163. dwPaletteSize = 256;
  164. break;
  165. case 24:
  166. break;
  167. default:
  168. assert(0); //Not going to work!
  169. break;
  170. }
  171. try{
  172. unsigned char * dib_data =(unsigned char *)pDIB+sizeof(BITMAPINFOHEADER)+(sizeof(RGBQUAD)*dwPaletteSize);
  173. unsigned char * img_data =(unsigned char *)img->imageData;
  174. RGBQUAD* rgb_quads =(RGBQUAD*)((unsigned char *)pDIB+sizeof(BITMAPINFOHEADER));
  175. if(pDIB->biBitCount ==1){
  176. const int shift[8]={1,1<<1,1<<2,1<<3,1<<4,1<<5,1<<6,1<<7};
  177. for (int y=0;y<pDIB->biHeight;y++)
  178. {
  179. dib_index =dib_width_step*(pDIB->biHeight-y-1);
  180. img_index =img->widthStep*y;
  181. for (int x =0;x<pDIB->biWidth;dib_index++)
  182. {
  183. for (int i=7;i>=0&&x<pDIB->biWidth;x++,i--,img_index+=3)
  184. {
  185. memcpy(&img_data[img_index],&rgb_quads[(dib_data[dib_index]&shift[i])>>i],3);
  186. }
  187. }
  188. }
  189. }else if(pDIB->biBitCount ==8){
  190. for (int y=0;y<pDIB->biHeight;y++)
  191. {
  192. dib_index =dib_width_step*(pDIB->biHeight-y-1);
  193. img_index =img->widthStep*y;
  194. for (int x =0;x<pDIB->biWidth;dib_index++,x++,img_index+=3)
  195. {
  196. memcpy(&img_data[img_index],&rgb_quads[dib_data[dib_index]],3);
  197. }
  198. }
  199. }else if(pDIB->biBitCount ==24){
  200. for (int y=0;y<pDIB->biHeight;y++)
  201. {
  202. dib_index =dib_width_step*(pDIB->biHeight-y-1);
  203. img_index =img->widthStep*y;
  204. for (int x =0;x<pDIB->biWidth;dib_index+=3,x++,img_index+=3)
  205. {
  206. memcpy(&img_data[img_index],&dib_data[dib_index],3);
  207. }
  208. }
  209. }
  210. }catch(...){}
  211. while(TRUE)
  212. {
  213. if(m_img == NULL){
  214. m_img =img;
  215. img =NULL;
  216. }
  217. if(img!=NULL){
  218. ServiceState nextState =IService::OnRunning();
  219. if(nextState == stoping||nextState == stoping){
  220. cvReleaseImage(&img);
  221. img = NULL;
  222. }
  223. Sleep(10);
  224. }
  225. else {break;}
  226. }
  227. }
  228. void CSeparateThreadTwainFeeder::StartScan()
  229. {
  230. TW_UINT32 mech;
  231. TW_CAPABILITY Cap;
  232. Cap.Cap = ICAP_XFERMECH;
  233. Cap.hContainer = 0;
  234. if( TWCC_SUCCESS!=get_CAP(Cap, MSG_GETCURRENT)
  235. ||!getCurrent(&Cap, mech) )
  236. {
  237. TRACE("Error: could not get the transfer mechanism");
  238. return;
  239. }
  240. if(Cap.hContainer)
  241. {
  242. _DSM_Free(Cap.hContainer);
  243. }
  244. switch ( (TW_UINT16)mech )
  245. {
  246. case TWSX_NATIVE:
  247. initiateTransfer_Native();
  248. break;
  249. case TWSX_FILE:
  250. {
  251. TW_UINT32 fileformat = TWFF_TIFF;
  252. Cap.Cap = ICAP_IMAGEFILEFORMAT;
  253. Cap.hContainer = 0;
  254. if(TWCC_SUCCESS==get_CAP(Cap, MSG_GETCURRENT))
  255. {
  256. getCurrent(&Cap, fileformat);
  257. }
  258. if(Cap.hContainer)
  259. {
  260. _DSM_Free(Cap.hContainer);
  261. }
  262. initiateTransfer_File((TW_UINT16)fileformat);
  263. }
  264. break;
  265. case TWSX_MEMORY:
  266. initiateTransfer_Memory();
  267. break;
  268. }
  269. return;
  270. }
  271. bool CSeparateThreadTwainFeeder::GetNext( BOOL wait,IplImage* & img, feeder_param& param )
  272. {
  273. do
  274. {
  275. BOOL isStoped = m_serviceState == stoped;
  276. if(m_img!=NULL){
  277. EnterCriticalSection(&m_lock);
  278. if(m_img!=NULL){
  279. img = m_img;
  280. m_img =NULL;
  281. param.type = identyfi_type_normal;
  282. param.srcimg_type = SRC_IMG_HANDLE_TYPE_SAVE;
  283. LeaveCriticalSection(&m_lock);
  284. return true;
  285. }
  286. LeaveCriticalSection(&m_lock);
  287. }
  288. if(isStoped)return false;
  289. if(wait)Sleep(10);
  290. } while (wait);
  291. return false;
  292. }
  293. int CSeparateThreadTwainFeeder::Stop( void )
  294. {
  295. return IService::Stop();
  296. }
  297. void CSeparateThreadTwainFeeder::SetScannerIndex( const int scanner_index )
  298. {
  299. m_scanner_index =scanner_index;
  300. }
  301. BOOL CSeparateThreadTwainFeeder::HasStopCommand()
  302. {
  303. ServiceState nextState=IService::OnRunning();
  304. if(nextState == stoping) return TRUE;
  305. return FALSE;
  306. }
  307. int CSeparateThreadTwainFeeder::GetScannerList( vector<SCANNER_INFO> & scannerList )
  308. {
  309. if(GetDSMState()<3){connectDSM();}
  310. pTW_IDENTITY pID = NULL;
  311. int i=0;
  312. while(NULL !=(pID = getDataSource(i++))){
  313. SCANNER_INFO si;
  314. si.Id =pID->Id;
  315. strcpy(si.Manufacturer ,pID->Manufacturer);
  316. strcpy(si.ProductFamily ,pID->ProductFamily);
  317. strcpy(si.ProductName ,pID->ProductName);
  318. si.ProtocolMajor =pID->ProtocolMajor;
  319. si.ProtocolMinor =pID->ProtocolMinor;
  320. scannerList.push_back(si);
  321. }
  322. return i;
  323. }
  324. //获取默认扫描仪信息
  325. int CSeparateThreadTwainFeeder::GetDefaultScannerInfo( SCANNER_INFO &scannerInfo )
  326. {
  327. if(GetDSMState()<3){connectDSM();}
  328. pTW_IDENTITY pID = getDefaultDataSource();
  329. if(pID == NULL)return TRUE;
  330. scannerInfo.Id =pID->Id;
  331. strcpy(scannerInfo.Manufacturer ,pID->Manufacturer);
  332. strcpy(scannerInfo.ProductFamily ,pID->ProductFamily);
  333. strcpy(scannerInfo.ProductName ,pID->ProductName);
  334. scannerInfo.ProtocolMajor =pID->ProtocolMajor;
  335. scannerInfo.ProtocolMinor =pID->ProtocolMinor;
  336. return FALSE;
  337. }
  338. //显示扫描仪设置
  339. int CSeparateThreadTwainFeeder::ShowScannerSetting( const unsigned int nId )
  340. {
  341. if(GetDSMState()>3&&nId != getDataSource()->Id){
  342. unloadDS();
  343. loadDS(nId);
  344. }
  345. if(GetDSMState()<3){
  346. connectDSM();
  347. }
  348. if(GetDSMState()<4){
  349. loadDS(nId);
  350. }
  351. bool show_success =enableDSUIOnly(NULL);
  352. if(show_success){WaitCommand();}
  353. disableDS();
  354. return show_success?1:0;
  355. }
  356. //断开扫描仪
  357. int CSeparateThreadTwainFeeder::DisconnectScanner()
  358. {
  359. unloadDS();
  360. return TRUE;
  361. }
  362. //连接到扫描仪
  363. int CSeparateThreadTwainFeeder::ConnectScanner( const int nId )
  364. {
  365. EnterCriticalSection(&m_lock);
  366. if(m_current_task_type!=STTF_TASK_TYPE_DEFAULT&&m_current_task_type!=STTF_TASK_TYPE_CONNECT) {
  367. LeaveCriticalSection(&m_lock);
  368. return FALSE;
  369. }
  370. if(m_current_task_type!=STTF_TASK_TYPE_CONNECT){
  371. m_current_task_type = STTF_TASK_TYPE_CONNECT;
  372. m_current_connect_task_first =0;
  373. m_current_connect_task_count =0;
  374. }
  375. int index;
  376. //因为只能连接一个扫描仪,连接切换较快,直接忽略队列中的连接请求
  377. if(m_current_connect_task_count<MAX_CONNET_TASK_COUNT)
  378. {
  379. index =(m_current_connect_task_first+m_current_connect_task_count)%MAX_CONNET_TASK_COUNT;
  380. m_current_connect_task_count++;
  381. }
  382. else {
  383. index =(m_current_connect_task_first+m_current_connect_task_count-1)%MAX_CONNET_TASK_COUNT;
  384. }
  385. m_current_connect_task[index] = nId;
  386. BOOL is_stoping =FALSE;
  387. switch(m_connect_task_excute_state){
  388. case STTF_TASK_EXCUTE_STATE_STOPED:
  389. while(GetServiceSate()!= stoped){
  390. Sleep(1);
  391. }
  392. case STTF_TASK_EXCUTE_STATE_NOTSTART:
  393. Start();
  394. m_connect_task_excute_state = STTF_TASK_EXCUTE_STATE_EXCUTING;
  395. break;
  396. case STTF_TASK_EXCUTE_STATE_EXCUTING:
  397. break;
  398. case STTF_TASK_EXCUTE_STATE_STOPING:
  399. is_stoping =TRUE;
  400. break;
  401. }
  402. LeaveCriticalSection(&m_lock);
  403. if(is_stoping){
  404. while(GetServiceSate() != stoped){
  405. Sleep(1);
  406. }
  407. is_stoping =FALSE;
  408. Start();
  409. }
  410. return TRUE;
  411. }
  412. boost::shared_ptr<CSeparateThreadTwainFeeder> CSeparateThreadTwainFeeder::Get()
  413. {
  414. static boost::shared_ptr<CSeparateThreadTwainFeeder> instance(new CSeparateThreadTwainFeeder);
  415. return instance;
  416. }