Example 7: Acquire using compressed buffer transfer.

Top  Previous  Next

Example:  Advanced acquiring using the buffered mode.  Page data is saved as individual blobs of string data.

 

#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include "cdtwain.h"  // CDTwain

 

using namespace DTWAIN;

using namespace std;

 

class MyBufferedListener : public DTwainBufferedListener

{

   private:

      std::vector<std::string> ImageData;

     

   public:

 

       MyBufferedListener( DTwainBufferedTransfer *pTransfer) : DTwainBufferedListener("", pTransfer ) { }

 

       // Save the page of data to the vector when last strip has been processed

       LRESULT OnEndProcessingStrips( DTwainSource& Source, LONG UserParam )

       {

           ImageData.push_back( GetData( ) );

           return 1;

       }

 

       std::vector<std::string>& GetImageData( ) { return ImageData; }

};

 

using namespace std;

 

int main( )

{

  // Initialize DTWAIN - return if initialization does not work

  DTwainInterface TI;

  if ( !TI.IsValid( ) )

      return 0;

 

  // Declare a Source and assign the default TWAIN source to it

  DTwainSource Source = DTwainSource::SelectDefault( );

  if ( !Source.IsValid( ) )

     return 0;

 

  // Create an acquire object, attach the source

  DTwainAcquirer Acq( Source );

 

  // Set the acquire type

  Acq.SetAcquireType( TWAIN_ACQUIREBUFFERED );

 

  // Set the number of pages to acquire to all pages

  Acq.SetMaxPages( DTWAIN_MAXACQUIRE );

 

  // Create a buffered transfer helper object, and attach Source to the object,

  DTwainBufferedTransfer BT(Source);

 

  // Set the compression type

  // This could be any other compression type supported by the Source  

  // For example: COMPRESS_JPEG, COMPRESS_RLE8, etc.

  // But for the example, we use no compression

  BT.SetCompressionType(COMPRESS_NONE);

 

  // Now set the strip size

  BT.SetStripSize( BT.GetPreferredStripSize( ) );

 

  // Attach to a special buffered transfer listener that saves data to memory

  MyBufferedListener Listener(&BT );

 

  // Add listener to DTwainAcquirer object

  Acq.AddListener( &Listener );

 

  // Go.

  Acq.Acquire( BT );

 

 // Get the data from the listener

 std::vector<std::string>& TheData = Listener.GetImageData( );

 

 // Get the number of pages

 cout << "The number of pages saved is " << TheData.size( ) << endl;

 

 // You can now save the images in TheData to a file, or for any other purpose.

 // Here is page 1 data, written to a file:

 ofstream ofs("page1.bin", ios::out | ios::binary);

 if ( TheData[0].size( ) > 0 )

     ofs.write((const char *)TheData[0].data( ), TheData[0].size( ) );

}

 

This example shows the usage of the DTwainBufferedListener.  By default, this listener saves the raw image data to files.  Since we want to save the data to memory instead of files, a class called MyBufferedListener is derived from DTwainBufferedListener, and the OnEndProcessingStrips( ) virtual function is overridden (this "turns off" the file saving process).  In the overridden OnEndProcessingStrips( ), the image data that has been saved is stored in a vector of pages by calling DTwainBufferedListener::GetData( ).

 

When the processing is completed, the vector will contain data for page 1, page 2, etc., and it's just a matter of calling GetImageData( ) to retrieve the data.