5

I have a HP Scanjet 7000 (duplex & ADF scanner) and a HP Scanjet 5500c (only ADF) and a scanner program I'm developing which uses WIA 2.0 on Windows 7.

The problem is that the code works perfectly on the older scanner model, but on the newer one the code seems to run just fine through the first page, then fail on the second. If I step through the code around the following line;

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

the old scanner stops and waits for another call to be made on the same reference, but the newer one just runs through all it's pages from the feeder in one continuous operation.

I notice if I'm using the default scanning program in Windows 7, the newer one returns a single .tif file which contains all the separate pages. The older one returns separate .jpg files (one for each page).

This indicates to me that the newer scanner is scanning through its whole feeder before it is ready to return a collection of images where the older one returns ONE image between each page scanned.

How can I support this behavior in code? The following is part of the relevant code which works on the older scanner model:

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;
    }

Any help on this issue would be very much appreciated! I can't seem to find a working solution on the web. Just unanswered forum posts from people with the same problem.

4

3 回答 3

1

我们有一个非常相似的问题和各种解决方案,例如通过设置某些属性,并没有帮助。主要问题是扫描仪 (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;
    }
于 2018-06-05T13:15:49.497 回答
0

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

我有一篇关于在 Silverlight 中从 ADF 进行扫描的博客文章,我相信评论者遇到了与您遇到的相同问题。将 WIA_DPS_PAGES 设置为 1 为他修复了它。我最终修改了代码的 SetDeviceProperties 方法,将 WIA_DPS_PAGES 设置为 1,将 WIA_DPS_SCAN_AHEAD_PAGES 设置为 0。

于 2012-07-06T21:48:18.683 回答
0

经过大量试验和错误后,我偶然发现了一个解决方案,该解决方案由于我不太确定的原因而起作用。ShowTransfer() 方法似乎无法在扫描时将页面转换为 .png 或 .tiff。将格式设置为 JPEG 或 BMP 实际上为我解决了这个问题:

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

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

在旁注中,我将属性设置:3088 设置为 0x005(adf 和双工模式)。

于 2012-09-27T09:13:30.653 回答