Solved

Using EZTwain library

Posted on 1998-11-24
2
443 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
Comment Utility
Adjusted points to 250
0
 
LVL 7

Accepted Solution

by:
psdavis earned 250 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now