Example: Setting Device Capabilities

Top  Previous  Next

The example below sets the device capability for DTWAIN_CV_ICAPBRIGHTNESS (the brightness setting).  This example assumes that DTWAIN_SysInitialize has been called successfully.  Note that these examples may not work for all Sources, since some Sources will not support the capabilities used in the examples.

 

Example 1:

 

#include <dtwain.h>

#include <tchar.h>

 

/* Set the brightness */

void SetBrightness( )

{

  DTWAIN_ARRAY  Brightness;

  DTWAIN_BOOL bOk;

  DTWAIN_SOURCE SelectedSource;

 

    /* Select the Source.  Return if none selected */

   SelectedSource = DTWAIN_SelectSource();

   if ( !SelectedSource)                

       return;

 

   /* Create the array for the brightness value */

   Brightness = DTWAIN_ArrayCreateFromCap(DTWAIN_CV_ICAPBRIGHTNESS, 0);

 

   /* The Brightness takes DTWAIN_FLOAT's. I found the info here.

       Add the 20.0 to the array */

  DTWAIN_ArrayAddFloat(Brightness, 20.0);

 

   /* Now call function to set the capability */

   bOk = DTWAIN_SetCapValues(SelectedSource,

                                                 DTWAIN_CV_ICAPBRIGHTNESS,

                                                 DTWAIN_CAPSET,

                                                 Brightness);

 

   /* Check if capability failed to set */

   if ( bOk == FALSE )

      MessageBox(NULL, _T("Sorry.  Capability could not be set"), _T("Error"), MB_ICONSTOP);

 

   /* Destroy array */

  DTWAIN_ArrayDestroy(Brightness);

  DTWAIN_CloseSource( SelectedSource );

}

 

The example below sets the device capability for DTWAIN_CV_ICAPFRAMES  If so, it sets the capability using the DTWAIN_FRAME type as the data.  If you want to know how to set the TW_FRAME type, this example shows this in action.  Note the use of DTWAIN_ArraySetAt instead of DTWAIN_ArrayAdd.

 

Example 2:

 

#include <dtwain.h>

void SetFrameValues()

{

  DTWAIN_FRAME Frame;

  DTWAIN_BOOL bOk;

  DTWAIN_SOURCE SelectedSource;

  DTWAIN_ARRAY FrameArray;

 

   /* Select the Source.  Return if none selected */

   SelectedSource = DTWAIN_SelectSource();

   if ( !SelectedSource)

       return;

 

   /* Check if brightness is supported by the Source */

   if ( !DTWAIN_IsCapSupported(Source, DTWAIN_CV_ICAPFRAMES) )

       return;

 

   /* Create a compatible array for the ICAP_FRAME capability

       Note that we stated that there is room for 1 value already set up*/

   FrameArray = DTWAIN_ArrayCreateFromCap(DTWAIN_CV_ICAPFRAMES, 1);

 

   /*Set the Frame values */

   Frame = DTWAIN_FrameCreate(0.0, 0.0, 8.5, 11.0);

 

   /* Add the Frame to the array   Note the use of DTWAIN_ArraySetAt,

        since there is one slot that we made available when we called

        DTWAIN_ArrayCreateFromCap */

  DTWAIN_ArraySetAt(FrameArray, 0, &Frame);

 

   /* Now call function to set the capability */

   bOk = DTWAIN_SetCapValues(SelectedSource,

                                                                        DTWAIN_CV_ICAPFRAMES,

                                                                        DTWAIN_CAPSET,

                                                                        FrameArray);

 

   /* Check if capability failed to set */

   if ( bOk == FALSE )

      MessageBox(NULL, "Sorry.  Capability could not be set", "Error", MB_ICONSTOP);

 

   /* Destroy array */

  DTWAIN_ArrayDestroy(Brightness);

  DTWAIN_CloseSource( SelectedSource );

}

 

 

The example below resets the device capability for DTWAIN_CV_ICAPBRIGHTNESS.

 

Example 3:

 

#include <dtwain.h>

/* Reset the brightness */

