在C#中利用ActiveX控件进行视频采集

来源:互联网 发布:软件测试方向 编辑:程序博客网 时间:2024/05/15 05:30
        在VS6.0年代,微软提供了VFW(Video For Window) 这样强大而方便的库来支持视频捕获和回放。但是无论是在。NET v1.0还是在.NET v2.0框架中,都没有提供相应的类库来支持视频捕获和回放。解决这个问题有很多种方法,比如利用平台调用P/Invoke对VFW中的功能函数进行封装,然后再C#中进行调用。这样效率很低并且太复杂。还可以利用第三方提供的ActiveX控件来实现这个功能,这有什么好处呢?简单!!!!这也就足够了。并且往往第三方提供的控件功能更强大。但是世界上没有免费的午餐——这种控件往往是要收费的。在这里肯定有的朋友会想到:“不是还可以利用DX(DirectX)来实现吗,微软也提供了Manager DX托管代码的SDK”。的确,利用DX来实现视频捕获和回放是一个很好的方法,无论从效率还是效果来说都是上上之选。不幸的是:微软虽然提供了D3D、DirectDraw、DirectSound、DirectPlayer......但是唯独没有我们感兴趣的,可以实现视频捕获的DirectShow的SDK。据我所知,国外有开源的代码对DirectShow进行了封装,我也用过,个人感觉还不错,有兴趣的朋友可以查找一下这方面的资料,这里我就不多说了。
        言归正传,要利用ActiveX在C#中实现视频捕获,先要做好以下准备工作:
  • 首先是要有VS2003或者VS2005的开发环境,这个我就不多说,相信地球人都知道。
  • 其次是要安装好摄像头的驱动程序,也就是说你在QQ或者MSN视频聊天时能看到你自己摄像头的图像。
  • 然后是最重要的了:安装支持视频捕获的ActiveX控件!什么控件?Pegasus CapturePro。哪里下载?下载地址:http://www.cncode.com/downinfo/3504.html 这个地址应该是可以下载的,我刚刚又测试了一次。但是我不能保证它一直有效。这个控件是收费的,网上有它的评估版,相信大家也有办法,实在不行,那么给我发Email吧,我来告诉你怎么办。安装时一直"Next"下去后就可以了。
        在VS2003中新建一个“Windows 应用程序”工程,工程建立后,首先要向“工具箱”中添加ActiveX控件具体方法是在VS2003菜单的“工具”菜单中单击“添加/移除工具箱项”。在弹出的对话框中选择“COM 组件”选项卡,在下面的列表框中,将“Pegasus Imaging CapturePRO Control v3.0”前面的复选框选中,然后单击“确定”返回编译器编辑界面就可以了。然后将刚刚添加的控件拖放到窗体上调整好大小和位置,在属性页中修改属性。修改属性的方法和普通控件一样,至于各个属性的含义可以参考控件的帮助文档。控件所有的方法、属性和事件在帮助文档中都有详细的说明。这里建议修改其“Name”属性,方便以后操作,比如将Name属性改为axCap(以后axCap都表示该控件)。将axCapSize属性改为320,240。因为很多摄像头的默认分辨率是320X240。窗体布局大概如下图:
        然后再窗体上放一个Button控件,在该控件的单击事件中添加以下代码:
private void butConnect_Click(object sender, System.EventArgs e)
{
    axCap.Connect (
0);        // 连接到设备
    axCap.Preview = true;        // 开始预览
}
        然后编译工程,允许程序,单击按钮应该就可以看到视频图像了。
         到这里就表示我们的操作成功了,最基本的功能实现了,这里对上述两行代码进行简单的解释。第一行axCap.Connect(0)表示将控件连接到设备0。在Windows中,可以同时支持多个视频设备,每个设备都有一个编号。第一个设备编号0,第二个设备编号1,依次类推。由于我的电脑上只连接了一个视频摄像头,所以视频设备的编号是0。这里也就可以看出Connect()函数的参数实际上就是视频设备的参数,至于怎样来确定视频设备对于得编号,下面会有详细的说明。第二行axCap.Preview = true;表示打开预览。Preview属性为真时表示在控件上显示视频图像,当然要是该属性为false,我们就看不到视频了。
  
 
     
        下面对一些常用的,比较重要的方法和属性进行说明:
         怎样确定视频设备的编号:首先利用属性axCap .NumDevices来获取Windows中可用视频设备的数目,然后利用方法axCap .ObtainDeviceName (i)来获取该设备的名称字符串。其中参数i同Connect()函数一样,表示视频设备编号。具体的方法和实现过程可以参考以下代码: 
//判断有没用可用的捕获设备
if(axCap .NumDevices <0)

{
    
//没有找到可用的视频设备则报错,并退出程序
    MessageBox.Show ("没有发现可用的设备!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
    cobDevice.Items .Add(
"未发现可用视频设备");
    cobDevice.SelectedIndex 
=0;
}
else
{
    
//找到则枚举所以设备,并初始化设备组合框
    try
    {
        
for(int i=0;i<axCap .NumDevices ;i++)
        {
            cobDevice.Items .Add (axCap .ObtainDeviceName (i));
        }
        cobDevice.SelectedIndex 
=0;    //设置默认设备
    }
    
catch
    {
        MessageBox.Show (
"初始化设备失败!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
        cobDevice.Items .Add(
"未发现可用视频设备");
        cobDevice.SelectedIndex 
=0;
    }
}
        怎样修改摄像头参数:每个摄像头都有很多参数,如亮度、对比度、灰度等。有的摄像还可能提供很多特效效果,比如像框,黑白等。这些参数和效果都可通过调用底层的视频设备属性对话框来修改,具体方法请参考以下代码:
private void butSet_Click(object sender, System.EventArgs e)
{
    
//调用DrawShow设置对话框
    if (axCap .HasFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE .FILTERPROP_VIDEO_DEVICE ))
    {
        axCap .ShowFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE .FILTERPROP_VIDEO_DEVICE, 
"");
    }
}

        怎样录像:首先要设置录像文件,可以通过axCap.StreamFile属性来设置录像文件路径,该属性指的是录像文件的完整路径。然后可以设置Preview = true属性来打开预览。然后调用方法axCap.StartCapture ()来开始录像。停止录像很简单只需要调用函数axCap.EndCapture ()就可以了。可以参考以下代码:

