c#摄像头编程实例

这是一个关于设想头的类

usingsystem;
usingSystem.Runtime.InteropServices;

namespacewebcam
{
///
///avicap的摘要说明。
///
publicclassshowVideo
{
//showVideocalls
[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,shortwParam,FrameEventHandlerlParam);
[DllImport("User32.dll")]publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,refBITMAPINFOlParam);
[DllImport("User32.dll")]publicstaticexternintSetWindowPos(IntPtrhWnd,inthWndInsertAfter,intx,inty,intcx,intcy,intwFlags);
[DllImport("avicap32.dll")]publicstaticexternintcapGetVideoFormat(IntPtrhWnd,IntPtrpsVideoFormat,intwSize);

//constants
publicconstintWM_USER=0x400;
publicconstintWS_CHILD=0x40000000;
publicconstintWS_VISIBLE=0x10000000;
publicconstintSWP_NOMOVE=0x2;
publicconstintSWP_NOZORDER=0x4;
publicconstintWM_CAP_DRIVER_CONNECT=WM_USER+10;
publicconstintWM_CAP_DRIVER_DISCONNECT=WM_USER+11;
publicconstintWM_CAP_SET_CALLBACK_FRAME=WM_USER+5;
publicconstintWM_CAP_SET_PREVIEW=WM_USER+50;
publicconstintWM_CAP_SET_PREVIEWRATE=WM_USER+52;
publicconstintWM_CAP_SET_VIDEOFORMAT=WM_USER+45;

//Structures
[StructLayout(LayoutKind.Sequential)]publicstructVIDEOHDR
{
[MarshalAs(UnmanagedType.I4)]publicintlpData;
[MarshalAs(UnmanagedType.I4)]publicintdwBufferLength;
[MarshalAs(UnmanagedType.I4)]publicintdwBytesUsed;
[MarshalAs(UnmanagedType.I4)]publicintdwTimeCaptured;
[MarshalAs(UnmanagedType.I4)]publicintdwUser;
[MarshalAs(UnmanagedType.I4)]publicintdwFlags;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=4)]publicint[]dwReserved;
}

[structlayout(layoutkind.sequential)]publicstructbitmapinfoheader
{
[MarshalAs(UnmanagedType.I4)]publicInt32biSize;
[MarshalAs(UnmanagedType.I4)]publicInt32biWidth;
[MarshalAs(UnmanagedType.I4)]publicInt32biHeight;
[MarshalAs(UnmanagedType.I2)]publicshortbiPlanes;
[MarshalAs(UnmanagedType.I2)]publicshortbiBitCount;
[MarshalAs(UnmanagedType.I4)]publicInt32biCompression;
[MarshalAs(UnmanagedType.I4)]publicInt32biSizeImage;
[MarshalAs(UnmanagedType.I4)]publicInt32biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]publicInt32biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)]publicInt32biClrUsed;
[MarshalAs(UnmanagedType.I4)]publicInt32biClrImportant;
}

[structlayout(layoutkind.sequential)]publicstructbitmapinfo
{
[MarshalAs(UnmanagedType.Struct,SizeConst=40)]publicBITMAPINFOHEADERbmiHeader;
[MarshalAs(UnmanagedType.ByValArray,SizeConst=1024)]publicInt32[]bmiColors;
}

publicdelegatevoidFrameEventHandler(IntPtrlwnd,IntPtrlpVHdr);

//Publicmethods
publicstaticobjectGetStructure(IntPtrptr,valueTypestructure)
{
returnMarshal.PtrToStructure(ptr,structure.GetType());
}

publicstaticobjectGetStructure(intptr,valueTypestructure)
{
returnGetStructure(newIntPtr(ptr),structure);
}

publicstaticvoidCopy(IntPtrptr,byte[]data)
{
Marshal.Copy(ptr,data,0,data.Length);
}

publicstaticvoidCopy(intptr,byte[]data)
{
Copy(newIntPtr(ptr),data);
}

publicstaticintSizeOf(objectstructure)
{
returnMarshal.SizeOf(structure);
}
}

