C/C++ Sample: Acquire and save to BMP file

Top  Previous  Next

The following 'C' / C++ function is all that is required to do the following:

 

Check if TWAIN is installed.
Initialize the DTWAIN Dynamic Link Library.
Select a Source
Acquire a page using the Native transfer mode
Save page to a BMP file called "test.bmp"
Shut down any open TWAIN Source's and DTWAIN itself

___________________________________________________________________

#include <dtwain.h>

#include <tchar.h>  // for Windows _T macro to denote string type

 

void ExampleFunction( )  

{

   DTWAIN_SOURCE TheSource;    /* The TWAIN Data Source */

   DTWAIN_BOOL RetVal;

   LONG Status;

 

   /* Check if TWAIN exists.  Return if TWAIN is not installed */

   if ( DTWAIN_IsTwainAvailable( ) == 0 )

           return;

 

   /* Initialize DTWAIN DLL, return if there is a problem */

   if ( DTWAIN_SysInitialize( ) == 0 )

          return;

 

   /* Select the Twain Source */

   TheSource = DTWAIN_SelectSource( );

 

   /* If no Source selected or there is a problem, uninitialize DLL and return */

   if ( TheSource == NULL )

   {

          DTWAIN_SysDestroy( );

          return;

   }

 

  /* Proceed to acquire image and save to a bmp file */

  RetVal = DTWAIN_AcquireFile(TheSource,   /* Source */

                                                           _T("test.bmp"),   /* File name */

                                                            DTWAIN_BMP,  /* File type is BMP */

                                                            DTWAIN_USENATIVE | DTWAIN_USENAME,  /* acquisition options */

                                                            DTWAIN_PT_DEFAULT, /* use default color type */

                                                            1,           /* Acquire 1 page */

                                                            TRUE,   /* Show the device's user interface */

                                                            FALSE. /* Do not keep Source open when finished. */

                                                            &Status);  /* returned status */

 

   /* Close down TWAIN,  Data Sources, etc. */

  DTWAIN_SysDestroy( );

}

 

__________________________________________________________________

 

The example above is a C or C++ function that demonstrates some of the power of DTWAIN.  The only functions that are DTWAIN specific are the following:

 

DTWAIN_IsTwainAvailable
DTWAIN_SysInitialize
DTWAIN_SelectSource (or another DTWAIN Source selection function)
DTWAIN_AcquireFile
DTWAIN_SysDestroy

 

DTWAIN_IsTwainAvailable is only optional.  DTWAIN will still work correctly in a system that has no TWAIN drivers installed by returning an error if a selection of a TWAIN device is attempted.

 

Note that DTWAIN has the ability to save the images to a file (as in the example above shows), or if your application wants the raw image data, a "handle" to the image data can be returned instead of writing the image data to a file.

 

If you are using C++, you can use the traditional DTWAIN interface as the program above demonstrates, or the DTWAIN C++ class wrapper.  Here is a console program using the C++ class wrapper that essentially does the same thing as the program above:

_________________________________________________________________

 

#include "cdtwain.h"

using namespace DTWAIN;

 

int main( )

{

  DTwainInterface TwainInterface;  // Initialize DTWAIN

   if ( TwainInterface.IsValid( ) )  // Check if valid interface

   {

          DTwainSource Source = DTwainSource::Select( );

           if ( Source.IsValid( ) )

      DTwainAcquirer( Source ).Acquire(_T("test.bmp") );

   }

} // shuts down TWAIN, DTWAIN automatically

 

_________________________________________________________________

 

Note how much shorter using the DTWAIN class wrapper code is.  This code

 

Initializes DTWAIN
Displays the TWAIN "Select Source" dialog
Shows the device's user interface.
The image is acquired and saved to an image file called "test.bmp".  The format of the image file is a Windows BMP.
Shuts down DTWAIN.

 

This is a lot of power in just a few lines of C++ code.