private void butStartCap_Click(object sender, System.EventArgs e)
{
    
if(saveFile.FileName !=""//如果设置了路径则执行操作 否则报错
    {
        
if(radCapFarme.Checked )  //执行帧捕获操作
        {
            
if(!flagCapingFarmes)  //如果当前没有执行其它捕获
            {
                
if(!axCap.Preview )   //打开自动预览
                {
                    axCap.Visible 
=true;
                    axCap.Preview 
=true;
                    txtBack.Visible 
=false;
                    butCaptureImg.Enabled 
=true;
                    butPreview.Text 
="停止预览";
                }

                
if(chkAutoSave.Checked )
                
{
                    
//如果自动保存为真则设置周期
                    axCap.Interval =Convert.ToInt32 (txtTime.Text ,10); //先设置自动保存时间
                }

                axCap.AutoSave 
=chkAutoSave.Checked;   //处理自动保存
                axCap.AutoIncrement =chkAutoRename.Checked; //处理自动改文件名
                axCap.FrameFile =saveFile.FileName ;    //设置保存路径
                butStartCap.Text ="停止捕获";         
                flagCapingFarmes
=true;                   //设置工作进行标志 标志忙
                axCap.CaptureFrame ();                   //开始捕获帧
            }

            
else        //如果当前正在进行其它捕获工作
            
                axCap.AutoSave 
=false;                 //关自动保存
                axCap.Interval =0;                     //自动保存周期置0
                butStartCap.Text ="开始捕获";
                flagCapingFarmes
=false;                //置工作空闲标志
                DisConnect();                          //断开连接 关闭捕获 
            }

        }

        
else   //执行流捕获
        {
            
if(!flagCapingStream )
            
{
                axCap.VideoCompressorIndex 
=cobVicomp.SelectedIndex ;
                axCap.AudioDeviceIndex 
=comAudioDevice.SelectedIndex ;
                axCap.AudioCompressorIndex 
=comAudioComp.SelectedIndex ;
                axCap.StreamFile 
=saveFile.FileName ;
                butStartCap.Text 
="停止捕获";
                flagCapingStream
=true;
                
if(!axCap.Preview )   //打开自动预览
                {
                    axCap.Visible 
=true;
                    axCap.Preview 
=true;
                    txtBack.Visible 
=false;
                    butCaptureImg.Enabled 
=true;
                    butPreview.Text 
="停止预览";
                }

                
try
                
{
                    axCap.StartCapture ();
                }

                
catch
                
{
                    MessageBox.Show (
"视频编码器不可用,请重新连接设备");
                    cobVicomp.Items.Clear ();
                    butStartCap.Text 
="开始捕获";
                    flagCapingStream
=false;
                    axCap.Preview 
=false;
                    mnuDisableLink.Enabled 
=false;
                    mnuLink.Enabled 
=true;
                    cobColor.Enabled 
=false;
                    cobPix.Enabled 
=false;
                    cobColor.Text 
=null;
                    cobColor.Items.Clear ();
                    cobPix.Text 
=null;
                    cobPix.Items .Clear ();
                    txtBack.Visible 
=true;
                    chkAdvCap.Enabled 
=false;
                    butPreview.Enabled 
=false;
                    butSet.Enabled 
=false;
                    butDeviceSet.Enabled 
=false;
                    butVideoFormat.Enabled 
=false;
                    butCaptureImg.Enabled 
=false;
                    chkData.Enabled 
=false;
                    chkTime.Enabled 
=false;
                    txtLable.Enabled 
=false;
                    butPreview.Text 
="开始预览";
                    butLink.Text 
="连接设备";
                    
if(chkAdvCap.Checked )
                    chkAdvCap.CheckState 
=CheckState.Unchecked ;
                    visableControl(
false,3);
                }

            }

            
else
            
{
                
if(axCap.Preview )
                
{
                    axCap.Preview 
=false;
                    axCap.Visible 
=false;
                    txtBack.Visible 
=true;
                    butCaptureImg.Enabled 
=false;
                    butPreview.Text 
="开始预览";
                }

                butStartCap.Text 
="开始捕获";
                flagCapingStream
=false;
                axCap.EndCapture ();
            }

        }

    }

    
else
    
{
        MessageBox.Show (
"请先设置要存储的文件!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
    }

    
}

              Pegasus CapturePRO 控件是一个功能非常强大的控件,由于篇幅和时间关系这里就不一一叙述了,该控件所有的属性、方法、事件在它的帮助文档中都有详细的说明,大家在使用的时候可以仔细阅读。我写了一个比较详细的Demo程序,该程序使用了绝大多数功能,下面我将软件的界面和源代码给出,希望对大家有所帮助。

               该Demo程序的源代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace CaptureStudio
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class Capture : System.Windows.Forms.Form
    
{
        
private System.Windows.Forms.MainMenu mainMenu1;
        
private System.Windows.Forms.MenuItem menuItem1;
        
private System.Windows.Forms.MenuItem menuItem2;
        
private System.Windows.Forms.MenuItem mnuLink;
        
private System.Windows.Forms.MenuItem mnuDisableLink;
        
private System.Windows.Forms.ComboBox cobColor;
        
private System.Windows.Forms.ComboBox cobPix;
        
private System.Windows.Forms.ComboBox cobDevice;
        
private System.Windows.Forms.Button butPreview;
        
private Size m_size=new Size (176,144);
        
private Image capImage;
        
private bool flagCapingFarmes=false;
        
private bool flagCapingStream=false;
        
private System.Windows.Forms.GroupBox groupBox1;
        
private System.Windows.Forms.Button butSet;
        
private System.Windows.Forms.GroupBox groupBox2;
        
private System.Windows.Forms.Button butCaptureImg;
        
private System.Windows.Forms.Button butEditImg;
        
private System.Windows.Forms.Label label1;
        
private System.Windows.Forms.TextBox txtBack;
        
private System.Windows.Forms.Label label2;
        
private System.Windows.Forms.Label label3;
        
private System.Windows.Forms.PictureBox picDis;
        
private System.Windows.Forms.GroupBox groupBox3;
        
private System.Windows.Forms.MenuItem mnuClose;
        
private System.Windows.Forms.MenuItem mnuAdvanceSet;
        
private System.Windows.Forms.MenuItem menuItem3;
        
private System.Windows.Forms.MenuItem mnuMain;
        
private System.Windows.Forms.MenuItem mnuReg;
        
private System.Windows.Forms.MenuItem mnuAbout;
        
private System.Windows.Forms.Label label4;
        
private System.Windows.Forms.CheckBox chkData;
        
private System.Windows.Forms.CheckBox chkTime;
        
private System.Windows.Forms.TextBox txtLable;
        
private System.Windows.Forms.GroupBox groupBox4;
        
private System.Windows.Forms.Button butLink;
        
private System.Windows.Forms.Button butDeviceSet;
        
private System.Windows.Forms.Button butVideoFormat;
        
private System.Windows.Forms.CheckBox chkAdvCap;
        
private System.Windows.Forms.RadioButton radCapFarme;
        
private System.Windows.Forms.GroupBox groupBox5;
        
private System.Windows.Forms.RadioButton radCapStream;
        
private System.Windows.Forms.SaveFileDialog saveFile;
        
private System.Windows.Forms.Label label5;
        
private System.Windows.Forms.Button butSaveFile;
        
private System.Windows.Forms.TextBox txtPath;
        
private System.Windows.Forms.Button butStartCap;
        
private System.Windows.Forms.GroupBox groupBox6;
        
private System.Windows.Forms.CheckBox chkAutoSave;
        
private System.Windows.Forms.CheckBox chkAutoRename;
        
private System.Windows.Forms.CheckBox chkExtJpg;
        
private System.Windows.Forms.HScrollBar sclChrom;
        
private System.Windows.Forms.TextBox txtTime;
        
private System.Windows.Forms.Label label6;
        
private System.Windows.Forms.HScrollBar sclLum;
        
private System.Windows.Forms.Label labl7;
        
private System.Windows.Forms.Label label7;
        
private System.Windows.Forms.Label label8;
        
private System.Windows.Forms.TextBox txtChrom;
        
private System.Windows.Forms.GroupBox groupBox7;
        
private System.Windows.Forms.Label label9;
        
private System.Windows.Forms.ComboBox cobVicomp;
        
private System.Windows.Forms.Button butVideoComp;
        
private System.Windows.Forms.Label label11;
        
private System.Windows.Forms.Label label12;
        
private System.Windows.Forms.Button butAudioDevice;
        
private System.Windows.Forms.ComboBox comAudioDevice;
        
private System.Windows.Forms.ComboBox comAudioComp;
        
private System.Windows.Forms.TextBox txtLum;
        
private System.Windows.Forms.Button butSaveFrame;
        
private System.Windows.Forms.Timer timStreamStatus;
        
private System.Windows.Forms.GroupBox groupBox8;
        
private AxCAPTUREPRO3Lib.AxCapturePRO axCap;
        
private System.ComponentModel.IContainer components;
        
        
public Capture()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            InitializeComponent();
            visableControl(
false,3);
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
            
//判断有没用可用的捕获设备
            if(axCap .NumDevices <0)
            
{
                
//没有找到可用的视频设备则报错,并退出程序
                MessageBox.Show ("没有发现可用的设备!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                cobDevice.Items .Add(
"未发现可用视频设备");
                cobDevice.SelectedIndex 
=0;
            }

            
else
            
{
                
//找到则枚举所以设备,并初始化设备组合框
                try
                
{
                    
for(int i=0;i<axCap .NumDevices ;i++)
                    
{
                        cobDevice.Items .Add (axCap .ObtainDeviceName (i));
                    }

                    cobDevice.SelectedIndex 
=0;    //设置默认设备
                }

                
catch
                
{
                    MessageBox.Show (
"初始化设备失败!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                    cobDevice.Items .Add(
"未发现可用视频设备");
                    cobDevice.SelectedIndex 
=0;
                }

            }

        }

        
        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }

        
        
