C# WIA ADF 有下一页

发布于 2024-12-23 13:30:58 字数 9156 浏览 1 评论 0原文

我创建了一个 WIA Wrapper,我相信它对某些人有用。 它仍处于开发阶段,但除了 HasNextPage 方法不起作用之外,它一切正常。我从很多不同的来源获取了代码。但基本上,在我的代码中,如果 ADF 中没有剩余页面,WIA.Properties 不会更改,因此它总是认为还有另一个页面?我可以做一个草率的(如果没有纸张错误的话尝试捕获,但那是非常草率的)

有什么想法吗?

这是我的代码:

ps - 我不介意对编码标准的批评,请评论任何能让我成为更好的程序员的事情:)

(我有几行代码可以尝试和调试它)主要问题是,无论我做什么,文档处理状态都不会改变

public struct PageSize
{
    public double Height;
    public double Width;

    public PageSize(double height, double width)
    {
        this.Height = height;
        this.Width = width;
    }
}

class WIA_PROPERTIES
{
    public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const uint WIA_DIP_FIRST = 2;
    public const uint WIA_DPA_FIRST  =  WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPC_FIRST  = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    //
    // Scanner only device properties (DPS)
    //
    public const uint WIA_DPS_FIRST    =                      WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS  =     WIA_DPS_FIRST + 13;
    public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT  =     WIA_DPS_FIRST + 14;
}



public class WiaWrapper
{

    //Image Filenames
    const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";

    //Standard Page Sizes
    public PageSize A3 = new PageSize(16.5, 11.7);
    public PageSize A4 = new PageSize(11.7, 8.3);
    public PageSize A5 = new PageSize(8.3, 5.8);
    public PageSize A6 = new PageSize(5.8, 4.1);

    public string DeviceID;

    #region Setup/select Scanner

    /// <summary>
    /// Select Scanner.
    /// If you need to save the Scanner, Save WiaWrapper.DeviceID
    /// </summary>
    public void SelectScanner()
    {
        WIA.CommonDialog wiaDiag = new WIA.CommonDialog();

        try
        {
            Device d = wiaDiag.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
            if (d != null)
            {
                DeviceID = d.DeviceID;
                return;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error, Is a scanner chosen?");
        }

        throw new Exception("No Device Selected");
    }

    /// <summary>
    /// Connect to Scanning Device
    /// </summary>
    /// <param name="deviceID"></param>
    /// <returns></returns>
    private Device Connect()
    {
        Device WiaDev = null;

        DeviceManager manager = new DeviceManager();

        //Iterate through each Device until correct Device found
        foreach (DeviceInfo info in manager.DeviceInfos)
        {
            if (info.DeviceID == DeviceID)
            {
                WIA.Properties infoprop = info.Properties;

                WiaDev = info.Connect();
                return WiaDev;
            }
        }

        throw new Exception("Scanner not found - Is it setup in DeviceID?");
    }

    #endregion

    #region Scanning utilities - hasMorePages, SetupPageSize, SetupADF, DeleteFile

    /// <summary>
    /// Check to see if ADF has more pages loaded
    /// </summary>
    /// <param name="wia"></param>
    /// <returns></returns>
    private bool HasMorePages(Device wia)
    {

        //determine if there are any more pages waiting
        Property documentHandlingSelect = null;
        Property documentHandlingStatus = null;

        string test = string.Empty;

        foreach (Property prop in wia.Properties)
        {
            string propername = prop.Name;
            string propvalue = prop.get_Value().ToString();

            test += propername + " " + propvalue + "<br>";

            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                documentHandlingSelect = prop;
            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                documentHandlingStatus = prop;
        }

        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & 0x00000001) != 0)
        {
            return ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & 0x00000001) != 0);
        }

        string tester = test;

        return false;

    }

    /// <summary>
    /// Setup Page Size
    /// </summary>
    /// <param name="wia"></param>
    private void SetupPageSize(Device wia, bool rotatePage, PageSize pageSize, int DPI, WIA.Item item)
    {

        //Setup Page Size Property
        foreach (WIA.Property itemProperty in item.Properties)
        {

            if (itemProperty.Name.Equals("Horizontal Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Vertical Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Horizontal Extent"))
            {

                double extent = DPI * pageSize.Height;

                if (rotatePage)
                {
                    extent = DPI * pageSize.Width;
                }


                ((IProperty)itemProperty).set_Value(extent);


            }
            else if (itemProperty.Name.Equals("Vertical Extent"))
            {
                double extent = DPI * pageSize.Width;

                if (rotatePage)
                {
                    extent = pageSize.Height * DPI;
                }


                ((IProperty)itemProperty).set_Value(extent);
            }

        }

    }

    /// <summary>
    /// Setup device to Use ADF if required
    /// </summary>
    private void SetupADF(Device wia, bool duplex)
    {
        string adf = string.Empty;

        foreach (WIA.Property deviceProperty in wia.Properties)
        {
            adf += deviceProperty.Name + "<br>";
            if (deviceProperty.Name == "Document Handling Select") //or PropertyID == 3088
            {
                int value = duplex ? 0x004 : 0x001;
                deviceProperty.set_Value(value);
            }

        }

    }

    private void Delete_File(string filename)
    {
        //Overwrite File
        if (File.Exists(filename))
        {
            //file exists, delete it
            File.Delete(filename);
        }

    }

    #endregion

    #region Scan Page - Main Public Method

    /// <summary>
    /// Scan Page,
    /// </summary>
    /// <param name="wia">Connected Device</param>
    /// <param name="pageSize">Page Size. A4, A3, A2 Etc</param>
    /// <param name="RotatePage">Rotation of page while scanning</param>
    public void Scan(PageSize pageSize, bool rotatePage, int DPI, string filepath, bool useAdf, bool duplex)
    {
        int pages = 0;
        bool hasMorePages = false;

        WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();

        try
        {
            do
            {
                //Connect to Device
                Device wia = Connect();
                WIA.Item item = wia.Items[1] as WIA.Item;

                //Setup ADF
                if ((useAdf) || (duplex))
                    SetupADF(wia, duplex);

                //Setup Page Size
                SetupPageSize(wia, rotatePage, pageSize, DPI,item);

                WIA.ImageFile imgFile = null;
                WIA.ImageFile imgFile_duplex = null; //if duplex is setup, this will be back page


                imgFile = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);

                //If duplex page, get back page now.
                if (duplex)
                {
                    imgFile_duplex = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);
                }

                string varImageFileName = filepath + "\\Scanned-" + pages.ToString() + ".jpg";
                Delete_File(varImageFileName); //if file already exists. delete it.
                imgFile.SaveFile(varImageFileName);

                string varImageFileName_duplex; 

                if (duplex)
                {
                    varImageFileName_duplex = filepath + "\\Scanned-" + pages++.ToString() + ".jpg";
                    Delete_File(varImageFileName_duplex); //if file already exists. delete it.
                    imgFile_duplex.SaveFile(varImageFileName);
                }

                //Check with scanner to see if there are more pages.
                if (useAdf || duplex)
                {
                    hasMorePages = HasMorePages(wia);
                    pages++;
                }

            }
            while (hasMorePages);
        }
        catch (COMException ex)
        {
            throw new Exception(CheckError((uint)ex.ErrorCode));
        }
    }

    #endregion

