0

I'm trying to use the Leadtools API version 21 for automatically scanning some documents while setting some properties from code (do not want to show the TWAIN dialog). for example I set the scan DPI to 300 using L_TwainSetResolution(), but the image I get inside the bitmap callback always has resolution of 96x96.
Here is a sudo code of what I have done (it runs in a secondary thread and the unlock has been done in the main thread):

void CheckRetCode(int rc)
{
    if (SUCCESS != rc)
    {
        L_TCHAR errMsg[1024];

        memset(errMsg, 0, sizeof(errMsg));
        L_GetFriendlyErrorMessage(rc, errMsg, 1024, L_FALSE);

        throw TLeadException(errMsg, rc);
    }
}
L_INT EXT_CALLBACK GetBmpCB(HTWAINSESSION hS, pBITMAPHANDLE pBitmap, L_VOID* pUserData)
{
    // in here pBitmap->XResolution and pBitmap->YResolution are always 96
    // but I have clearly set them to 300
    
    // process image here
    
    L_FreeBitmap(pBitmap); // free the image
    return SUCCESS;
} 
void OnThreadExecute(void)
{
    HTWAINSESSION hSession = nullptr;
    APPLICATIONDATA appData;
    L_INT nRet;
    L_TCHAR pszTwnSourceName[1024];
    LTWAINSOURCE sInfo;
    TW_FIX32 XRes = L_TwainFloatToFix32(300.0);
    TW_FIX32 YRes = L_TwainFloatToFix32(300.0);
    BITMAPHANDLE tBmp;

    memset(&tBmp, 0, sizeof(BITMAPHANDLE));
    tBmp.uStructSize = sizeof(BITMAPHANDLE);
    memset(&appData, 0, sizeof(APPLICATIONDATA));
    appData.uStructSize = sizeof(APPLICATIONDATA);
    appData.hWnd = hWnd;// hWnd is valid handle of my main window
    appData.uLanguage = TWLG_ENGLISH_USA;
    appData.uCountry = TWCY_USA;
    wcscpy(appData.szManufacturerName, L"MyCompanyName");
    wcscpy(appData.szAppProductFamily, L"MyProductName");
    wcscpy(appData.szAppName, appData.szAppProductFamily);
    wcscpy(appData.szVersionInfo, L"Version 0.1.0.1");
    nRet = L_TwainInitSession2(&hSession, &appData, LTWAIN_INIT_MULTI_THREADED);
    CheckRetCode(nRet);here
    memset(pszTwnSourceName, 0, sizeof(pszTwnSourceName));
    wcscpy(pszTwnSourceName, L"EPSON Artisan837/PX830"); 
    sInfo.uStructSize = sizeof(LTWAINSOURCE);
    sInfo.pszTwainSourceName = pszTwnSourceName;
    CheckRetCode(L_TwainSelectSource(hSession, &sInfo));
    CheckRetCode(L_TwainStartCapsNeg(hSession));
    CheckRetCode(L_TwainSetImageUnit(hSession, TWUN_INCHES)); 
    CheckRetCode(L_TwainEnableDuplex(hSession, FALSE));
    CheckRetCode(L_TwainSetResolution(hSession, &XRes, &YRes)); // setting the res to 300 x 300
    CheckRetCode(L_TwainEndCapsNeg(hSession));
    L_TwainAcquire(hSession, &tBmp, sizeof(BITMAPHANDLE), GetBmpCB, 0, NULL, NULL);
    if(tBmp.Flags.Allocated)
        L_FreeBitmap(&tBmp);
}

By the way the scanned image has the correct number of pixels. If I scan a 8.5x11 page, I get an image that is 2550x3300 pixels, but XResolution and YResolution are set to 96 which causes the saved image to be 26.5"x34.375".

Thank you
Sam

Sam
  • 2,473
  • 3
  • 18
  • 29

1 Answers1

0

I tested with 4 different Twain drivers and got the following results:

  1. One of them did not support 300 DPI, so it returned error “Bad value” when calling L_TwainSetResolution(). However, it returned correct image size for the actual DPI it supported, which is 100.
  2. The other 3 supported different DPI values, and returned correct image size and DPI values in the callback’s pBitmap.

The only major difference between my code and yours is that I called L_TwainEndSession(). If your code doesn’t include it, make sure to call it once for each call of L_TwainInitSession/L_TwainInitSession2.

If that’s not the cause of your problem, try to test with more than one Twain driver and see if the problem is specific to one driver. If it’s not, put your code in a small test program and email it to support@leadtools.com and we will check it for you.

LEADTOOLS Support
  • 2,755
  • 1
  • 12
  • 12
  • Are you saying this might be the fault of my scanner's TWAIN driver? – Sam Jan 11 '21 at 16:44
  • Testing with multiple scanners can either eliminate or confirm this possibility. If you don't have more than one Twain driver, you could test using a virtual scanner, either [32-bit](https://cfhcable.dl.sourceforge.net/project/twain-samples/TWAIN%202%20Sample%20Data%20Source/TWAIN%20DS%202.1.3/twainds.win32.installer.2.1.3.msi) or [64-bit](https://netix.dl.sourceforge.net/project/twain-samples/TWAIN%202%20Sample%20Data%20Source/TWAIN%20DS%202.1.3/twainds.win64.installer.2.1.3.msi), depending on your platform. – LEADTOOLS Support Jan 11 '21 at 18:03