Windows 窗体设计器生成的代码
        
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Capture());
        }

        
        
private void mnuLink_Click(object sender, System.EventArgs e)
        
{
            
//连接设备
            ConnectDevice();
        }

        
        
private void mnuDisableLink_Click(object sender, System.EventArgs e)
        
{
            DisConnect();
        }

        
        
private void cobColor_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
//更改颜色
            if(axCap.IsConnected )
            
{
                axCap.VideoColorFormatIndex 
=cobColor.SelectedIndex ;
                axCap.Size 
=m_size;
            }

        }

        
        
private void cobPix_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
//更改分辨率
            if(axCap.IsConnected )
            
{
                axCap.VideoResolutionIndex
=cobPix.SelectedIndex ;   
                axCap.Size 
=m_size;   //限制视频预览窗口大小
            }

        }

        
        
private void butPreview_Click(object sender, System.EventArgs e)
        
{
            
if(!axCap.Preview )
            
{
                axCap.Visible 
=true;
                axCap.Preview 
=true;
                txtBack.Visible 
=false;
                butCaptureImg.Enabled 
=true;
                butPreview.Text 
="停止预览";
            }

            
else
            
{
                axCap.Preview 
=false;
                axCap.Visible 
=false;
                txtBack.Visible 
=true;
                butCaptureImg.Enabled 
=false;
                butPreview.Text 
="开始预览";
            }

        }

        
        
