C# WIA with Automatic Document Feeder (ADF) retuns only one page on certain scanners

六眼飞鱼酱① 提交于 2019-12-18 05:03:45

问题


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.


回答1:


I see you're calling a method called SetupADF, which is not shown, that presumably sets some properties on the device object. Have you tried setting WIA_DPS_PAGES (property 3096) and/or WIA_DPS_SCAN_AHEAD_PAGES (property 3094)?

I have a blog post about scanning from an ADF in Silverlight, and I believe a commenter came up against the same issue you're having. Setting WIA_DPS_PAGES to 1 fixed it for him. I ended up modifying my code's SetDeviceProperties method to set WIA_DPS_PAGES to 1 and WIA_DPS_SCAN_AHEAD_PAGES to 0.




回答2:


After alot of trial and error I stumbled upon a solution which worked for reasons I'm not quite sure of. It seems like the ShowTransfer() method was unable to convert the page to .png or .tiff WHILE scanning. Setting the format to JPEG or BMP actually solved the issue for me:

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

I think I saw somewhere on the web that this method actually returns BMP regardless of the format specified. Might be that converting the image to png or tiff is too heavy as opposed to using bmp or jpeg.

On a sidenote, I'm setting the property setting: 3088 to 0x005 (adf AND duplex mode).




回答3:


we had a very similar problem and various solutions, e.g. by setting certain properties, did not help. The main problem was that the scanner (ADF) retracted all pages on startup, regardless of what was happening in the program code. The process repeatedly led to errors, since "too much" was made before the next page was scanned. This applies in particular to the fact that another "Connect" was attempted. For this reason, we have modified the code so that the individual pages can be read in as quickly as possible:

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


来源:https://stackoverflow.com/questions/11331498/c-sharp-wia-with-automatic-document-feeder-adf-retuns-only-one-page-on-certain

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!