Example: Acquire To File

Top  Previous  Next

The example below shows an example of a 'C' function that acquires one page from the Source using the File transfer mode.   In the example, the File Transfer mode is DTWAIN Mode (as opposed to DTWAIN_USESOURCEMODE).  Using DTWAIN Mode, the native mode is specified as the transfer mode to use when initially transfer the image from the Source.

 

If the image is acquired successfully from the Source, the file generated is a BMP file named "File1.BMP".

 

Example 1:

 

#include <dtwain.h>

DTWAIN_SOURCE SelectedSource;

LONG ErrStatus;

LONG FileFlags;

TCHAR szBuf[255];

TCHAR  FileNames[] = _T("File1.BMP");

DTWAIN_BOOL AcquiredOK;

 

void AcquireUsingFileMode1( )  

{

   /* See if TWAIN exists on the system */

   /* Quit if TWAIN is not there! */

   /* This call is added so that we can return immediately

       if no TWAIN is found */

   if ( !DTWAIN_IsTwainAvailable( ) ) return;

   /* Initialize the DTWAIN DLL.  Quit if error! */

   if ( !DTWAIN_SysInitialize( )) return;

 

   /* Set the File Flags */

   /* Use the native mode, the name of the files are supplied by a list */

   FileFlags = DTWAIN_USENATIVE | DTWAIN_USENAME;

   /* Select the Source using TWAIN dialog */

   SelectedSource = DTWAIN_SelectSource( );

 

   /* Only do this if a source is selected */

   if ( SelectedSource )

   {  

       AcquiredOK = DTWAIN_AcquireFile(

                                                    SelectedSource, /* TWAIN Source */

                                                    FileNames,    /* List of names */

                                                    DTWAIN_BMP,/* Acquire to BMP files */

                                                    FileFlags,       /* Flags */

                                                    DTWAIN_PT_DEFAULT, /* Use default pixel setting */

                                                    1,                /* Get only 1 page */

                                                    TRUE,         /* Show the UI */

                                                  TRUE,        /* Close Source when UI closes */

                                                   &ErrStatus   /* Error Status */

                                              );

 

       /* Check if acquisition worked */

       if ( AcquiredOK == FALSE )

     

        /* Didn't work */

          MessageBox(NULL, _T('TWAIN Acquisition failed!"), _T("TWAIN Error"), MB_ICONSTOP);

        /* End 'if AcquireArray */

     else

           MessageBox(NULL, _T("You now have a file called File1.BMP!"), _T("Image Saved"), MB_OK);

      }

      /* Close up shop.  Close all sources,

       TWAIN Sessions, the TWAIN Source Manager, */                

    /* End 'if SourceSelected... */

  DTWAIN_SysDestroy( );

/* End the function */

}

 

 

Example 2:

The example below shows an example of a 'C' function that acquires one page from the Source using DTWAIN_USESOURCEMODE.  This example checks if the Source has internal support for file transfer mode, and will only transfer if the TIFF format is supported.

 

#include <dtwain.h>

#include <tchar.h>

 

DTWAIN_SOURCE SelectedSource;

LONG ErrStatus;

LONG                FileFlags;

TCHAR szBuf[255];

DTWAIN_BOOL AcquiredOK;

 

void AcquireUsingFileMode2( )  

{

   /* See if TWAIN exists on the system */

   /* Quit if TWAIN is not there! */

   /* This call is added so that we can return immediately

       if no TWAIN is found */

   if ( !DTWAIN_IsTwainAvailable( ) ) return;

   /* Initialize the DTWAIN DLL.  Quit if error! */

   if ( !DTWAIN_SysInitialize( )) return;

 

   /* Set the File Flags */

   /* Use the internal Source mode, the name of the file is supplied by the user */

   FileFlags = DTWAIN_USESOURCEMODE | DTWAIN_USEPROMPT;

 

   /* Select the Source using TWAIN dialog */

   SelectedSource = DTWAIN_SelectSource( );

 

   /* Only do this if a source is selected */

   if ( SelectedSource )

   {  

       /* Check if Source supports TIFF file transfers */

      if ( !DTWAIN_IsFileXferSupported( SelectedSource,  DTWAIN_FF_TIFF ) )

      {

           TCHAR szBuf[256];

           TCHAR szSource[33];

          DTWAIN_GetSourceProductName(SelectedSource, szSource, 32);

           wsprintf(szBuf, _T("Sorry, the Source "%s"not support TIFF file transfers :-("), szSource);

           MessageBox(NULL, szBuf, _T("No support"), MB_ICONSTOP);

           /* Not supported, so clean up and return */

          DTWAIN_SysDestroy( );

           return;

        }

       /* Source supports TIFF file transfer.  Start transfer! */

       AcquiredOK = DTWAIN_AcquireFile(

                               SelectedSource, /* TWAIN Source */

                               DTWAIN_FF_TIFF, /* Acquire to TIFF file */

                               NULL,  /* List of names */

                               FileFlags,                /* Flags */

                               DTWAIN_PT_DEFAULT, /* Use default pixel setting */

                              1,                /* Get only 1 page */

                              TRUE,  /* Show the UI */

                              &ErrStatus /* Error Status */

                            );

 

       /* Check if acquisition worked */

       if ( AcquiredOK == FALSE )

       {

           /* Didn't work */

          MessageBox(NULL,

                               _T('TWAIN Acquisition failed!'),

                               _T("TWAIN Error'),

                               MB_ICONSTOP);

         /* End 'if AcquireArray */

       }

          /* Close up shop.  Close all sources,

          TWAIN Sessions, the TWAIN Source Manager, */

    }/* End 'if SourceSelected... */

  DTWAIN_SysDestroy( );

/* End the function */

}