Solved

Using EZTwain library

Posted on 1998-11-24
2
453 Views
Last Modified: 2012-05-04
Now I'm using EZTwain library to acquire image from scanner, using C++Builder. The code to acquire image is

  HANDLE hDib;
  hDib = TWAIN_AcquireNative(hwnd, PIXEL_TYPE);

where hwnd is MainForm->Handle  /* my main window handle */
  and PEXEL_TYPE is 0           /* any type */

Is it correct?? if yes, I try to get the bitmap information by
 
  LPBITMAPINFOHEADER lpbi;  
  lpbi = (LPBITMAPINFOHEADER)hDib;

BUT lpbi has incorrect result. What's wrong with it?
0
Comment
Question by:mrbird
2 Comments
 

Author Comment

by:mrbird
ID: 1178534
Adjusted points to 250
0
 
LVL 7

Accepted Solution

by:
psdavis earned 250 total points
ID: 1178535
Hello again, MrBird...

Here's my version of TransferNativeImage.  I know that this works.  I've always had trouble with EzTwain.  It seemed like it just barely worked only in it's own circumstances.  

I've added the message loop directly inside of the class in the PreTranslateMessage function, and I believe that they require you to put it inside of your application.  That's where I always had my problem, make sure it looks the same (similar) in yours.

BOOL CTwain::PreTranslateMessage( MSG* pMsg )
{
   if( m_eCurrentState < TWAIN_SOURCE_ENABLED )
      return FALSE;

   TW_EVENT      twEvent;

      twEvent.pEvent    = (TW_MEMREF) pMsg;
      twEvent.TWMessage = MSG_NULL;

   TW_UINT16 twRC = (*m_lpDSM_Entry)( &m_AppId, &m_SourceId, DG_CONTROL, DAT_EVENT, MSG_PROCESSEVENT, (TW_MEMREF) &twEvent );

   if( twRC == TWRC_NOTDSEVENT )
      return FALSE;

      switch( twEvent.TWMessage )
   {
// Retrieve the image!

   case MSG_XFERREADY:
      TRACE0( "TWAIN: Pre-Translate XFerReady\n" );
      TransferNativeImage( );     // XferReady( lpmsg );
      break;

// Default callback closes the source

   case MSG_CLOSEDSREQ:
      TRACE0( "TWAIN: Pre-Translate CloseDSReq\n" );
      StopAcquire( );                  // CloseDSReq
      break;

// No message returned

   case MSG_NULL:
      break;
            } // switch

      return TRUE;
   }


BOOL CTwain::TransferNativeImage( )
{
// Do until there are no more pending transfers
// explicitly initialize the our flags

   BOOL  bReturn = FALSE;
   int   iStatus;

   TW_IMAGEINFO    twImageInfo;
   TW_PENDINGXFERS twPendingXfer;

   memset( &twPendingXfer, 0, sizeof( TW_PENDINGXFERS ));
   twPendingXfer.Count = 0;

   TRACE0( "TWAIN: TransferNative\n" );

   do
   {
      TRACE1( "TWAIN: TransferNative IMAGENATIVEXFER begin: %d\n", twPendingXfer.Count );

      TW_UINT16 twRC2;
      TW_UINT16 twRC = (*m_lpDSM_Entry )( &m_AppId, &m_SourceId, DG_IMAGE, DAT_IMAGEINFO, MSG_GET, (TW_MEMREF) &twImageInfo );

      if( twRC != TWRC_SUCCESS )
      {
         continue;
         }

      TW_UINT32 hBitmap = NULL;

      twRC = (*m_lpDSM_Entry )( &m_AppId, &m_SourceId, DG_IMAGE, DAT_IMAGENATIVEXFER, MSG_GET, (TW_MEMREF) &hBitmap );

      switch( twRC )
      {
         case TWRC_XFERDONE:  // Session is in State 7
                     
            TRACE0( "TWAIN: TransferNative XFERDONE\n" );

         // Acknowledge the end of the transfer and transition to state 6/5

            twRC2 = ( *m_lpDSM_Entry)(&m_AppId, &m_SourceId, DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, (TW_MEMREF) &twPendingXfer );
               
         // close the DSM and DS

            if( twPendingXfer.Count == 0 )
            {      
               LPBITMAPINFOHEADER pBitmap = (LPBITMAPINFOHEADER) GlobalLock( (void*) hBitmap );
//             BYTE* pBitmap = (BYTE*) GlobalLock( (void*) hBitmap );

               freeimage( m_vTwainImage );
               iStatus = dibtoimage( (BYTE*) pBitmap, m_vTwainImage );

            // Free the bitmap now.  The dibtoimage function copied it for us!

               GlobalUnlock( (void*) hBitmap );
               GlobalFree  ( (void*) hBitmap );

               TRACE0( "TWAIN: TransferNative CopyImage\n" );

            // Send a message to the main app that we're done!!

               SendMessage( m_hWnd, PM_XFERDONE, 0, 0);

               StopAcquire( );
               }

            PostMessage( m_hWnd, WM_COMMAND, PM_XFERDONE, 0 );
            TRACE0( "TWAIN: TransferNative PM_XFERDONE\n" );
            break;

         // the user canceled or wants to rescan the image
         // something wrong, abort the transfer and delete the image
         // pass a null ptr back to App

         case TWRC_CANCEL:   // Session is in State 7
         case TWRC_FAILURE:  
         default:
               
         // Source (or User) Canceled Transfer transistion to state 6/5

            twRC2 = (*m_lpDSM_Entry)(&m_AppId, &m_SourceId, DG_CONTROL, DAT_PENDINGXFERS, MSG_ENDXFER, (TW_MEMREF) &twPendingXfer );
               
        // Close the DSM and DS

            if( twPendingXfer.Count == 0)
               StopAcquire( );

            PostMessage( m_hWnd, WM_COMMAND, PM_XFERDONE, 0 );
            TRACE0( "TWAIN: TransferNative TWRC_FAILURE\n" );

            break;
         }

      } while( twPendingXfer.Count != 0 );

   TRACE0( "TWAIN: TransferNative exiting\n" );

   return bReturn;
   }

Phillip
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question