Question

I am using TWAIN in C++ and I am trying to set the DPI manually so that a user is not displayed with the scan dialog but instead the page just scans with set defaults and is stored for them. I need to set the DPI manually but I can not seem to get it to work. I have tried setting the capability using the ICAP_XRESOLUTION and the ICAP_YRESOLUTION. When I look at the image's info though it always shows the same resolution no matter what I set it to using the ICAPs. Is there another way to set the resolution of a scanned in image or is there just an additional step that needs to be done that I can not find in the documentation anywhere?

Thanks

Was it helpful?

Solution

I use ICAP_XRESOLUTION and the ICAP_YRESOLUTION to set the scan resolution for a scanner, and it works at least for a number of HP scanners.

Code snipset:

float x_res = 1200;
cap.Cap = ICAP_XRESOLUTION;
cap.ConType = TWON_ONEVALUE;
cap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE));
if(cap.hContainer)
{
    val_p = (pTW_ONEVALUE)GlobalLock(cap.hContainer);
    val_p->ItemType = TWTY_FIX32;
    TW_FIX32 fix32_val = FloatToFIX32(x_res);
    val_p->Item = *((pTW_INT32) &fix32_val);
    GlobalUnlock(cap.hContainer);
    ret_code = SetCapability(cap);
    GlobalFree(cap.hContainer);
}

TW_FIX32 FloatToFIX32(float i_float)
{
    TW_FIX32 Fix32_value;
    TW_INT32 value = (TW_INT32) (i_float * 65536.0 + 0.5);
    Fix32_value.Whole = LOWORD(value >> 16);
    Fix32_value.Frac = LOWORD(value & 0x0000ffffL);
    return Fix32_value;
}

The value should be of type TW_FIX32 which is a floating point format defined by twain (strange but true).

I hope it works for you!

OTHER TIPS

It should work the way.

But unfortunately we're not living in a perfect world. TWAIN drivers are among the most buggy drivers out there. Controlling the scanning process with TWAIN has always been a big headache because most drivers have never been tested without the scan dialog.

As far as I know there is also no test-suite for twain-drivers, so each of them will behave slightly different.

I wrote an OCR application back in the 90th and had to deal with these issues as well. What I ended up was having a list of supported scanners and a scanner module with lots of hacks and work-arounds for each different driver.

Take the ICAP_XRESOLUTION for example: The TWAIN documentation sais you have to send the resolution as a 32 bit float. Have you tried to set it using an integer instead? Or send it as float but put the bit-representation of an integer into the float, or vice versa. All this could work for the driver you're working with. Or it could not work at all.

I doubt the situation has changed much since then. So good luck getting it working on at least half of the machines that are out there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top