//webcameraclass
publicclassWebCamera
{
//Constructur
publicWebCamera(IntPtrhandle,intwidth,intheight)
{
mControlPtr=handle;
mWidth=width;
mHeight=height;
}

//delegateforframecallback
publicdelegatevoidRecievedFrameEventHandler(byte[]data);
publiceventRecievedFrameEventHandlerRecievedFrame;

privateIntPtrlwndC;//Holdstheunmanagedhandleofthecontrol
privateIntPtrmControlPtr;//Holdsthemanagedpointerofthecontrol
privateintmWidth;
privateintmHeight;

privateshowVideo.FrameEventHandlermFrameEventHandler;//Delegateinstancefortheframecallback-mustkeepalive!gcshouldNOTcollectit

//Closethewebcamera
publicvoidCloseWebcam()
{
this.capDriverDisconnect(this.lwndC);
}

//startthewebcamera
publicvoidStartWebCam()
{
byte[]lpszName=newbyte[100];
byte[]lpszVer=newbyte[100];

showVideo.capGetDriverDescriptionA(0,lpszName,100,lpszVer,100);
this.lwndC=showVideo.capCreateCaptureWindowA(lpszName,showVideo.WS_VISIBLE+showVideo.WS_CHILD,0,0,mWidth,mHeight,mControlPtr,0);

if(this.capDriverConnect(this.lwndC,0))
{
this.capPreviewRate(this.lwndC,66);
this.capPreview(this.lwndC,true);
showVideo.BITMAPINFObitmapinfo=newshowVideo.BITMAPINFO();
bitmapinfo.bmiHeader.biSize=showVideo.SizeOf(bitmapinfo.bmiHeader);
bitmapinfo.bmiHeader.biWidth=352;
bitmapinfo.bmiHeader.biHeight=288;
bitmapinfo.bmiHeader.biPlanes=1;
bitmapinfo.bmiHeader.biBitCount=24;
this.capSetVideoFormat(this.lwndC,refbitmapinfo,showVideo.SizeOf(bitmapinfo));
this.mFrameEventHandler=newshowVideo.FrameEventHandler(FrameCallBack);
this.capSetCallbackOnFrame(this.lwndC,this.mFrameEventHandler);
showVideo.SetWindowPos(this.lwndC,0,0,0,mWidth,mHeight,6);
}
}

//privatefunctions
privateboolcapDriverConnect(IntPtrlwnd,shorti)
{
returnshowVideo.SendMessage(lwnd,showVideo.WM_CAP_DRIVER_CONNECT,i,0);
}

privateboolcapdriverdisconnect(intptrlwnd)
{
returnshowVideo.SendMessage(lwnd,showVideo.WM_CAP_DRIVER_DISCONNECT,0,0);
}

privateboolcapPreview(IntPtrlwnd,boolf)
{
returnshowVideo.SendMessage(lwnd,showVideo.WM_CAP_SET_PREVIEW,f,0);
}

privateboolcappreviewrate(intptrlwnd,shortwms)
{
returnshowVideo.SendMessage(lwnd,showVideo.WM_CAP_SET_PREVIEWRATE,wMS,0);
}

privateboolcapSetCallbackOnFrame(IntPtrlwnd,showVideo.FrameEventHandlerlpProc)
{
returnshowVideo.SendMessage(lwnd,showVideo.WM_CAP_SET_CALLBACK_FRAME,0,lpProc);
}

privateboolcapsetvideoformat(intptrhcapwnd,refshowvideo.bitmapinfobmpformat,intcapformatsize)
{
returnshowVideo.SendMessage(hCapWnd,showVideo.WM_CAP_SET_VIDEOFORMAT,CapFormatSize,refBmpFormat);
}

privatevoidframecallback(intptrlwnd,intptrlpvhdr)
{
showVideo.VIDEOHDRvideoHeader=newshowVideo.VIDEOHDR();
byte[]VideoData;
videoHeader=(showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr,videoHeader);
VideoData=newbyte[videoHeader.dwBytesUsed];
showVideo.Copy(videoHeader.lpData,VideoData);
if(this.RecievedFrame!=null)
this.RecievedFrame(VideoData);
}
}

}

具体调用如下:

usingsystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingwebcam;

namespacewebcam
{
///
///Form1的摘要说明。
///
publicclassForm1:System.Windows.Forms.Form
{
privateSystem.Windows.Forms.PanelpanelPreview;
privateSystem.Windows.Forms.Buttonb_play;
privateSystem.Windows.Forms.Buttonb_stop;
///
///必需的设计器变量。
///
privateSystem.ComponentModel.Containercomponents=null;
WebCamerawc;

publicform1()
{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();

//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}

///
///清理所有正在使用的资源。
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#regionwindows窗体设计器生成的代码
///
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///
privatevoidInitializeComponent()
{
this.b_play=newSystem.Windows.Forms.Button();
this.panelPreview=newSystem.Windows.Forms.Panel();
this.b_stop=newSystem.Windows.Forms.Button();
this.SuspendLayout();
//
//b_play
//
this.b_play.Location=newSystem.Drawing.Point(280,368);
this.b_play.Name="b_play";
this.b_play.TabIndex=0;
this.b_play.Text="&Play";
this.b_play.Click+=newSystem.EventHandler(this.button1_Click);
//
//panelPreview
//
this.panelPreview.Location=newSystem.Drawing.Point(8,8);
this.panelPreview.Name="panelPreview";
this.panelPreview.Size=newSystem.Drawing.Size(344,272);
this.panelPreview.TabIndex=1;
//
//b_stop
//
this.b_stop.Enabled=false;
this.b_stop.Location=newSystem.Drawing.Point(360,368);
this.b_stop.Name="b_stop";
this.b_stop.TabIndex=2;
this.b_stop.Text="&Stop";
this.b_stop.Click+=newSystem.EventHandler(this.b_stop_Click);
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(464,413);
this.Controls.Add(this.b_stop);
this.Controls.Add(this.panelPreview);
this.Controls.Add(this.b_play);
this.MaximizeBox=false;
this.MinimizeBox=false;
this.Name="Form1";
this.Text="GoodViewtestWebCamera";
this.Load+=newSystem.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.Run(newForm1());
}

privatevoidform1_load(objectsender,system.eventargse)
{
b_play.Enabled=false;
b_stop.Enabled=true;
panelPreview.Size=newSize(330,330);
wc=newWebCamera(panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();
}

privatevoidbutton1_click(objectsender,system.eventargse)
{
b_play.Enabled=false;
b_stop.Enabled=true;
panelPreview.Size=newSize(330,330);
wc=newWebCamera(panelPreview.Handle,panelPreview.Width,panelPreview.Height);
wc.StartWebCam();
}

privatevoidb_stop_click(objectsender,system.eventargse)
{
b_play.Enabled=true;
b_stop.Enabled=false;
wc.CloseWebcam();
}
}
}

你可能感兴趣的:(编程,C++,c,windows,C#)