C#获取摄像头的几个简单方法.docx
《C#获取摄像头的几个简单方法.docx》由会员分享,可在线阅读,更多相关《C#获取摄像头的几个简单方法.docx(57页珍藏版)》请在冰豆网上搜索。
![C#获取摄像头的几个简单方法.docx](https://file1.bdocx.com/fileroot1/2023-1/27/97678b4b-6cf5-4c8a-910c-a371d1974d1c/97678b4b-6cf5-4c8a-910c-a371d1974d1c1.gif)
C#获取摄像头的几个简单方法
有很多C#获取摄像头的实现方法,都是使用已经过时的技术之所以过时,是因为他们还在使用97年被directshow抛弃的摄像头调用类(avicap32.dll),
现在有比较先进一点的资料,大家到Google里面搜索一下吧!
我自己总结了一下,一些C#获取摄像头的方法很好用的,代码比较少的程序提供给大家下载:
1.C#写的简单的摄像头视频获取源代码
camera.rar
2.我自己正在使用的C#获取摄像头程序,代码相当的短(不到100行),
以下系统必备组件是必需的:
WindowsInstaller3.1
.NETFramework3.5
Debug.rar源代码
c#获取摄像头实现录像
利用普通的简易摄像头,通过C#语言即可开发成简易视频程序。
本实例利用市场上购买的普通摄像头,利用VFW技术,实现单路视频监控系统。
运行程序,窗体中将显示舰体摄像头采集的视频信息。
如图13.9所示。
技术要点
本实例主要使用了VFW(VideoforWindows)技术。
VFW是Microsoft公司为开发Windows平台下的视频应用程序提供的软件工具包,提供了一系列应用程序编程接口(API),用户可以通过这些接口很方便地实现视频捕获、视频编辑及视频播放等通用功能,还可利用回调函数开发比较复杂的视频应用程序。
该技术的特点是播放视频时不需要专用的硬件设备,而且应用灵活,可以满足视频应用程序开发的需要。
Windows操作系统自身就携带了VFW技术,系统安装时,会自动安装VFW的相关组件。
VFW技术主要由六个功能模块组成,下面进行简单说明。
AVICAP32.DLL:
包含执行视频捕获的函数,给AVI文件的I/O处理和视频,音频设备驱动程序提供一个高级接口。
MSVIDEO.DLL:
包含一套特殊的DrawDib函数,用来处理程序上的视频操作。
MCIAVI.DRV:
包括对VFW的MCI命令解释器的驱动程序。
AVIFILE.DLL:
包含由标准多媒体I/O(mmio)函数提供的更高级的命令,用来访问.AVI文件。
ICM:
压缩管理器,用于管理的视频压缩/解压缩的编译码器。
ACM:
音频压缩管理器,提供与ICM相似的服务,适用于波形音频。
AVICAP32.DLL中的函数和USER32.DLL中的函数,函数语法及结构如下。
(1)capCreateCaptureWindow函数
该函数用于创建一个视频捕捉窗口。
语法如下:
[DllImport("avicap32.dll")]
publicstaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);
参数说明如下。
lpszWindowName:
标识窗口的名称。
dwStyle:
标识窗口风格。
x、y:
标识窗口的左上角坐标。
nWidth、nHeight:
标识窗口的宽度和高度。
hWnd:
标识父窗口句柄。
nID:
标识窗口ID。
返回值:
视频捕捉窗口句柄。
(2)SendMessage函数
用于向Windows系统发送消息机制。
[DllImport("User32.dll")]
privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,intlParam);
参数说明如下。
hWnd:
窗口句柄。
wMsg:
将要发送的消息。
wParam、lParam:
消息的参数,每个消息都有两个参数,参数设置由发送的消息而定。
Form1窗体中通过调用视频类中的方法来实现相应的功能。
在【开始录像】按钮的Click事件中添加如下代码:
privatevoidbuttonStart_Click(objectsender,EventArgse)
{
buttonStart.Enabled=false;
buttonStop.Enabled=true;
//btnPz.Enabled=true;
video=newcVideo(picCapture.Handle,picCapture.Width,picCapture.Height);
video.StartWebCam();
}
在【停止录像】按钮的Click事件中添加如下代码:
privatevoidbuttonStop_Click(objectsender,EventArgse)
{
buttonStart.Enabled=true;
buttonStop.Enabled=false;
buttonPz.Enabled=false;
video.CloseWebcam();
}
在【拍照】按钮的Click事件下添加如下代码:
privatevoidbuttonPz_Click(objectsender,EventArgse)
{
video.GrabImage(picCapture.Handle,"d:
\\a.bmp");
}
完整代码如下:
1、Program.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Windows.Forms;
namespaceVideo1
{
staticclassProgram
{
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newForm1());
}
}
}
2、Form1.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespaceVideo1
{
publicpartialclassForm1:
Form
{
cVideovideo;
publicForm1()
{
InitializeComponent();
}
privatevoidbuttonStart_Click(objectsender,EventArgse)
{
buttonStart.Enabled=false;
buttonStop.Enabled=true;
//btnPz.Enabled=true;
video=newcVideo(picCapture.Handle,picCapture.Width,picCapture.Height);
video.StartWebCam();
}
privatevoidbuttonStop_Click(objectsender,EventArgse)
{
buttonStart.Enabled=true;
buttonStop.Enabled=false;
buttonPz.Enabled=false;
video.CloseWebcam();
}
privatevoidbuttonPz_Click(objectsender,EventArgse)
{
video.GrabImage(picCapture.Handle,"d:
\\a.bmp");
}
}
}
3、Form1.Designer.cs
namespaceVideo1
{
partialclassForm1
{
///
///必需的设计器变量。
///
privateSystem.ComponentModel.IContainercomponents=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,为true;否则为false。
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///
privatevoidInitializeComponent()
{
System.ComponentModel.ComponentResourceManagerresources=newSystem.ComponentModel.ComponentResourceManager(typeof(Form1));
this.buttonStart=newSystem.Windows.Forms.Button();
this.buttonStop=newSystem.Windows.Forms.Button();
this.buttonPz=newSystem.Windows.Forms.Button();
this.panelTop=newSystem.Windows.Forms.Panel();
this.picCapture=newSystem.Windows.Forms.PictureBox();
this.panelBottom=newSystem.Windows.Forms.Panel();
this.panelTop.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.picCapture)).BeginInit();
this.panelBottom.SuspendLayout();
this.SuspendLayout();
//
//buttonStart
//
this.buttonStart.Location=newSystem.Drawing.Point(39,3);
this.buttonStart.Name="buttonStart";
this.buttonStart.Size=newSystem.Drawing.Size(88,36);
this.buttonStart.TabIndex=1;
this.buttonStart.Text="开始录像";
this.buttonStart.UseVisualStyleBackColor=true;
this.buttonStart.Click+=newSystem.EventHandler(this.buttonStart_Click);
//
//buttonStop
//
this.buttonStop.Location=newSystem.Drawing.Point(181,3);
this.buttonStop.Name="buttonStop";
this.buttonStop.Size=newSystem.Drawing.Size(80,36);
this.buttonStop.TabIndex=2;
this.buttonStop.Text="停止录像";
this.buttonStop.UseVisualStyleBackColor=true;
this.buttonStop.Click+=newSystem.EventHandler(this.buttonStop_Click);
//
//buttonPz
//
this.buttonPz.Location=newSystem.Drawing.Point(313,3);
this.buttonPz.Name="buttonPz";
this.buttonPz.Size=newSystem.Drawing.Size(77,36);
this.buttonPz.TabIndex=3;
this.buttonPz.Text="拍照";
this.buttonPz.UseVisualStyleBackColor=true;
this.buttonPz.Click+=newSystem.EventHandler(this.buttonPz_Click);
//
//panelTop
//
this.panelTop.AutoSize=true;
this.panelTop.Controls.Add(this.picCapture);
this.panelTop.Dock=System.Windows.Forms.DockStyle.Fill;
this.panelTop.Location=newSystem.Drawing.Point(0,0);
this.panelTop.Name="panelTop";
this.panelTop.Size=newSystem.Drawing.Size(468,372);
this.panelTop.TabIndex=4;
//
//picCapture
//
this.picCapture.Dock=System.Windows.Forms.DockStyle.Fill;
this.picCapture.InitialImage=((System.Drawing.Image)(resources.GetObject("picCapture.InitialImage")));
this.picCapture.Location=newSystem.Drawing.Point(0,0);
this.picCapture.Name="picCapture";
this.picCapture.Size=newSystem.Drawing.Size(468,372);
this.picCapture.TabIndex=0;
this.picCapture.TabStop=false;
//
//panelBottom
//
this.panelBottom.Controls.Add(this.buttonStart);
this.panelBottom.Controls.Add(this.buttonStop);
this.panelBottom.Controls.Add(this.buttonPz);
this.panelBottom.Dock=System.Windows.Forms.DockStyle.Bottom;
this.panelBottom.Location=newSystem.Drawing.Point(0,327);
this.panelBottom.Name="panelBottom";
this.panelBottom.Size=newSystem.Drawing.Size(468,45);
this.panelBottom.TabIndex=5;
//
//Form1
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(468,372);
this.Controls.Add(this.panelBottom);
this.Controls.Add(this.panelTop);
this.Name="Form1";
this.Text="Form1";
this.panelTop.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.picCapture)).EndInit();
this.panelBottom.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.PictureBoxpicCapture;
privateSystem.Windows.Forms.ButtonbuttonStart;
privateSystem.Windows.Forms.ButtonbuttonStop;
privateSystem.Windows.Forms.ButtonbuttonPz;
privateSystem.Windows.Forms.PanelpanelTop;
privateSystem.Windows.Forms.PanelpanelBottom;
}
}
4、ClassVideoAPI.cs
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Runtime.InteropServices;
usingSystem.Threading;
usingSystem.Windows.Forms;
usingSystem.IO;
namespaceVideo1
{
publicclassVideoAPI//视频API类
{
//视频API调用
[DllImport("avicap32.dll")]//包含了执行视频捕获的函数,它给AVI文件I/O和视频、音频设备驱动程序提供一个高级接口
publicstaticexternIntPtrcapCreateCaptureWindow(stringlpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhwndParent,intnID);
[DllImport("AVICAP32.dll",CharSet=CharSet.Unicode)]
publicstaticexternboolcapGetDriverDescription(intwDriverIndex,StringBuilderlpszName,intcbName,StringBuilderlpszVer,intcbVer);
[DllImport("avicap32.dll")]
publicstaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);
[DllImport("avicap32.dll")]
publicstaticexternboolcapGetDriverDescriptionA(shortwDriver,byte[]lpszName,intcbName,byte[]lpszVer,intcbVer);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,boolwParam,intlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,shortwParam,intlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,intlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,shortwParam,FrameEventHandlerlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,refBITMAPINFOlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,refCAPDRIVERCAPSlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,refCAPTUREPARMSlParam);
[DllImport("User32.dll")]
publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,refCAPSTATUSlParam);
[DllImport("User32.dll")]
publicstaticexternintSetWindowPos(IntPtrhWnd,inthWndInsertAfter,intx,inty,intcx,intcy,intwFlags)