Solved

Using EZTwain library

Posted on 1998-11-24
2
450 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

863 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

23 Experts available now in Live!

Get 1:1 Help Now