Example 4: Acquire in Native mode |
Top Previous Next |
Example: Start DTWAIN, Select a Source, Acquire all pages in Native mode. Destroy the generated Device Independent Bitmaps.
#include "cdtwain.h" #include <iostream>
using namespace DTWAIN; 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::Select( ); if ( !Source.IsValid( ) ) return 0;
// Declare an acquisition object, and attach the source to it DTwainAcquirer Acq(Source);
// Set the acquire type to Native Acq.SetAcquireType(TWAIN_ACQUIRENATIVE);
// Set the maximum pages to acquire Acq.SetMaxPages( DTWAIN_ACQUIREALL );
// Start the acquisition if ( !Acq.Acquire( ) ) { // Acquisition failed return 0; }
// get all the bitmaps (DIBs) that have been acquired DTwainAcquisitionArray AllDibs; Acq.GetAllAcquisitions( AllDibs );
// AllDibs is a two-dimensional "array" (actually a two-dimensional vector). // The first index is the acquisition (which is an array of image data), the second index is the page // data itself. The page data is stored in a DTwainImageData object. // So, if there are 2 acquisitions, and in acquisition 1 there are 3 pages, // and acquisition 2 has 1 page, the following is true: // // AllDibs[0][0] is acquisition 1, page 1's DIB (represented as a DTwainImageData object) // AllDibs[0][1] is acquisition 1, page 2's DIB (represented as a DTwainImageData object) // AllDibs[0][2] is acquisition 1, page 3's DIB (represented as a DTwainImageData object) // AllDibs[1][0] is acquisition 2, page 1's DIB (represented as a DTwainImageData object) // // To get a handle to the DIB for a particular acquisition and page // HANDLE hDib = AllDibs[0][1].GetImageData( ); // Handle to acquisition 1, page 2 image data. // // To get an array of DIBs for a particular acquisition: // DTwainImageDataArray DibsForAcq1 = AllDibs[0]; // All Dibs for acquisition 1 // // To make sure that you are accessing a valid subscript, it is safer to first get the number of acquisitions and pages // instead of using operator [] first.
cout << "The number of acquisitions attempted are " << AllDibs.size( ) << endl;
// Now get the number of acquisitions. int nAcquisitions = AllDibs.size( ); for ( int i = 0; i < nAcquisitions; ++i ) { // Get array of DTwainImageData for acquisition i DTwainImageDataArray AData = AllDibs[i]; cout << "For acquisition " << i << ", there are (is) " << AData.size( ) << " Dibs (pages)" << endl;
// Now free the image data here. This does it in a loop, but there are other ways to do // this without a loop. Also note you can use the traditional array operator [] to access the // image information, but for illustrative purposes, we will use the STL iterator to loop through the // array of DTwainImageInfo DTwainImageDataArray::iterator start = AData.begin( ); DTwainImageDataArray::iterator stop = AData.end( );
DTwainImageData ImageData; // represents the raw image data for a single page while ( start != stop ) { ImageData = *start; // get the current DTwainImageData object HANDLE hDib = ImageData.GetImageData( ); GlobalFree( hDib ); // Free the data here ++start; } } } // Twain is shut down automatically on exit
|