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?
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::PreTranslateMessag
{
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::TransferNativeImag
{
// 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,
// 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