Twain problems

Mikael Olofsson mikael at isy.liu.se
Mon Jan 31 11:59:28 EST 2005


Hi all!

Sorry for this rather lengthy post. It's a long preamble followed by a few 
questions based on that.

Here's my situation: I am trying to write a specialized scanner utility for 
my own needs, and I want to use the module twain to control my scanner. I 
haven't used the twain module before, which is most certainly why I'm facing 
problems. My problem is to interprete some of the information I get from the 
scanner. The scanner, by the way, is an HP LaserJet 3020, which is a 
multi-toy (printer/scanner/copyer), and I use Python 2.3 on Windows XP.

I set up communication with the scanner as follows (modulo a few ifs to take 
care of unsuccessful connections). Here self is an instance of a class with 
Tkinter.Tk as its base class.

    self.SM = twain.SourceManager(self.winfo_id(),ProductName='foo')
    self.SD = self.SM.OpenSource()

The TWAIN dialog pops up nicely, and I get two options: "hp LaserJet 3020 
TWAIN 1.0 (32-32)" and "WIA-hp LaserJet 3020 1.0 (32-32)". The first choice 
is HPs interface to the scanner, while the second seems to be a scanner 
interface that comes with Windows.

One issue arises if I choose "WIA-hp LaserJet 3020 1.0 (32-32)" and then do 
the following:

    capVal=self.SD.GetCapability(twain.ICAP_CONTRAST)

After that capVal is

    {'StepSize': 1,
    'DefaultValue': 0,
    'CurrentValue': 0,
    'MaxValue': 1000,
    'MinValue': 64536}

My educated guess is that 64536 should be interpreted as -1000, since 64536 
is -1000 modulo 2**16. One problem is that the twain type is not specified. 
So, perhaps I can find the twain type in some other way. The check

    capDef=self.SD.GetCapabilityDefault(twain.ICAP_CONTRAST)

gives me capDef==( 7, 0.0 ). According to my introspection of the twain 
module, the type 7 is twain.TWTY_FIX32. Googling around gives me the 
impression that this corresponds to 32 bits of which the first 16 bits 
correspond to the integer part, and the last 16 bits correspond to the 
fraction part. I thought that the twain module would take care of sign 
issues for me, but it does not seem to do so. OK, I can probably live with 
that. In any case, I guess I'll have to.

OK, that's perhaps not so tough.

Another issue arises if I choose "hp LaserJet 3020 TWAIN 1.0 (32-32)" and 
then do the following:

    capVal=self.SD.GetCapability(twain.ICAP_XSCALING)

After that capVal is

    {'StepSize': 429457408,
    'DefaultValue': 1,
    'CurrentValue': 1,
    'MaxValue': 6,
    'MinValue': 429457408}

Now I'm in trouble. The problem is to interprete capVal['StepSize'] and 
capVal['MinValue']. Both should correspond to some small positive value. 
Here the check

    capDef=self.SD.GetCapabilityDefault(twain.ICAP_XSCALING)

gives me capDef==( 4, 100 ). More introspection says that type 4 is 
twain.TWTY_UINT16. So, again googling around, and I find that this 
corresponds to an unsigned 16bit integer, but that doesn't help me much 
since 429457408 > 2**16. I also notice that capDef[1]==100, while 
capVal['DefaultValue']==1, so there must be a scaling factor of 100 
(percents, yes I know). But that doesn't change the fact that I'm completely 
lost here.

Finally, here are my questions:
Can anyone enlighten me? From the above, I get the impression that the 
dictionary returned by self.SD.GetCapability in these cases give me integer 
approximations of the current and default values. Is that a correct 
interpretation? How should I deal with 429457408 in the example above? Are 
there other twain-type related issues that I haven't seen yet, that I should 
be aware of? Is there some web-resource out there that answers my questions? 
Am I simply missing some information that I might already have? At least I 
haven't been able to find any enlightment in the documentation of the twain 
module or in the TWAIN specification. It seems to me that I need to be very 
careful with the type of the capabilities. How careful should I be?

Googling for things like TWTY_UINT16 typically results in what I interprete 
as C or C++ code, which does not help me much. I'm less fluent in C or C++ 
than I am in Italian (I can order a beer and buy postcards or stamps, that's 
more or less it).

I-cannot-order-a-beer-in-C-but-I-can-in-Python-ly yours
/Mikael Olofsson
Universitetslektor (Senior Lecturer [BrE], Associate Professor [AmE])
Linköpings universitet

-----------------------------------------------------------------------
E-Mail:  mikael at isy.liu.se
WWW:     http://www.dtr.isy.liu.se/en/staff/mikael
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
-----------------------------------------------------------------------
Linköpings kammarkör: www.kammarkoren.com   Vi söker tenorer och basar! 




More information about the Python-list mailing list