void ResetBrightness( )

{

  DTWAIN_BOOL bOk;

  DTWAIN_SOURCE SelectedSource;

 

   /* Select the Source.  Return if none selected */

   SelectedSource = DTWAIN_SelectSource();

   if ( !SelectedSource)                

       return;

 

   /* Check if brightness is supported by the Source */

   if ( !DTWAIN_IsCapSupported(Source, DTWAIN_CV_ICAPBRIGHTNESS) )

       return;

 

   /* Now call function to set the capability */

   bOk = DTWAIN_SetCapValues(SelectedSource,

                                                                        DTWAIN_CV_ICAPBRIGHTNESS,

                                                                        DTWAIN_CAPRESET,

                                                                       NULL);

 

   /* Check if capability failed to set */

   if ( bOk == FALSE )

      MessageBox(NULL, "Sorry.  Capability could not be set", "Error", MB_ICONSTOP);

 

   /* Destroy array */

  DTWAIN_ArrayDestroy(Brightness);

  DTWAIN_CloseSource( SelectedSource );

}

 

 

The example below sets the device capability for DTWAIN_CV_ICAPXRESOLUTIONS.  Multiple values are set (this is assuming that the Source is able to set multiple values).  This allows the Source to limit the list of available resolutions.

 

Example 4:

 

#include <dtwain.h>

void SetMultiCapValues()

{

  DTWAIN_ARRAY  Resolution;

  DTWAIN_BOOL bOk;

  DTWAIN_SOURCE SelectedSource;

   LONG Container;

 

   /* Select the Source.  Return if none selected */

   SelectedSource = DTWAIN_SelectSource( );

   if ( !SelectedSource)                

       return;

 

   /* Check if resolution is supported by the Source */

   if ( !DTWAIN_IsCapSupported(Source, DTWAIN_CV_ICAPXRESOLUTION) )

       return;

   /* See if the container type for setting supports multiple values */

   Container = DTWAIN_GetCapContainer(Source,

                                                                  DTWAIN_CV_ICAPXRESOLUTION,

                                                                   DTWAIN_CAPSETAVAILABLE);

 

   /* Check if TW_RANGE is the container type */

   if ( Container == DTWAIN_CONTRANGE )

   {

       DTWAIN_FLOAT RangeValues[5]=10.0, 150.0, 10.0, 100.0, 150.0;

       /* Note that a DTWAIN_ARRAY is compatible with a DTWAIN_RANGE,

           therefore the next assignment is valid */

       Resolutions = DTWAIN_RangeCreate(DTWAIN_RANGEFLOAT);

      DTWAIN_RangeSetAll(&Resolution, &RangeValue[0],

                                                         &RangeValue[1],

                                                         &RangeValue[2],

                                                         &RangeValue[3],

                                                         &RangeValue[4]);

 

   }

   else

   if ( Container == DTWAIN_CONTENUMERATION )

   {

       LONG index;

      DTWAIN_FLOAT ResolutionVal[] = 100.0, 150.0,300.0;

       /* Create the array for the Range value */

       Resolutions = DTWAIN_ArrayCreateFromCap(

                                    DTWAIN_CV_ICAPXRESOLUTION, 3);

       for ( index = 0; index < 3; index++ )

          DTWAIN_ArraySetAt(Resolutions, i, &Resolutions[index]);

   }

   else /* This is a DTWAIN_ONEVALUE or error.

             This will not allow constraint of the list */

       return;

 

   /* Now call function to set the capability */

   bOk = DTWAIN_SetCapValues(SelectedSource,

                                                                        DTWAIN_CV_ICAPXRESOLUTION,

                                                                        DTWAIN_CAPSETAVAILABLE,

                                                                         Resolutions);

 

   /* Check if capability failed to set */

   if ( bOk == FALSE )

      MessageBox(NULL, "Sorry.  Capability could not be set", "Error", MB_ICONSTOP);

 

   /* Destroy array */

  DTWAIN_ArrayDestroy(Resolution);

  DTWAIN_CloseSource( SelectedSource );

}