Get scanner image-Twain Protocol

Source: Internet
Author: User

Source code (Save the connection as a download method, rename it as a zip file, and decompress it to the source file)

Download link http://hi.csdn.net/attachment/201107/19/0_1311044325WNjx.gif

Download the twain32 header file definition on the official Twain website.


Bool savebitmap (hglobal _ hdib, const int serial_number) {bitmapfileheader BFH = {0}; bitmapinfoheader * pbih = NULL; handle HF = NULL; // obtain the temporary file char _ sztempfile [max_path] = {0}; char _ sztempdir [max_path] = {0}; srand (gettickcount ()); gettemppatha (sizeof (_ sztempdir), _ sztempdir); sprintf_s (_ sztempfile, sizeof (_ sztempfile), "% S // %d-demod.bmp", _ sztempdir, serial_number, rand (); HF = createfilea (_ Sztempfile, generic_write, 0, null, create_always, file_attribute_normal, null); If (HF = invalid_handle_value) globalfree (_ hdib); Return false; // parse it into a bitmap file header pbih = (bitmapinfoheader *) globallock (_ hdib); DWORD dwbyteswritten = 0l; BFH. bftype = (Word) ('M' <8) | 'B'); BFH. bfsize = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + (pbih-> biwidth * pbih-> bibitcount + 31)/32) * 4) * pbih-> bihei Ght) + pbih-> biclrused * sizeof (rgbquad); BFH. bfreserved1 = 0; BFH. bfreserved2 = 0; BFH. bytes = sizeof (bitmapfileheader) + sizeof (bitmapinfoheader) + pbih-> biclrused * sizeof (rgbquad); // write bitmap to the file writefile (HF, & BFH, sizeof (bitmapfileheader ), & dwbyteswritten, null); writefile (HF, pbih, BFH. bfsize-sizeof (bitmapfileheader), & dwbyteswritten, null); closehandle (HF); HF = invalid_handle_value; Trace ("Save bitmap to:"); trace (_ sztempfile); globalunlock (_ hdib); globalfree (_ hdib); Return true ;} unsigned long _ stdcall scanthread (void * ARGs) {hwnd _ hwnd = NULL; MSG _ MSG = {0}; hmodule _ hdsm = NULL; hbitmap _ hbitmap = NULL; int _ ncount = 0; tw_capability _ Cap = {0}; tw_uint16 _ twrc = twrc_failure; ptw_enumeration _ penumeration = NULL; ponevalue = NULL; tw_event _ twevent = {0 }; t W_identity _ DSID = {0}; tw_userinterface _ twui = {0}; tw_pendingxfers _ twpendingxfer = {0}; tw_uint16 _ modetype =-1; tw_uint32 _ Index = 0; tw_identity _ dsmid = {0, {1,703, twlg_usa, twcy_usa, "twain-1.7.0.3"}, 1, 7, (dg_image | dg_control), "XXXX it Corp. ltd. "," XXXX webctrl "," wuzh1230 "}; // initialize _ hdsm = loadlibrarya (" twain_32.dll "); // obtain the interface pointer dsmentryproc _ pdsmentryproc = (dsmentryproc) Ge Tprocaddress (_ hdsm, "dsm_entry"); // create a window and collect bitmap _ hwnd = create1_wexa (0, "#32770", "Scan client ", ws_overlappedwindow, 0, 0, 1, 1, null, null, getmodulehandlea (0), null); // open the data source Manager _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_parent, msg_opendsm, (tw_memref) & _ hwnd); // select the data source _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_identity, msg_getdefault, (tw_memre F) & _ DSID); _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_identity, msg_userselect, (tw_memref) & _ DSID ); // open the data source _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_identity, msg_opends, & _ DSID); // read and set the transmission mode icap_xfermech memset (& _ cap, 0, sizeof (tw_capability); _ cap. CAP = icap_xfermech; _ cap. contype = twon_enumeration; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capabi Lity, msg_get, (tw_memref) & _ CAP); _ penumeration = (ptw_enumeration) globallock (_ cap. hcontainer); // search native-mode indexes for (_ Index = 0; _ index <_ penumeration-> numitems; _ index ++) {If (tw_uint16 *) _ penumeration-> itemlist) [_ Index] = twsx_native) {break;} // If (_ Index = _ penumeration-> numitems) {goto end_of_function;} // if the current index location mode is native if (_ Index = _ penumeration-> Curren Tindex) {trace ("Current xfer mode is 'native (twsx_native) '"); _ modetype = twsx_native; globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer);} else {trace ("current mode is not 'native (twsx_native) '"); // sets the native mode, _ index is the index _ penumeration-> currentindex = _ index; _ penumeration-> defaultindex = _ index; globalunlock (_ cap. hcontainer); _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_con Trol, dat_capability, msg_set, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {trace ("failed to set xfer mode to 'native (twsx_native) '"); goto end_of_function;} globalfree (_ cap. hcontainer); // read mode again _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_get, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {trace ("failed to read back xfer mode. "); goto end_of_function;} _ penumeration = (ptw_enumeration) globallock (_ cap. hcontainer); // read _ modetype = (tw_uint16 *) _ penumeration-> itemlist) [_ penumeration-> currentindex]; globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer); // check whether the mode is native if (_ modetype! = Twsx_native) {trace ("xfer mode not 'native (twsx_native) ', quit. "); goto end_of_function;} trace (" Current xfer mode is 'native (twsx_native )'"); // read the cap_feederenabled trace ("read capability 'feed enabled (cap_feederenabled )'. "); memset (& _ cap, 0, sizeof (tw_capability); _ cap. CAP = cap_feederenabled; _ cap. contype = twon_onevalue; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capabil Ity, msg_get, (tw_memref) & _ CAP); globalfree (_ cap. hcontainer); // failed to read the automatic paper feed device, possibly because no hardware supports if (_ twrc! = Twrc_success) {trace ("failed to read 'feed enabled (cap_feederenabled) '"); trace ("[possiblly no this hardware.]. "); If (feed_enabled) {trace (" no hardware to support feed, so unable to set, quit. "); goto end_of_function;} else {// There is a hardware trace for paper delivery (" OK to read 'feed enabled (cap_feederenabled) '"); trace (" [hardware present]. "); // first check whether memset (& _ cap, 0, sizeof (tw_capability) is enabled; _ cap. CAP = cap_feede Renabled; _ cap. contype = twon_onevalue; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_get, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {trace ("failed to set 'feed enabled (cap_feederenabled) '"); globalfree (_ cap. hcontainer); goto end_of_function;} _ ponevalue = (ptw_onevalue) globallock (_ cap. hcontainer); // if you have enabled if (_ ponevalue-> item = feed_enabled) {trace ("[feeder enabled (cap_feederenabled)] is same as parameter ") globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer);} // disabled. Enable the Paper Feed Device else {_ ponevalue-> ite M = feed_enabled; globalunlock (_ cap. hcontainer); _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_set, (tw_memref) & _ CAP); globalfree (_ cap. hcontainer); If (_ twrc! = Twrc_success) {trace ("faile to set [feeder enabled (cap_feederenabled)]"); goto end_of_function ;}// read again and check whether memset (& _ cap, 0, sizeof (tw_capability); _ cap. CAP = cap_feederenabled; _ cap. contype = twon_onevalue; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_get, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {globalfree (_ cap. hcontainer); trace ("failed to read [feeder enabled (cap_feederenabled)]"); goto end_of_function;} _ ponevalue = (ptw_onevalue) globallock (_ cap. hcontainer); globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer); // trace ("OK to read [feed enabled (cap_feederenabled)]"); # If 0 // enable paper delivery: Read memset (& _ cap, 0, sizeof (tw_capability); _ cap. CAP = cap_autofeed; _ cap. Contype = twon_onevalue; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_get, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {globalfree (_ cap. hcontainer); trace ("setting 'automatic paper delivery (cap_autofeed) 'failed"); goto end_of_function;} // if the paper delivery _ ponevalue = (ptw_onevalue) is enabled) globallock (_ cap. hcontainer); If (_ ponevalue-> item = autofeed_enabled) {globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer);} // not enabled. Set else {_ ponevalue-> item = autofeed_enabled; globalunlock (_ cap. hcontainer); _ twrc = calldsmentry (& _ dsmid, & _ DS ID, dg_control, dat_capability, msg_set, (tw_memref) & _ CAP); globalfree (_ cap. hcontainer); If (_ twrc! = Twrc_success) {trace ("setting 'automatic paper delivery (cap_autofeed) 'failed"); goto end_of_function ;}}// read again, check whether memset (& _ cap, 0, sizeof (tw_capability); _ cap is enabled successfully. CAP = cap_autofeed; _ cap. contype = twon_onevalue; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_capability, msg_get, (tw_memref) & _ CAP); If (_ twrc! = Twrc_success) {globalfree (_ cap. hcontainer); trace ("setting 'automatic paper delivery (cap_autofeed) 'failed"); goto end_of_function;} _ ponevalue = (ptw_onevalue) globallock (_ cap. hcontainer); globalunlock (_ cap. hcontainer); globalfree (_ cap. hcontainer); // trace ("set 'automatic paper delivery (cap_autofeed) 'succeeded"); # endif} // display interface, scan _ twui. hparent = getactivewindow (); _ twui. showui = true; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_use Rinterface, msg_enableds, (tw_memref) & _ twui); If (_ twrc! = Twrc_success) {trace ("faile to enable UI. "); goto end_of_function;} // message loop _ twevent. pevent = (tw_memref) & _ MSG; while (getmessage (& _ MSG, null, 0, 0) {_ twrc = twrc_failure; _ twevent. twmessage =-1; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_event, msg_processevent, (tw_memref) & _ twevent ); if (_ twrc = twrc_failure | _ twevent. twmessage =-1) {trace ("failed to get message. "); G Oto end_of_function;} // the message is not sent from the data source. By default, if (_ twrc = twrc_notdsevent) {translatemessage (& _ MSG); dispatchmessage (& _ MSG ); continue;} // process the switch (_ twevent. twmessage) {// The bitmap is ready in the scanner. Case msg_xferready: {// cyclically accepts each bitmap memset (& _ twpendingxfer, 0, sizeof (tw_pendingxfers )); do {_ hbitmap = NULL; // accept a bitmap _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_image, dat_imagenativexfer, msg_get, (Tw_memref) & _ hbitmap); Switch (_ twrc) {Case twrc_xferdone: // transfer completed, save the bitmap {trace ("xfer is done. (twrc_xferdone) "); If (savebitmap (hglobal) _ hbitmap, _ ncount ++) {trace (" OK to save bitmap to file. ");} else {trace (" failed to save bitmap to file. ");} trace (" Send endxfer [msg_endxfer] Message. "); _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_pendingxfers, msg_endxfer, (tw_memref) & _ TW Pendingxfer);} break; Case twrc_cancel: // cancel case twrc_failure: // failure default: {// end bitmap transmission trace ("User canceld (twrc_cancel ),"); trace ("or xfer failed (twrc_failure ). "); _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_pendingxfers, msg_endxfer, (tw_memref) & _ twpendingxfer); trace (" Send endxfer message. ");} break;} while (_ twpendingxfer. count! = 0); // If there are multiple bitmaps} break; Case msg_closedsreq: // case msg_closedsok: // if the data source is closed successfully, Case msg_null: // empty message trace ("msg_closedsreq or msg_closedsok or msg_null)"); default: break; // ignore} // exit the outer loop if (_ twevent. twmessage! = Msg_null) {trace ("Break up from message loop. "); break;} end_of_function: trace (" start to cleanup resources of thread. "); // close the user interface memset (& _ twui, 0, sizeof (tw_userinterface); _ twui. hparent = _ hwnd; _ twui. showui = twon_dontcare8; _ twrc = calldsmentry (& _ dsmid, & _ DSID, dg_control, dat_userinterface, msg_disableds, (tw_memref) & _ twui ); // close the data source _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_identity, msg_closeds, & _ DSID); // close the data source Manager _ twrc = calldsmentry (& _ dsmid, null, dg_control, dat_parent, msg_closedsm, & _ hwnd); // close the window destroywindow (_ hwnd); // release the management database freelibrary (_ hdsm); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.