I have created a WIA Wrapper, which I beleive will be usefull for some people.
It's still in development stages, but it all works besides the fact that the HasNextPage method does not work. I have taken the code from a lot of different sources. But basically, in my code, the WIA.Properties does not change if there is no pages left in the ADF so it always thinks there is another page? I could do a sloppy (try catch if no paper error, but thats very sloppy)

Any ideas?

heres my code:

ps - i dont mind critisism on coding standards, please comment on anything that'll make me a better programmer :)

(There are a few lines of code I have that are there to try and debug it) The main problem is, no matter what I do the Document Handling Status does not change

public struct PageSize
{
    public double Height;
    public double Width;

    public PageSize(double height, double width)
    {
        this.Height = height;
        this.Width = width;
    }
}

class WIA_PROPERTIES
{
    public const uint WIA_RESERVED_FOR_NEW_PROPS = 1024;
    public const uint WIA_DIP_FIRST = 2;
    public const uint WIA_DPA_FIRST  =  WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPC_FIRST  = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    //
    // Scanner only device properties (DPS)
    //
    public const uint WIA_DPS_FIRST    =                      WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
    public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS  =     WIA_DPS_FIRST + 13;
    public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT  =     WIA_DPS_FIRST + 14;
}



public class WiaWrapper
{

    //Image Filenames
    const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
    const string wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}";

    //Standard Page Sizes
    public PageSize A3 = new PageSize(16.5, 11.7);
    public PageSize A4 = new PageSize(11.7, 8.3);
    public PageSize A5 = new PageSize(8.3, 5.8);
    public PageSize A6 = new PageSize(5.8, 4.1);

    public string DeviceID;

    #region Setup/select Scanner

    /// <summary>
    /// Select Scanner.
    /// If you need to save the Scanner, Save WiaWrapper.DeviceID
    /// </summary>
    public void SelectScanner()
    {
        WIA.CommonDialog wiaDiag = new WIA.CommonDialog();

        try
        {
            Device d = wiaDiag.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
            if (d != null)
            {
                DeviceID = d.DeviceID;
                return;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error, Is a scanner chosen?");
        }

        throw new Exception("No Device Selected");
    }

    /// <summary>
    /// Connect to Scanning Device
    /// </summary>
    /// <param name="deviceID"></param>
    /// <returns></returns>
    private Device Connect()
    {
        Device WiaDev = null;

        DeviceManager manager = new DeviceManager();

        //Iterate through each Device until correct Device found
        foreach (DeviceInfo info in manager.DeviceInfos)
        {
            if (info.DeviceID == DeviceID)
            {
                WIA.Properties infoprop = info.Properties;

                WiaDev = info.Connect();
                return WiaDev;
            }
        }

        throw new Exception("Scanner not found - Is it setup in DeviceID?");
    }

    #endregion

    #region Scanning utilities - hasMorePages, SetupPageSize, SetupADF, DeleteFile

    /// <summary>
    /// Check to see if ADF has more pages loaded
    /// </summary>
    /// <param name="wia"></param>
    /// <returns></returns>
    private bool HasMorePages(Device wia)
    {

        //determine if there are any more pages waiting
        Property documentHandlingSelect = null;
        Property documentHandlingStatus = null;

        string test = string.Empty;

        foreach (Property prop in wia.Properties)
        {
            string propername = prop.Name;
            string propvalue = prop.get_Value().ToString();

            test += propername + " " + propvalue + "<br>";

            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                documentHandlingSelect = prop;
            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                documentHandlingStatus = prop;
        }

        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & 0x00000001) != 0)
        {
            return ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & 0x00000001) != 0);
        }

        string tester = test;

        return false;

    }

    /// <summary>
    /// Setup Page Size
    /// </summary>
    /// <param name="wia"></param>
    private void SetupPageSize(Device wia, bool rotatePage, PageSize pageSize, int DPI, WIA.Item item)
    {

        //Setup Page Size Property
        foreach (WIA.Property itemProperty in item.Properties)
        {

            if (itemProperty.Name.Equals("Horizontal Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Vertical Resolution"))
            {
                ((IProperty)itemProperty).set_Value(DPI);
            }
            else if (itemProperty.Name.Equals("Horizontal Extent"))
            {

                double extent = DPI * pageSize.Height;

                if (rotatePage)
                {
                    extent = DPI * pageSize.Width;
                }


                ((IProperty)itemProperty).set_Value(extent);


            }
            else if (itemProperty.Name.Equals("Vertical Extent"))
            {
                double extent = DPI * pageSize.Width;

                if (rotatePage)
                {
                    extent = pageSize.Height * DPI;
                }


                ((IProperty)itemProperty).set_Value(extent);
            }

        }

    }

    /// <summary>
    /// Setup device to Use ADF if required
    /// </summary>
    private void SetupADF(Device wia, bool duplex)
    {
        string adf = string.Empty;

        foreach (WIA.Property deviceProperty in wia.Properties)
        {
            adf += deviceProperty.Name + "<br>";
            if (deviceProperty.Name == "Document Handling Select") //or PropertyID == 3088
            {
                int value = duplex ? 0x004 : 0x001;
                deviceProperty.set_Value(value);
            }

        }

    }

    private void Delete_File(string filename)
    {
        //Overwrite File
        if (File.Exists(filename))
        {
            //file exists, delete it
            File.Delete(filename);
        }

    }

    #endregion

    #region Scan Page - Main Public Method

    /// <summary>
    /// Scan Page,
    /// </summary>
    /// <param name="wia">Connected Device</param>
    /// <param name="pageSize">Page Size. A4, A3, A2 Etc</param>
    /// <param name="RotatePage">Rotation of page while scanning</param>
    public void Scan(PageSize pageSize, bool rotatePage, int DPI, string filepath, bool useAdf, bool duplex)
    {
        int pages = 0;
        bool hasMorePages = false;

        WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();

        try
        {
            do
            {
                //Connect to Device
                Device wia = Connect();
                WIA.Item item = wia.Items[1] as WIA.Item;

                //Setup ADF
                if ((useAdf) || (duplex))
                    SetupADF(wia, duplex);

                //Setup Page Size
                SetupPageSize(wia, rotatePage, pageSize, DPI,item);

                WIA.ImageFile imgFile = null;
                WIA.ImageFile imgFile_duplex = null; //if duplex is setup, this will be back page


                imgFile = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);

                //If duplex page, get back page now.
                if (duplex)
                {
                    imgFile_duplex = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, false);
                }

                string varImageFileName = filepath + "\\Scanned-" + pages.ToString() + ".jpg";
                Delete_File(varImageFileName); //if file already exists. delete it.
                imgFile.SaveFile(varImageFileName);

                string varImageFileName_duplex; 

                if (duplex)
                {
                    varImageFileName_duplex = filepath + "\\Scanned-" + pages++.ToString() + ".jpg";
                    Delete_File(varImageFileName_duplex); //if file already exists. delete it.
                    imgFile_duplex.SaveFile(varImageFileName);
                }

                //Check with scanner to see if there are more pages.
                if (useAdf || duplex)
                {
                    hasMorePages = HasMorePages(wia);
                    pages++;
                }

            }
            while (hasMorePages);
        }
        catch (COMException ex)
        {
            throw new Exception(CheckError((uint)ex.ErrorCode));
        }
    }

    #endregion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-12-30 13:30:58

我现在相信这是我的扫描仪硬件(或驱动程序)问题。我正在使用理光 Aficio IS330DC。

我所做的工作是放置一个 Try/catch 块,如果异常是 Out Of Paper 则完成。

有用。

I now beleive it is my scanner hardware (or driver) problem. I am uysing a Ricoh Aficio IS330DC.

The work around I did was put a Try/catch block and if exception is Out Of Paper then finish.

It works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文