Example 11: Monitoring acquisition using listeners

Top  Previous  Next

Example:  Acquire To file.  Use a Listener to monitor the acquisition success/failure.

 

Note:  The DTwainListener object must be valid (not go out of scope) for the lifetime of the acquisition.  You can add as many listener objects as desired.

 

#include "cdtwain.h"

#include <string>

#include <iostream>

 

using namespace DTWAIN;

using namespace std;

 

class MyFileListener : public DTwainListener

{

public:

     // This function is called when a transfer is ready

     LRESULT OnTransferReady( DTwainSource& Source, LONG UserParam )

     {

         // Get the image information

        DTwainImageInfo Info;

        Source.GetImageInfo( Info );

     

         cout << "X-Res: " << Info.XResolution << endl;

         cout << "Y-Res: " << Info.YResolution << endl;

 

        // always return 1 except for special cases.

         return 1;

     }

 

     // This is called if there is a file save error

     LRESULT OnFileSaveError( DTwainSource& Source, LONG UserParam )

     {

         DisplayError( );

         return 1;

     }

 

     // This is called if the single page file or multi-page file was saved

     // successfully

     LRESULT OnFileSaveOk( DTwainSource& Source, LONG UserParam )

     {

         cout << "File was saved successfully" << endl;

         return 1;

     }

 

     // This is called if there is a particular page from a multi-page file acquisition fails

     LRESULT OnFilePageSaveError( DTwainSource& Source, LONG UserParam )

     {

         DisplayError( );

         return 1;

     }

 

     // This is called if there is a file save was cancelled

     LRESULT OnFileSaveCancelled( DTwainSource& Source, LONG UserParam )

     {

         cout << "File acquisition cancelled " << endl;

         return 1;

     }

 

     // Called when file page was created and saved to the single-page or multi-page file

     // successfully

     LRESULT OnFilePageSaveOk( DTwainSource& Source, LONG UserParam )

     {

         cout << "File page saved OK" << endl;

         return 1;

     }

   

     // called when transfer is done.

     LRESULT OnTransferDone( DTwainSource& Source, LONG UserParam )

     {

         cout << "Transfer successful" << endl;

         return 1;

     }

 

      private:

          void DisplayError( )

          {

              std::string sError = DTwainInterface::GetLastErrorString( );

              cout << sError << endl;

          }

};

 

int main( )

{

  DTwainInterface TI;  // Initialize DTWAIN

  DTwainSource Source = DTwainSource::Select( );

   if ( !Source.IsValid( ) )

       return 0;

   

    DTwainAcquirer Acq( Source );

 

    // Create a listener

    MyFileListener FL;

 

    // Add it to the list of listeners in the DTwainAcquirer object

    Acq.AddListener( &FL );

 

    // Go

    Acq.Acquire("DTWAIN.BMP");      

}