使用 WIA、C# 和 Win 7 进行相机捕捉

Posted

技术标签:

【中文标题】使用 WIA、C# 和 Win 7 进行相机捕捉【英文标题】:Camera Capture with WIA, C# and Win 7 【发布时间】:2011-08-15 08:55:45 【问题描述】:

我尝试在 Windows 7 上使用 WIA 2.0、C#、.net 4.0 从网络摄像头拍照。我尝试了许多不同的示例,但没有任何效果。我总是得到这个异常:“ComException 未处理”来自 HRESULT 的异常:0x80210015”。代码是WIA_S_NO_DEVICE_AVAILABLE。我检查了 WIA-Service 是否正在运行以及摄像头是否出现在扫描仪和相机中。我没有知道这里出了什么问题!有人可以帮忙吗?

在这一行抛出异常:

设备 d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);:

这里是代码

string DeviceID;

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


    class WIA_DPS_DOCUMENT_HANDLING_SELECT
    
        public const uint FEEDER = 0x00000001;
        public const uint FLATBED = 0x00000002;
    

    class WIA_DPS_DOCUMENT_HANDLING_STATUS
    
        public const uint FEED_READY = 0x00000001;
    

    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;
    

    class WIA_ERRORS
    
        public const uint BASE_VAL_WIA_ERROR = 0x80210000;
        public const uint WIA_ERROR_PAPER_EMPTY = BASE_VAL_WIA_ERROR + 3;
    


    public void ADFScan()
    

        //Choose Scanner
        CommonDialogClass class1 = new CommonDialogClass();
        Device d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);
        if (d != null)
        
            this.DeviceID = d.DeviceID;
        
        else
        
            //no scanner chosen
            return;
        



        WIA.CommonDialog WiaCommonDialog = new CommonDialogClass();

        bool hasMorePages = true;
        int x = 0;
        int numPages = 0;
        while (hasMorePages)
        
            //Create DeviceManager
            DeviceManager manager = new DeviceManagerClass();
            Device WiaDev = null;
            foreach (DeviceInfo info in manager.DeviceInfos)
            
                if (info.DeviceID == this.DeviceID)
                
                    WIA.Properties infoprop = null;
                    infoprop = info.Properties;

                    //connect to scanner
                    WiaDev = info.Connect();


                    break;
                
            



            //Start Scan

            WIA.ImageFile img = null;
            WIA.Item Item = WiaDev.Items[1] as WIA.Item;

            try
            

                img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, wiaFormatJPEG, false);


                //process image:
                //one would do image processing here
                //

                //Save to file
                string varImageFileName = "c:\\test" + x.ToString() + ".jpg";
                if (File.Exists(varImageFileName))
                
                    //file exists, delete it
                    File.Delete(varImageFileName);
                
                img.SaveFile(varImageFileName);
                numPages++;
                img = null;

            
            catch (Exception ex)
            
                MessageBox.Show("Error: " + ex.Message);
            
            finally
            
                Item = null;
                //determine if there are any more pages waiting
                Property documentHandlingSelect = null;
                Property documentHandlingStatus = null;
                foreach (Property prop in WiaDev.Properties)
                
                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                        documentHandlingSelect = prop;
                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                        documentHandlingStatus = prop;


                

                hasMorePages = false; //assume there are no more pages
                if (documentHandlingSelect != null)
                //may not exist on flatbed scanner but required for feeder
                
                    //check for document feeder
                    if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                    
                        hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                    
                
                x++;
            
        

    

感谢您的帮助!

【问题讨论】:

显示在扫描仪和相机下的设备不一定是相机。您的代码仅请求摄像头。你得到的错误是你没有这种类型的设备。 【参考方案1】:

这是 Microsoft 未记录错误的典型示例...错误代码仅表示未找到任何设备,未连接或未识别 WIA。

【讨论】:

【参考方案2】:

如果有摄像头连接,尝试添加类似CameraTest的方法进行测试:

