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(SelectedSource, 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_ArraySetAtFrame instead of DTWAIN_ArraySetAt.

 

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(SelectedSource, DTWAIN_CV_ICAPFRAMES, 1);

 

   /*Set the Frame values */

   DTWAIN_ArraySetAtFrame(FrameArray, 0, 0.0, 0.0, 8.5, 11.0);

 

   /* 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(FrameArray);

  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);

 

  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, bSet = FALSE;

   DTWAIN_SOURCE SelectedSource;

   LONG Container;


   /* The DTWAIN_CV_ICAPXRESOLUTION capability potentially has three container types that can be used 

       when using DTWAIN_CAPSETCONSTRAINT as the "set type" */

   /* This also could have been determined by calling DTWAIN_GetCapContainer, which would have returned

       the boolean OR combination of DTWAIN_CONTRANGE, DTWAIN_CONTENUMERATION, and DTWAIN_CONTONEVALUE */


   LONG ContainerType[] = {DTWAIN_CONTRANGE, DTWAIN_CONTENUMERATION, DTWAIN_CONTONEVALUE };

   LONG CurrentContainerType;

   

   /* 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;


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

   { 

       /* We will try the RANGE, ENUMERATION, and ONEVALUE containers, in that order */

       if ( CurrentContainerType == 0 ) /* 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 */

           Resolution = DTWAIN_RangeCreate(DTWAIN_RANGEFLOAT);

           DTWAIN_RangeSetAll(&Resolution, &RangeValues[0],

                                                  &RangeValues[1],

                                                  &RangeValues[2],

                                                  &RangeValues[3],

                                                  &RangeValues[4]);

      }

      else if ( CurrentContainerType == 1 ) /* DTWAIN_CONTENUMERATION */

      {

          LONG index;

          DTWAIN_FLOAT ResolutionVal[] = { 100.0, 150.0,300.0 };

          /* Create the array for the Enumeration */

          Resolution = DTWAIN_ArrayCreateFromCap(SelectedSource, DTWAIN_CV_ICAPXRESOLUTION, 3);

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

              DTWAIN_ArraySetAtFloat(Resolution, index, ResolutionVal[index]);

      }

      else /* DTWAIN_CONTONEVALUE */

      {

          DTWAIN_FLOAT ResolutionVal = 100.0};

          /* Create the array for the single value */

          Resolution = DTWAIN_ArrayCreateFromCap(SelectedSource, DTWAIN_CV_ICAPXRESOLUTION, 1);

          DTWAIN_ArraySetAtFloat(Resolution, 0, ResolutionVal);

      }


      /* Now call function to set the capability 

          Note we call DTWAIN_SetCapValuesEx, since we want to use the current container type */

      bOk = DTWAIN_SetCapValuesEx(SelectedSource,

                                                                DTWAIN_CV_ICAPXRESOLUTION,

                                                                DTWAIN_CAPSETCONSTRAINT,

                                                                ContainerType[CurrentContainerType],

                                                                Resolution);

 

      DTWAIN_ArrayDestroy(Resolution);


      /* Check if capability failed to set */

      if ( bOk == FALSE )

      {

           /* Try again with the next container type */

           continue;

      }

      else

      {

           // The set worked, so no need to continue the loop
           bSet = TRUE;

           break;

      }

 }


 /* Check if capability failed to set */

 if ( bSet == FALSE )

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

 else

     MessageBox(NULL, "Capability was set", "Success", MB_OK);

    

 DTWAIN_CloseSource( SelectedSource );

}