开发者

Detect all available scanner resolutions using WIA

开发者 https://www.devze.com 2023-03-10 03:59 出处:网络
How can I programmatically detect all available resolutions (in dpi) for a specified scanner using WIA 2.0?开发者_如何学CWhat about page sizes supported?Any ideas?Pseudo code:

How can I programmatically detect all available resolutions (in dpi) for a specified scanner using WIA 2.0? 开发者_如何学CWhat about page sizes supported? Any ideas?


Pseudo code: Assume you have device info, connect to it:

var device = deviceInfo.Connect(); if device is not null….. then you can get the Item Note that items begin at index 1 for device items

Item item = device.Items[1]; An Item has various properties which you can enumerate e.g.

foreach (Property prop in item.Properties) { var temp = prop.Name + " " + prop.PropertyID + " " + prop.get_Value(); }

In this case to find Maximum DPI supported by your scanner you could use. "Horizontal Optical Resolution" (property id: 3090) "Vertical Optical Resolution" property id: 3091

.. However if you examine the properties enumeration you will see there is nothing to tell you the minimum or list of all available DPI settings. .. Now I could not find anything either…. However I did find a way of discovering the minimum DPI… You may be aware of WIA intent, the enumeration allows you to set scanning type e.g. colour, grey scale etc. (property id: 6146)

var currentIntent = item.Properties.get_Item("6146"); // set to colour currentIntent.set_Value(WiaImageIntent.ColorIntent);

.. However you can also set WIA image bias in this way (to either maximum quality or minimum size) This is not via image intent enumeration but is done by OR ing the appropriate values

// set minimum size as WIA bias var intent = (int)item.Properties.get_Item("6146").get_Value(); item.Properties.get_Item("6146").set_Value(intent | 0x00010000);

http://msdn.microsoft.com/en-us/library/ms630190%28v=vs.85%29.aspx

And if you now look at the DPI (assuming previously DOI was not actually at minimum)

var dpiHorizontal = item.Properties.get_Item("6147").get_Value(); var dpiVertical = item.Properties.get_Item("6148").get_Value(); .. you should see that DPI is now set to it’s lowest (to give minimum size scan)

A big warning.

As soon as you set bias (be it minimum size or maximum quality) any previous DPI values you set are changed (as are various other properties for image dimensions etc – it makes wide ranging changes).

.. So, if you want to stay in control I suggest just finding minimum dpi once and storing it in configuration – Or if you do it each time, discard that item (after getting minimum DPI) and use a new item with no bias set.

.. So you now have max and min DPI values – how to get a list?

.. Again I know of no way .. but You could have a list of “sensible DPI values” e.g. 75, 100, 150, 200, 300, 600, 1200, 2400 As (depending on scanner resolution) these are “popular” values to support .. Depending on your max / min DPI values you know what “popular” values may be worth trying.

.. and when setting DPI do it via try / catch

e.g.

try { item.Properties.get_Item("6147").set_Value(myDPI); // horizontal DPI item.Properties.get_Item("6148").set_Value(myDPI); // vertical DPI } catch { // not supported so have some logic where try a different “popular” value }

I see this type of issue with one scanner I use – just because a DPI is within the max / min limits does not mean it is supported. Although many scanners WIA drivers support 100, 100 DPI, this one does not but does provide 75,75 DPI or 150,150 DPI This approach is nasty and kludgy but works (and again you could just do this once, run through your list of popular “DPI” values, try and set them, and be left with a list of which DPI settings your scanner supports.

For the sake of simplicity sake these pseudocode examples assume vertical & horizontal DPI may both be set to same value .. this may not be the case with all scanners!


You can use the SubTypeMin and SubTypeMax properties of WIA_IPS_XRES and WIA_IPS_YRES to get the allowed resolution ranges. You can probably use something similar to determine which page sizes are supported.

WIA.Property horizontalResolutionProperty = FindProperty(_scannerDevice.Items[1].Properties, HORIZONTAL_RESOLUTION);
if (horizontalResolutionProperty != null)
{
    // SubTypeMin and SubTypeMax are subproperties that tell what the min and max values are for the given property
    if (horizontalResolutionProperty.SubTypeMin > minResolution) minResolution = horizontalResolutionProperty.SubTypeMin;
    if (horizontalResolutionProperty.SubTypeMax < maxResolution) maxResolution = horizontalResolutionProperty.SubTypeMax;
}
WIA.Property verticalResolutionProperty = FindProperty(_scannerDevice.Items[1].Properties, VERTICAL_RESOLUTION);
if (verticalResolutionProperty != null)
{
    // SubTypeMin and SubTypeMax are subproperties that tell what the min and max values are for the given property
    if (verticalResolutionProperty.SubTypeMin > minResolution) minResolution = verticalResolutionProperty.SubTypeMin;
    if (verticalResolutionProperty.SubTypeMax < maxResolution) maxResolution = verticalResolutionProperty.SubTypeMax;
}


The horizontal and vertical resolution properties have a Subtype property which, depending on it's value, let you acces the allowed value or values for the resolution:

var xResolutionProperty = item.Properties["Horizontal Resolution"];

switch (xResolutionProperty.SubType)
{
    case WIASubType.ListSubType:
        // Here you can access property SubTypeValues, which contains a list of allowed values for the resolution.
        break;
    case WIASubType.RangeSubTypes:
        // Here you can access SubTypeMin and SubTypeMax properties, with the minimum and maximum allowed values.
        break;
    case WIASubType.UnspecifiedSubType:
        // Here you can access SubTypeDefault property, which contains the default resolution value.
        break;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号