Scan Images from Twain Devices

Source: Internet
Author: User
I. Introduction Twain Data Source Management Program (DSM) industrial standard software library for extracting images from static image devices. Most scanners and digital cameras provide Twain drivers that allow applications to conveniently retrieve images from these devices using a unified Twain interface.

2. Using steps on the Internet there is very little Chinese information about Twain programming, and the code is harder to find, because I had to carefully read the twain specification provided by the http://www.twain.org. The following describes how to use the twain interface to obtain images.

 

In Windows, there is a twain_32.dll. All Twain operations are implemented through the dsm_entry function exported by this DLL. (To be honest, I personally think the twain interface is too poorly designed, look at the WIA of MS, logically very clear ). The declaration of this function is as follows:

 

Tw_uint16 far Pascal dsm_entry (
Ptw_identity porigin, // source of message
Ptw_identity pdest, // destination of message
Tw_uint32 DG, // data group ID: dg_xxxx
Tw_uint16 dat, // data argument type: dat_xxxx
Tw_uint16 MSG, // Message ID: msg_xxxx
Tw_memref pdata // pointer to data
);

 

1. Open DSM (data source Manager: data source Manager)

 

Twain is a data source management program. The application must first use the msg_opendsm message to open the data source Manager. You need to specify a window handle. The application should process the twain message (msg_processevent) in the message loop of the window ).

 

2. Select DS (Data Source: Data Source)

 

Because a system may have multiple Twain devices, You must select a data source. You can select the default data source (msg_getdefault) and the Select data source dialog box to select the data source (msg_userselect ).

 

3. Open DS

 

Use the msg_opends message to open the data source.

 

4. Set Parameters

 

The message is msg_set and various parameters are set, such as the acquisition method and number of images. Some parameters are valid only when they are supported by the device driver.

 

5. display the scan page

 

Use the message msg_enableds to display the user interface (UI) provided by the device driver ).

 

6. Get the image

 

If you select a scan event, you can obtain the event (msg_xferready) in the message loop of the window ). In this case, the application can set the file name through dat_setupfilexfer, and then use dat_imagefilexfer to get the image to the file.

 

7. Close the scan page

 

The msg_closedsreq or msg_closedsok message is obtained in the message loop of the window. You can close the scan interface (msg_disableds ).

 

8. Disable DS

 

The message is msg_closeds.

 

9. Close the data source

The message is msg_closedsm.

 

Iii. ctwainhelper helper class I wrote a static Twain helper class ctwainhelper for ease of use. Using the five functions of ctwainhelper, you can obtain images from the twain device to the file. The usage is as follows:

 

  1. Call ctwainhelper: Initialize () to check whether a device is available.
  2. In the window message loop, call ctwainhelper: processmessage () to process Twain messages.
  3. Call ctwainhelper: getimage () to obtain the image ().
  4. If the image is ready (for example, if the user is sure to scan the image), the window will receive the wm_command message, and wparam is idc_twainhelper. In this case, the application can call ctwainhelper: transferimage () to obtain the image to the file.
For more information, see the sample code.

Ctwainhelper can be used in Visual C ++ 6.x/ 7.x projects and supports Unicode compiling. Because it is a static class, you only need to do a little bit of work to rewrite it to C code.

 

4. Later, of course, the above is just a common step. In fact, the application can completely customize all the steps, such as scanning directly without using the twain driver to provide a scan dialog box, or scanning images to the memory. For details, refer to Twain specification. The steps are similar, and the messages and parameters vary widely. It should be easy to take a closer look.

 

If you do not have a Twain device and want to develop the twain program, you can download Twain developers toolkit from the twain official website. A virtual Twain device will be installed. However, the application works normally on this virtual Twain device, which does not necessarily mean that it can be used normally on the actual Twain device. In the past, ctwainhelper had encountered such a situation that it was good on the virtual Twain device, but it was not able to scan images on my scanner. After checking, we found that the unsupported parameters were set.

Finally, Twain is the abbreviation of technology without a interesting name. The literal translation is not "technology that is not interesting". It is really a confusing name.

 

Twain helper class: ctwainhelper (including sample code 35 K)
Twain Official Website: http://www.twain.org
Twain header file: http://www.twain.org/devfiles/twain.h
Twain specification: http://www.twain.org/docs/Spec1_9_197.pdf
Twain developers Toolkit: http://www.twain.org/devfiles/twainkit.exe

[2006.2.22] The following is a simpler version. The getimage function automatically contains a message loop and will not be returned until the twain dialog box is completely closed. This is suitable for any scenario:

Download file click here to download the file

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.