带自动文档进纸器(ADF)的C#WIA在某些扫描仪上仅返回一页 [英] C# WIA with Automatic Document Feeder (ADF) retuns only one page on certain scanners

查看:1079
本文介绍了带自动文档进纸器(ADF)的C#WIA在某些扫描仪上仅返回一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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

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

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.

在旁注中,我正在设置属性设置:3088到0x005(adf AND双工模式)。

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

这篇关于带自动文档进纸器(ADF)的C#WIA在某些扫描仪上仅返回一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