带自动文档进纸器(ADF)的C#WIA仅在某些扫描仪上重新调整一页

时间:2012-07-04 14:58:12

标签: image scanning wia

我有一台HP Scanjet 7000(双面和ADF扫描仪)和一台HP Scanjet 5500c(仅限ADF)以及我正在开发的扫描仪程序,该程序在Windows 7上使用WIA 2.0。

问题是代码在较旧的扫描仪模型上完美运行,但在较新的代码上,代码似乎在第一页运行得很好,然后在第二页上运行失败。如果我绕过以下行的代码;

image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false);

旧扫描仪停止并等待在同一参考上进行另一次调用,但较新的扫描仪只是在一次连续操作中从进纸器运行所有页面。

我注意到如果我在Windows 7中使用默认扫描程序,则较新的一个会返回一个包含所有单独页面的.tif文件。较旧的文件返回单独的.jpg文件(每页一个)。

这向我表明,较新的扫描仪正在扫描其整个进纸器,然后准备返回一组图像,其中较旧的扫描仪在每个扫描的页面之间返回一个图像。

如何在代码中支持此行为?以下是适用于旧扫描仪型号的相关代码的一部分:

public static List<Image> Scan(string scannerId)
    {
        List<Image> images = new List<Image>();
        List<String> tmp_imageList = new List<String>();

        bool hasMorePages = true;
        bool useAdf = true;
        bool duplex = false;

        int pages = 0;

        string fileName = null;
        string fileName_duplex = null;

        WIA.DeviceManager manager = null;
        WIA.Device device = null;
        WIA.DeviceInfo device_infoHolder = null;
        WIA.Item item = null;
        WIA.ICommonDialog wiaCommonDialog = null;

        manager = new WIA.DeviceManager();

        // select the correct scanner using the provided scannerId parameter
        foreach (WIA.DeviceInfo info in manager.DeviceInfos)
        {
            if (info.DeviceID == scannerId)
            {
                // Find scanner to connect to
                device_infoHolder = info;        
                break;
            }
        }

        while (hasMorePages)
        {
            wiaCommonDialog = new WIA.CommonDialog();              

            // Connect to scanner
            device = device_infoHolder.Connect();

            if (device.Items[1] != null)
            {
                item = device.Items[1] as WIA.Item;

                try
                {
                    if ((useAdf) || (duplex))
                        SetupADF(device, duplex); //Sets the right properties in WIA

                    WIA.ImageFile image = null;
                    WIA.ImageFile image_duplex = null;

                    // scan image                
                    image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false);

                    if (duplex)
                    {
                        image_duplex = (ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatPNG, false);
                    }

                    // save (front) image to temp file
                    fileName = Path.GetTempFileName();
                    tmp_imageList.Add(fileName);
                    File.Delete(fileName);
                    image.SaveFile(fileName);
                    image = null;               

                    // add file to images list
                    images.Add(Image.FromFile(fileName));

                    if (duplex)
                    {
                        fileName_duplex = Path.GetTempFileName();
                        tmp_imageList.Add(fileName_duplex);
                        File.Delete(fileName_duplex);
                        image_duplex.SaveFile(fileName_duplex);
                        image_duplex = null;

                        // add file_duplex to images list
                        images.Add(Image.FromFile(fileName_duplex));
                    }

                    if (useAdf || duplex)
                    {
                        hasMorePages = HasMorePages(device); //Returns true if the feeder has more pages
                        pages++;                         
                    }
                }
                catch (Exception exc)
                {
                    throw exc;
                }
                finally
                {
                    wiaCommonDialog = null;
                    manager = null;
                    item = null;
                    device = null;
                }
            }
        }
        device = null;
        return images;
    }

非常感谢您对此问题的任何帮助!我似乎无法在网上找到一个可行的解决方案。来自具有相同问题的人的未答复的论坛帖子。

3 个答案:

答案 0 :(得分:0)

我看到你正在调用一个名为SetupADF的方法,该方法未显示,可能会在设备对象上设置一些属性。您是否尝试过设置WIA_DPS_PAGES (property 3096)和/或WIA_DPS_SCAN_AHEAD_PAGES (property 3094)

我有一个关于在Silverlight中从ADF扫描的blog post,我相信一位评论者会遇到你遇到的同样问题。将WIA_DPS_PAGES设置为1可以为他修复它。我最后修改了我的代码的SetDeviceProperties方法,将WIA_DPS_PAGES设置为1,将WIA_DPS_SCAN_AHEAD_PAGES设置为0。

答案 1 :(得分:0)

经过大量的反复试验后,我偶然发现了一个解决方案,原因是我不太确定。似乎ShowTransfer()方法无法将页面转换为.png或.tiff WHILE扫描。将格式设置为JPEG或BMP实际上解决了我的问题:

image = (ImageFile)scanDialog.ShowTransfer(item, wiaFormatJPEG, false);

我想我在网上看到,无论指定的格式如何,此方法实际上都会返回BMP。可能是将图像转换为png或tiff太重而不是使用bmp或jpeg。

在旁注中,我正在设置属性设置:3088到0x005(adf AND duplex mode)。

答案 2 :(得分:0)

我们有一个非常相似的问题和各种解决方案,例如通过设置某些属性,没有帮助。主要问题是扫描仪(ADF)在启动时收回所有页面,无论程序代码中发生了什么。 该过程反复导致错误,因为在扫描下一页之前“过多”。这特别适用于尝试另一个“连接”的事实。 出于这个原因,我们修改了代码,以便尽可能快地读取各个页面:

public List<Image> Scan(string deviceID)
    {
        List<Image> images = new List<Image>();

        WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
        WIA.Device device = this.Connect(deviceID);
        if (device == null)
            return images;

        WIA.Item item = device.Items[1] as WIA.Item;

        List<WIA.ImageFile> wiaImages = new List<ImageFile>();
        try
        {
            // scan images
            do
            {
                WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);
                wiaImages.Add(image);
            } while (true);
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            if ((uint)ex.ErrorCode != WIA_PROPERTIES.WIA_ERROR_PAPER_EMPTY)
                throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }

        foreach (WIA.ImageFile image in wiaImages)
            this.DoImage(images, image);

        return images;
    }