Getting the WIA Automation Library v2.0 to deal with document feeder scanners

Building on the code from my previous post. The WIA Automation library wizard only gets one image at a time if you use the Item object returned from the ShowSelectItems method

Items items = WIACommonDialog.ShowSelectItems(d);

You can call the ShowTransfer method as many times as you want on an item in the collection and it will keep scanning until the cows come home if it is a flatbed scanner or until the document feeder runs out of paper and an exception is thrown

ImageFile imageFile = (ImageFile)WIACommonDialog.ShowTransfer(item);

so firstly we need to determine what kind of scanner it is and then determine the status of the scanner so we know when to stop transferring. Detailed info about the state of the device is available in the Properties collection of the selected device. The relevant constants we need are in wiadef.h in the Platform SDK. The abridged C# version:

class WIA_DPS_DOCUMENT_HANDLING_SELECT 
{
    
public const uint FEEDER = 0x00000001;
    
public const uint FLATBED = 0x00000002;
}

class WIA_DPS_DOCUMENT_HANDLING_STATUS 
{
    
public const uint FEED_READY = 0x00000001;
}

class WIA_PROPERTIES
{
    
public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
    
public const uint WIA_DIP_FIRST = 2;
    
public const uint WIA_DPA_FIRST  =  WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    
public const uint WIA_DPC_FIRST  = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    
//
    // Scanner only device properties (DPS)
    //
    
public const uint WIA_DPS_FIRST    =                      WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    
public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS  =     WIA_DPS_FIRST + 13;
    
public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT  =     WIA_DPS_FIRST + 14;    
}

class WIA_ERRORS
{
    
public const uint BASE_VAL_WIA_ERROR = 0x80210000;
    
public const uint WIA_ERROR_PAPER_EMPTY  = BASE_VAL_WIA_ERROR + 3;    
}

The DocumentHandlingSelect property tells us what type of scanner it is and the DocumentHandlingStatus will tell us the status of the document feeder.

foreach (Item item in items)
{
    
bool hasMorePages = true;    
    
while (hasMorePages)
    {                        
        ImageFile imageFile = (ImageFile)WIACommonDialog.ShowTransfer(item);
        Vector vector = imageFile.FileData;
        
byte[] bin = (byte[])vector.get_BinaryData();
        
//Do something here with the image data
                
        
Property documentHandlingSelect = null;
        Property documentHandlingStatus = 
null;
        
//get the two properties
        
foreach (Property prop in device.Properties)
        {
            
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                documentHandlingSelect = prop;
            
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                documentHandlingStatus = prop;
        }                        

        hasMorePages = 
false//assume there are no more pages
        
if (documentHandlingSelect != null)
        
//may not exist on flatbed scanner but required for feeder
        
{
            
//check for document feeder
            
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0) 
            {
                hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
            }
        }
    }
}

I don't have a better way of handling the errors other than the text message

catch (Exception ex)
{
    
string message = ex.Message;
    
if (message.EndsWith("0x80210001."))
        message = " General error.";
    
else 
        if 
(message.EndsWith("0x80210002."))
        message = " Paper Jam.";
    
else 
        if 
(message.EndsWith("0x80210003."))
        message = " Paper Empty.";
    
//etc

powered by IMHO 1.2

Published Tuesday, March 15, 2005 6:31 PM by colin
Filed under:

Comments

# re: Getting the WIA Automation Library v2.0 to deal with document feeder scanners

The code seems very usefull for a project i am commencing. Just wanted to thank you ahead of time!
David,

Wednesday, November 02, 2005 7:52 AM by David Vanunu