bool CameraTest()

bool present = false;

try

    Device d = class1.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true, false);
    present = true;

catch (Exception ex)


return present;

【讨论】:

【参考方案3】:

试试下面的代码:

class WIAScanner

    const string wiaFormatBMP = "B96B3CAB-0728-11D3-9D7B-0000F81EF32E";

    class WIA_DPS_DOCUMENT_HANDLING_SELECT
    
        public const uint FEEDER = 0x00000001;
        public const uint FLATBED = 0x00000002;
    

    class WIA_DPS_DOCUMENT_HANDLING_STATUS
    
        public const uint FEED_READY = 0x00000001;
    

    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;
    

    /// <summary>
    /// Use scanner to scan an image (with user selecting the scanner from a dialog).
    /// </summary>
    /// <returns>Scanned images.</returns>
    public static List<Image> Scan()
    
        WIA.ICommonDialog dialog = new WIA.CommonDialog();
        WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.UnspecifiedDeviceType, true, false);

        if (device != null)
        
            return Scan(device.DeviceID);
        
        else
        
            throw new Exception("You must select a device for scanning.");
        
    

    /// <summary>
    /// Use scanner to scan an image (scanner is selected by its unique id).
    /// </summary>
    /// <param name="scannerName"></param>
    /// <returns>Scanned images.</returns>
    public static List<Image> Scan(string scannerId)
    
        List<Image> images = new List<Image>();

        bool hasMorePages = true;
        while (hasMorePages)
        
            // select the correct scanner using the provided scannerId parameter
            WIA.DeviceManager manager = new WIA.DeviceManager();
            WIA.Device device = null;
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            
                if (info.DeviceID == scannerId)
                
                    // connect to scanner
                    device = info.Connect();
                    break;
                
            

            // device was not found
            if (device == null)
            
                // enumerate available devices
                string availableDevices = "";
                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                
                    availableDevices += info.DeviceID + "\n";
                

                // show error with available devices
                throw new Exception("The device with provided ID could not be found. Available Devices:\n" + availableDevices);
            

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

            try
            
                // scan image
                WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);

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

                // add file to output list
                images.Add(Image.FromFile(fileName));
            
            catch (Exception exc)
            
                throw exc;
            
            finally
            
                item = null;

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

                foreach (WIA.Property prop in device.Properties)
                
                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                        documentHandlingSelect = prop;

                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                        documentHandlingStatus = prop;
                

                // assume there are no more pages
                hasMorePages = false;

                // may not exist on flatbed scanner but required for feeder
                if (documentHandlingSelect != null)
                
                    // check for document feeder
                    if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                    
                        hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                    
                
            
        

        return images;
    

    /// <summary>
    /// Gets the list of available WIA devices.
    /// </summary>
    /// <returns></returns>
    public static List<string> GetDevices()
    
        List<string> devices = new List<string>();
        WIA.DeviceManager manager = new WIA.DeviceManager();

        foreach (WIA.DeviceInfo info in manager.DeviceInfos)
        
            devices.Add(info.DeviceID);
        

        return devices;
    


【讨论】:

为什么我投了反对票?...解释一下这个答案有什么问题? 什么是“试试这个”?这是一个答案吗?它有什么帮助?您的ShowSelectDevice 与所讨论的完全相同。这不是一个有效的答案,它没有解释问题,它没有提供解决方法。这只是一段冒险的代码。

以上是关于使用 WIA、C# 和 Win 7 进行相机捕捉的主要内容,如果未能解决你的问题,请参考以下文章

使用 C# 和 WIA 进行扫描

Android : 将图库功能与相机捕捉相结合

无法在 Windows 7 x64 上使用 WIA 进行扫描

需要信息 TWAIN 和 WIA 驱动程序来扫描 C# 中的图像?

确保相机预览大小/纵横比与生成的视频相匹配

C# WIA 设置分辨率不会增加分辨率,而是增加图像的宽度和高度。在 C# 中