private void butSet_Click(object sender, System.EventArgs e)
        
{
            
//调用DrawShow设置对话框
            if (axCap .HasFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE .FILTERPROP_VIDEO_DEVICE ))
            
{
                axCap .ShowFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE .FILTERPROP_VIDEO_DEVICE, 
"");
            }

        }

        
        
private void butCaptureImg_Click(object sender, System.EventArgs e)
        
{
            
//捕获图片按钮
            if(!butEditImg.Enabled )       //如果编辑按钮无效则置有效
            butEditImg.Enabled =true;
            axCap.CaptureFrame ();        
//帧捕获模式
            capImage=axCap.Picture ;      //显示最后一次捕获的帧
            picDis.Image =capImage;
        }

        
        
private void mnuClose_Click(object sender, System.EventArgs e)
        
{
            
//文件-->退出
            Application.Exit();//退出系统
        }

        
        
private void chkData_CheckedChanged(object sender, System.EventArgs e)
        
{
            
if(txtLable.Text =="")
            
{
                
if(chkTime.CheckState==CheckState.Checked && chkData.CheckState==CheckState.Checked)
                
{
                    axCap.HAlign 
=CAPTUREPRO3Lib.HALIGNTYPE.HAlignCenter  ;
                    axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignBottom    ;
                    axCap.Caption 
="%d"+"  "+"%t";
                }

                
else
                
{
                    
if(chkTime.CheckState ==CheckState.Checked )
                    
{
                        axCap.HAlign 
=CAPTUREPRO3Lib.HALIGNTYPE.HAlignRight ;
                        axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignBottom  ;
                        axCap.Caption 
="%t";
                    }

                    
else if(chkData.CheckState ==CheckState.Checked )
                    
{
                        axCap.HAlign 
=CAPTUREPRO3Lib.HALIGNTYPE.HAlignLeft  ;
                        axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignTop ;
                        axCap.Caption 
="%d";
                    }

                    
else
                    axCap.Caption 
="";
                }

            }

            
else
            
{
                chkTime.CheckState 
=CheckState.Unchecked ;
                chkData.CheckState 
=CheckState.Unchecked ;
                axCap.HAlign 
=CAPTUREPRO3Lib.HALIGNTYPE.HAlignCenter ;
                axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignTop ;
                axCap.Caption 
=txtLable.Text ;
            }

        }

        
        
private void chkTime_CheckedChanged(object sender, System.EventArgs e)
        
{
            
//处理时间及日期标题
            if(txtLable.Text =="")   //如果文本框中没有输入则有效 否则日期,时间选择无效
            
                
//时间与日期都选中
                if(chkTime.CheckState==CheckState.Checked && chkData.CheckState==CheckState.Checked)
                
{
                    
//在底部居中显示日期和时间
                    axCap.HAlign =CAPTUREPRO3Lib.HALIGNTYPE.HAlignCenter  ;
                    axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignBottom    ;
                    axCap.Caption 
="%d"+"  "+"%t";
                }

                
else
                
{
                    
//时间选中
                    if(chkTime.CheckState ==CheckState.Checked )
                    
{
                        
//底部靠右显示时间
                        axCap.HAlign =CAPTUREPRO3Lib.HALIGNTYPE.HAlignRight ;
                        axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignBottom  ;
                        axCap.Caption 
="%t";
                    }

                    
//日期选中
                    else if(chkData.CheckState ==CheckState.Checked )
                    
{
                        
//顶部靠左显示日期
                        axCap.HAlign =CAPTUREPRO3Lib.HALIGNTYPE.HAlignLeft  ;
                        axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignTop ;
                        axCap.Caption 
="%d";
                    }

                    
else   //都未选中则不显示标题
                    axCap.Caption ="";
                }

            }

            
else   //文本框中有输入则显示输入的标题
            {
                chkTime.CheckState 
=CheckState.Unchecked ;       //置时间,日期选择无效
                chkData.CheckState =CheckState.Unchecked ; 
                
//顶部居中显示输入标题
                axCap.HAlign =CAPTUREPRO3Lib.HALIGNTYPE.HAlignCenter ;
                axCap.VAlign  
=CAPTUREPRO3Lib.VALIGNTYPE.VAlignTop ;
                axCap.Caption 
=txtLable.Text ;
            }

            
        }

        
        
private void txtLable_TextChanged(object sender, System.EventArgs e)
        
{
            
//提取文本框中输入的标题 最大字符数: 15
            
//在预览视频的顶部居中显示输入的标题
            chkTime.CheckState =CheckState.Unchecked ;    //置日期,时间选择无效
            chkData.CheckState =CheckState.Unchecked ;
            
if(txtLable.Text .Length >=15)                  //提示输入已达最大长度
            {
                MessageBox.Show (
"已达最大允许输入字符数!","提示!",MessageBoxButtons.OK ,MessageBoxIcon.Information );
            }

            axCap.HAlign 
=CAPTUREPRO3Lib.HALIGNTYPE.HAlignCenter ;  //居中显示
            axCap.VAlign  =CAPTUREPRO3Lib.VALIGNTYPE.VAlignTop ;   //顶部显示
            
            axCap.Caption 
=txtLable.Text ;
        }

        
        
private void Capture_Load(object sender, System.EventArgs e)
        
{
            
        }

        
        
private void butDeviceSet_Click(object sender, System.EventArgs e)
        
{
            
if(axCap.IsConnected )
            axCap.ShowVideoSourceDlg() ;
            
else
            MessageBox.Show (
"设备连接也断开,请重新连接设备!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
            
if(axCap.AudioDeviceIndex !=-1 && chkAdvCap.CheckState ==CheckState.Checked && 
            radCapStream.Checked)
            comAudioDevice.SelectedIndex 
=axCap.AudioDeviceIndex ;
        }

        
        
private void butVideoFormat_Click(object sender, System.EventArgs e)
        
{
            
if(axCap.IsConnected )
            axCap.ShowVideoFormatDlg ();
            
else
            MessageBox.Show (
"设备连接也断开,请重新连接设备!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
        }

        
        
private void butLink_Click(object sender, System.EventArgs e)
        
{
            
if(!axCap.IsConnected )
            
{
                
//连接设备
                ConnectDevice();
            }

            
else
            
{
                
//断开连接
                DisConnect();
            }

        }

        
        
private void butSaveFile_Click(object sender, System.EventArgs e)
        
{
            saveFile.RestoreDirectory 
=true;
            
if(radCapStream.Checked )
            
{
                saveFile.Filter 
="视频文件(*.avi)|*.avi|所有文件(*.*)|*.*";
            }

            
else
            
{
                saveFile.Filter 
="位图文件(*.bmp)|*.bmp|JPEG文件(*.jpg;*.jpeg)|*.jpg;*.jpeg|所有文件(*.*)|*.*";
            }

            
if(saveFile.ShowDialog ()==DialogResult.OK )
            
{
                txtPath.Text 
=saveFile.FileName ;
            }

        }

        
        
private void chkAdvCap_CheckedChanged(object sender, System.EventArgs e)
        
{
            
bool ConEnable=true;
            
if(chkAdvCap.CheckState ==CheckState.Checked )
            
{
                
//控制模块中各控件的Enable属性
                radCapFarme.Enabled =ConEnable;
                radCapStream.Enabled
=ConEnable;
                txtPath.Enabled
=ConEnable;
                butSaveFile.Enabled
=ConEnable;
                
if(axCap.IsConnected )
                butStartCap.Enabled
=ConEnable;
                
else
                butStartCap.Enabled 
=!ConEnable;
                radCapFarme.Enabled
=ConEnable;
                radCapStream.Enabled
=ConEnable;
                
//判断当前选中的捕获模块
                if(radCapFarme.Checked)
                visableControl(ConEnable,
1);
                
else
                visableControl(ConEnable,
2);
            }

            
else
            
{
                ConEnable
=false;
                visableControl(ConEnable,
3);        //禁用所有模块
            }

        }

        
        
/// <summary>
        
/// 显示/隐藏控件
        
/// </summary>

        public void visableControl(bool ConEnable,int ModolType)
        
{
            
//ModolType=1 选中帧捕获模式 
            if(ModolType==1)
            
{
                
//帧捕获模块中各控件的Enable属性
                chkAutoSave.Enabled=ConEnable;
                chkAutoRename.Enabled
=ConEnable;
                chkExtJpg.Enabled
=ConEnable;
                
if(chkAutoSave.Checked )
                
{
                    butSaveFrame.Enabled 
=!ConEnable;
                    txtTime.Enabled
=ConEnable;
                }

                
else
                
{
                    butSaveFrame.Enabled 
=ConEnable;
                    txtTime.Enabled
=!ConEnable;
                }

                
if(chkExtJpg.Checked )
                
{
                    txtChrom.Enabled
=ConEnable;
                    txtLum.Enabled
=ConEnable;
                    sclChrom.Enabled
=ConEnable;
                    sclLum.Enabled
=ConEnable;
                }

                
else
                
{
                    txtChrom.Enabled
=!ConEnable;
                    txtLum.Enabled
=!ConEnable;
                    sclChrom.Enabled
=!ConEnable;
                    sclLum.Enabled
=!ConEnable;
                }

                
//流捕获模块中各控件的Enable属性
                cobVicomp.Enabled=!ConEnable;
                comAudioComp.Enabled
=!ConEnable;
                comAudioDevice.Enabled
=!ConEnable;
                butVideoComp.Enabled
=!ConEnable;
                butAudioDevice.Enabled
=!ConEnable;
            }

            
//ModolType=2 选中流捕获模式 
            else if(ModolType==2)
            
{
                
//帧捕获模块中各控件的Enable属性
                chkAutoSave.Enabled=!ConEnable;
                chkAutoRename.Enabled
=!ConEnable;
                chkExtJpg.Enabled
=!ConEnable;
                txtTime.Enabled
=!ConEnable;
                txtChrom.Enabled
=!ConEnable;
                txtLum.Enabled
=!ConEnable;
                sclChrom.Enabled
=!ConEnable;
                sclLum.Enabled
=!ConEnable;
                butSaveFrame.Enabled 
=!ConEnable;
                
//流捕获模块中各控件的Enable属性
                cobVicomp.Enabled=ConEnable;
                comAudioComp.Enabled
=ConEnable;
                comAudioDevice.Enabled
=ConEnable;
                butVideoComp.Enabled
=ConEnable;
                butAudioDevice.Enabled
=ConEnable;
                GetSystemInfo();
            }

            
//ModolType=3 关闭高级捕获
            else
            
{
                
//控制模块中各控件的Enable属性
                radCapFarme.Enabled =ConEnable;
                radCapStream.Enabled
=ConEnable;
                txtPath.Enabled
=ConEnable;
                butSaveFile.Enabled
=ConEnable;
                butStartCap.Enabled
=ConEnable;
                radCapFarme.Enabled
=ConEnable;
                radCapStream.Enabled
=ConEnable;
                
//帧捕获模块中各控件的Enable属性
                chkAutoSave.Enabled=ConEnable;
                chkAutoRename.Enabled
=ConEnable;
                chkExtJpg.Enabled
=ConEnable;
                txtTime.Enabled
=ConEnable;
                txtChrom.Enabled
=ConEnable;
                txtLum.Enabled
=ConEnable;
                sclChrom.Enabled
=ConEnable;
                sclLum.Enabled
=ConEnable;
                butSaveFrame.Enabled 
=ConEnable;
                
//流捕获模块中各控件的Enable属性
                cobVicomp.Enabled=ConEnable;
                comAudioComp.Enabled
=ConEnable;
                comAudioDevice.Enabled
=ConEnable;
                butVideoComp.Enabled
=ConEnable;
                butAudioDevice.Enabled
=ConEnable;
            }

        }

        
        
private void radCapFarme_CheckedChanged(object sender, System.EventArgs e)
        
{
            
//选中帧捕获模块 禁用流捕获模块
            if(radCapFarme.Checked )
            visableControl(
true,1);
        }

        
        
private void radCapStream_CheckedChanged(object sender, System.EventArgs e)
        
{
            
//选中流捕获模块 禁用帧捕获模块
            if(radCapStream.Checked )
            
{
                visableControl(
true,2);
            }

        }

        
        
private void butStartCap_Click(object sender, System.EventArgs e)
        
{
            
if(saveFile.FileName !=""//如果设置了路径则执行操作 否则报错
            {
                
if(radCapFarme.Checked )  //执行帧捕获操作
                {
                    
if(!flagCapingFarmes)  //如果当前没有执行其它捕获
                    {
                        
if(!axCap.Preview )   //打开自动预览
                        {
                            axCap.Visible 
=true;
                            axCap.Preview 
=true;
                            txtBack.Visible 
=false;
                            butCaptureImg.Enabled 
=true;
                            butPreview.Text 
="停止预览";
                        }

                        
if(chkAutoSave.Checked )
                        
{
                            
//如果自动保存为真则设置周期
                            axCap.Interval =Convert.ToInt32 (txtTime.Text ,10); //先设置自动保存时间
                        }

                        axCap.AutoSave 
=chkAutoSave.Checked;   //处理自动保存
                        axCap.AutoIncrement =chkAutoRename.Checked; //处理自动改文件名
                        axCap.FrameFile =saveFile.FileName ;    //设置保存路径
                        butStartCap.Text ="停止捕获";         
                        flagCapingFarmes
=true;                   //设置工作进行标志 标志忙
                        axCap.CaptureFrame ();                   //开始捕获帧
                    }

                    
else        //如果当前正在进行其它捕获工作
                    
                        axCap.AutoSave 
=false;                 //关自动保存
                        axCap.Interval =0;                     //自动保存周期置0
                        butStartCap.Text ="开始捕获";
                        flagCapingFarmes
=false;                //置工作空闲标志
                        DisConnect();                          //断开连接 关闭捕获 
                    }

                }

                
else   //执行流捕获
                {
                    
if(!flagCapingStream )
                    
{
                        axCap.VideoCompressorIndex 
=cobVicomp.SelectedIndex ;
                        axCap.AudioDeviceIndex 
=comAudioDevice.SelectedIndex ;
                        axCap.AudioCompressorIndex 
=comAudioComp.SelectedIndex ;
                        axCap.StreamFile 
=saveFile.FileName ;
                        butStartCap.Text 
="停止捕获";
                        flagCapingStream
=true;
                        
if(!axCap.Preview )   //打开自动预览
                        {
                            axCap.Visible 
=true;
                            axCap.Preview 
=true;
                            txtBack.Visible 
=false;
                            butCaptureImg.Enabled 
=true;
                            butPreview.Text 
="停止预览";
                        }

                        
try
                        
{
                            axCap.StartCapture ();
                        }

                        
catch
                        
{
                            MessageBox.Show (
"视频编码器不可用,请重新连接设备");
                            cobVicomp.Items.Clear ();
                            butStartCap.Text 
="开始捕获";
                            flagCapingStream
=false;
                            axCap.Preview 
=false;
                            mnuDisableLink.Enabled 
=false;
                            mnuLink.Enabled 
=true;
                            cobColor.Enabled 
=false;
                            cobPix.Enabled 
=false;
                            cobColor.Text 
=null;
                            cobColor.Items.Clear ();
                            cobPix.Text 
=null;
                            cobPix.Items .Clear ();
                            txtBack.Visible 
=true;
                            chkAdvCap.Enabled 
=false;
                            butPreview.Enabled 
=false;
                            butSet.Enabled 
=false;
                            butDeviceSet.Enabled 
=false;
                            butVideoFormat.Enabled 
=false;
                            butCaptureImg.Enabled 
=false;
                            chkData.Enabled 
=false;
                            chkTime.Enabled 
=false;
                            txtLable.Enabled 
=false;
                            butPreview.Text 
="开始预览";
                            butLink.Text 
="连接设备";
                            
if(chkAdvCap.Checked )
                            chkAdvCap.CheckState 
=CheckState.Unchecked ;
                            visableControl(
false,3);
                        }

                    }

                    
else
                    
{
                        
if(axCap.Preview )
                        
{
                            axCap.Preview 
=false;
                            axCap.Visible 
=false;
                            txtBack.Visible 
=true;
                            butCaptureImg.Enabled 
=false;
                            butPreview.Text 
="开始预览";
                        }

                        butStartCap.Text 
="开始捕获";
                        flagCapingStream
=false;
                        axCap.EndCapture ();
                    }

                }

            }

            
else
            
{
                MessageBox.Show (
"请先设置要存储的文件!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
            }

            
        }

        
        
private void chkAutoSave_CheckedChanged(object sender, System.EventArgs e)
        
{
            
//自动保存文件
            visableControl(true,1);
            
if(chkAutoSave.Checked )
            
{
                axCap.Interval 
=Convert.ToInt32 (txtTime.Text ,10); //首先设置自动保存周期
                axCap.AutoSave =true;
            }

            
else
            
{
                axCap.Interval 
=0;
                axCap.AutoSave 
=false;
            }

        }

        
        
private void chkAutoRename_CheckedChanged(object sender, System.EventArgs e)
        
{
            
//自动改变文件名
            visableControl(true,1);
            
if(chkAutoRename.Checked )
            axCap.AutoIncrement 
=true;
            
else
            axCap.AutoIncrement 
=false;
        }

        
        
private void chkExtJpg_CheckedChanged(object sender, System.EventArgs e)
        
{
            visableControl(
true,1);
            
if(chkExtJpg.Checked )
            axCap.SaveJPGProgressive 
=true;
            
else
            axCap.SaveJPGProgressive 
=false;
        }

        
        
private void butSaveFrame_Click(object sender, System.EventArgs e)
        
{
            
if(saveFile.FileName!="")
            
{
                axCap.CaptureFrame ();
                axCap.SaveFrame ();
            }

            
else
            MessageBox.Show (
"请先设置要存储的文件!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
        }

        
        
private void txtLum_TextChanged(object sender, System.EventArgs e)
        
{
            
try
            
{
                axCap.SaveJPGLumFactor 
=Convert.ToInt32 (txtLum.Text ,10);
            }

            
catch
            
{
                MessageBox.Show (
"输入数据的有错,请重新输入!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                txtLum.Text 
="30";
            }

            sclLum.Value 
=axCap.SaveJPGLumFactor ;
        }

        
        
private void txtChrom_TextChanged(object sender, System.EventArgs e)
        
{
            
try
            
{
                axCap.SaveJPGChromFactor 
=Convert.ToInt32 (txtChrom.Text ,10);
            }

            
catch
            
{
                MessageBox.Show (
"输入数据的有错,请重新输入!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                txtChrom.Text 
="36";
            }

            sclChrom.Value 
=axCap.SaveJPGChromFactor ;
        }

        
        
private void sclLum_ValueChanged(object sender, System.EventArgs e)
        
{
            txtLum.Text 
=sclLum.Value .ToString ();
        }

        
        
private void sclChrom_ValueChanged(object sender, System.EventArgs e)
        
{
            txtChrom.Text 
=sclChrom.Value.ToString ();
        }

        
        
private void txtTime_TextChanged(object sender, System.EventArgs e)
        
{
            
try
            
{
                axCap.Interval 
=Convert.ToInt32 (txtTime.Text ,10);
            }

            
catch
            
{
                MessageBox.Show (
"输入数据的有错,请重新输入!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                txtTime.Text 
="10";
            }

            axCap.CaptureFrame ();
        }

        
        
/// <summary>
        
/// 连接设备
        
/// </summary>

        public void ConnectDevice()
        
{
            
try
            
{
                axCap.Connect (cobDevice.SelectedIndex );   
//连接选定的设备
            }

            
catch
            
{
                
//连接失败的提示错误并退出程序
                MessageBox.Show ("设备连接错误,请检查后重新连接!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
                Application.Exit ();
            }

            axCap.Size 
=m_size;
            mnuDisableLink.Enabled 
=true;
            mnuLink.Enabled 
=false;
            chkData.Enabled 
=true;
            chkTime.Enabled 
=true;
            txtLable.Enabled 
=true;
            butPreview.Enabled 
=true;
            butSet.Enabled 
=true;
            butDeviceSet.Enabled 
=true;
            butVideoFormat.Enabled 
=true;
            cobColor.Enabled 
=true;
            cobPix.Enabled 
=true;
            chkAdvCap.Enabled 
=true;
            
try
            
{
                
//枚举当前可用的色彩格式,并用来初始化组合框
                for(int i=0;i<axCap .NumVideoColorFormats ;i++)
                cobColor.Items .Add (axCap .ObtainVideoColorFormatName (i));
                cobColor.SelectedIndex 
=0 ;        //设置默认颜色
            }

            
catch
            
{
                
//枚举不成功
                MessageBox.Show ("不能获取颜色!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
            }

            
try
            
{
                
//枚举当前可用分辨率来初始化组合框
                for(int i=0;i<axCap .NumVideoResolutions ;i++)
                cobPix.Items.Add (axCap .ObtainVideoResolutionName(i) );
                cobPix.SelectedIndex 
=1 ;          //设置默认分辨率
            }

            
catch
            
{
                
//枚举失败则提示错误
                MessageBox.Show ("不能获取分辨率!","错误",MessageBoxButtons.OK ,MessageBoxIcon.Error );
            }

            butLink.Text 
="断开连接";
        }

        
        
/// <summary>
        
/// 断开连接
        
/// </summary>

        public void DisConnect()
        
{
            
//控制-->断开连接
            axCap.Preview =false;
            axCap.Disconnect();
            mnuDisableLink.Enabled 
=false;
            mnuLink.Enabled 
=true;
            cobColor.Enabled 
=false;
            cobPix.Enabled 
=false;
            cobColor.Text 
=null;
            cobColor.Items.Clear ();
            cobPix.Text 
=null;
            cobPix.Items .Clear ();
            txtBack.Visible 
=true;
            chkAdvCap.Enabled 
=false;
            butPreview.Enabled 
=false;
            butSet.Enabled 
=false;
            butDeviceSet.Enabled 
=false;
            butVideoFormat.Enabled 
=false;
            butCaptureImg.Enabled 
=false;
            chkData.Enabled 
=false;
            chkTime.Enabled 
=false;
            txtLable.Enabled 
=false;
            butPreview.Text 
="开始预览";
            butLink.Text 
="连接设备";
            
if(chkAdvCap.Checked )
            chkAdvCap.CheckState 
=CheckState.Unchecked ;
            visableControl(
false,3);
        }

        
        
private void butVideoComp_Click(object sender, System.EventArgs e)
        
{
            
//配置视频编码器
            axCap.ShowFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE.FILTERPROP_VIDEO_COMPRESSOR, "");
        }

        
        
private void comAudioDevice_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
//获取音频设备
            axCap.AudioDeviceIndex =comAudioDevice.SelectedIndex ;
        }

        
        
private void butAudioDevice_Click(object sender, System.EventArgs e)
        
{
            
//配置音频设备
            axCap.ShowFilterPropertyPage (CAPTUREPRO3Lib.FILTERPROPTYPE.FILTERPROP_AUDIO_DEVICE , "");
        }

        
        
private void comAudioComp_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            
//获取音频编码器
            axCap.AudioCompressorIndex =comAudioComp.SelectedIndex ;
        }

        
        
/// <summary>
        
/// 遍历视频编码器,音频设备,音频编码器
        
/// </summary>

        public void GetSystemInfo()
        
{
            
if (axCap.NumVideoCompressors > 0)
            
{
                
for (int i = 0; i < axCap.NumVideoCompressors; i++)
                
{
                    cobVicomp.Items.Add (axCap.ObtainVideoCompressorName (i));
                }

                
for(int i=0;i<cobVicomp.Items.Count  ;i++)
                
{
                    axCap.VideoCompressorIndex 
=i;
                    
if(!axCap.HasFilterPropertyPage(CAPTUREPRO3Lib.FILTERPROPTYPE.FILTERPROP_VIDEO_COMPRESSOR))
                    
if(!butVideoComp. Enabled )
                    cobVicomp.Items.RemoveAt (i);
                }

                
if(axCap.VideoCompressorIndex ==-1)
                cobVicomp.SelectedIndex 
=0;
                
else
                cobVicomp.SelectedIndex  
=axCap.VideoCompressorIndex ;
                EnableVideoComp();
            }

            
else
            
{
                cobVicomp.Text 
="未发现可用视频编码器";
            }

            
if (axCap.NumAudioDevices > 0)
            
{
                
for (int i = 0; i <axCap.NumAudioDevices; i++)
                
{
                    comAudioDevice.Items.Add (axCap.ObtainAudioDeviceName (i));
                }

                
if(axCap.AudioDeviceIndex ==-1)
                comAudioDevice.SelectedIndex 
=0;
                
else
                comAudioDevice.SelectedIndex 
=axCap.AudioDeviceIndex ;
            }

            
else
            
{
                comAudioDevice.Text 
="未发现可用音频设备";
            }

            
if (axCap.NumAudioCompressors > 0)
            
{
                
for (int i = 0; i < axCap.NumAudioCompressors; i++)
                
{
                    comAudioComp.Items.Add (axCap.ObtainAudioCompressorName (i));
                }

                
if(axCap.AudioCompressorIndex ==-1)
                comAudioComp.SelectedIndex 
=0;
                
else
                comAudioComp.SelectedIndex 
=axCap.AudioCompressorIndex ;
            }

            
else
            
{
                comAudioComp.Text
= "未发现可用音频编码器";
            }

        }

        
        
private void cobVicomp_SelectedIndexChanged(object sender, System.EventArgs e)
        
{
            axCap.VideoCompressorIndex 
= cobVicomp.SelectedIndex ;
            EnableVideoComp();
        }

        
        
/// <summary>
        
/// 是否可以配置视频解码器
        
/// </summary>

        public void EnableVideoComp()
        
{
            
if (axCap.IsConnected)// && cobVicomp.SelectedIndex >= 0)
            {
                butVideoComp.Enabled 
=axCap.HasFilterPropertyPage(CAPTUREPRO3Lib.FILTERPROPTYPE.FILTERPROP_VIDEO_COMPRESSOR);
            }

            
else
            
{
                butVideoComp.Enabled 
= false;    
            }

            
        }

        
        
        
        
        
        
        
    }

}

 
原创粉丝点击