用C#如何实现启动摄像头

用C#如何实现启动摄像头,第1张

安装像头后,一般可以找到一个avicap32dll文件 这是一个关于像头的类 using system; using SystemRuntimeInteropServices; namespace webcam { /// /// avicap 的摘要说明。 /// public class showVideo { // showVideo calls [DllImport(avicap32dll)] public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); [DllImport(avicap32dll)] public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer); [DllImport(User32dll)] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam); [DllImport(User32dll)] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam); [DllImport(User32dll)] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam); [DllImport(User32dll)] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam); [DllImport(User32dll)] public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); [DllImport(avicap32dll)]public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize ); // constants public const int WM_USER = 0x400; public const int WS_CHILD = 0x40000000; public const int WS_VISIBLE = 0x10000000; public const int SWP_NOMOVE = 0x2; public const int SWP_NOZORDER = 0x4; public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5; public const int WM_CAP_SET_PREVIEW = WM_USER + 50; public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45; // Structures [StructLayout(LayoutKindSequential)] public struct VIDEOHDR { [MarshalAs(UnmanagedTypeI4)] public int lpData; [MarshalAs(UnmanagedTypeI4)] public int dwBufferLength; [MarshalAs(UnmanagedTypeI4)] public int dwBytesUsed; [MarshalAs(UnmanagedTypeI4)] public int dwTimeCaptured; [MarshalAs(UnmanagedTypeI4)] public int dwUser; [MarshalAs(UnmanagedTypeI4)] public int dwFlags; [MarshalAs(UnmanagedTypeByValArray, SizeConst=4)] public int[] dwReserved; } [structlayout(layoutkindsequential)] public struct bitmapinfoheader { [MarshalAs(UnmanagedTypeI4)] public Int32 biSize ; [MarshalAs(UnmanagedTypeI4)] public Int32 biWidth ; [MarshalAs(UnmanagedTypeI4)] public Int32 biHeight ; [MarshalAs(UnmanagedTypeI2)] public short biPlanes; [MarshalAs(UnmanagedTypeI2)] public short biBitCount ; [MarshalAs(UnmanagedTypeI4)] public Int32 biCompression; [MarshalAs(UnmanagedTypeI4)] public Int32 biSizeImage; [MarshalAs(UnmanagedTypeI4)] public Int32 biXPelsPerMeter; [MarshalAs(UnmanagedTypeI4)] public Int32 biYPelsPerMeter; [MarshalAs(UnmanagedTypeI4)] public Int32 biClrUsed; [MarshalAs(UnmanagedTypeI4)] public Int32 biClrImportant; } [structlayout(layoutkindsequential)] public struct bitmapinfo { [MarshalAs(UnmanagedTypeStruct, SizeConst=40)] public BITMAPINFOHEADER bmiHeader; [MarshalAs(UnmanagedTypeByValArray, SizeConst=1024)] public Int32[] bmiColors; } public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr); // Public methods public static object GetStructure(IntPtr ptr,valueType structure) { return MarshalPtrToStructure(ptr,structureGetType()); } public static object GetStructure(int ptr,valueType structure) { return GetStructure(new IntPtr(ptr),structure); } public static void Copy(IntPtr ptr,byte[] data) { MarshalCopy(ptr,data,0,dataLength); } public static void Copy(int ptr,byte[] data) { Copy(new IntPtr(ptr),data); } public static int SizeOf(object structure) { return MarshalSizeOf(structure); } } //web camera class public class WebCamera { // Constructur public WebCamera(IntPtr handle, int width,int height) { mControlPtr = handle; mWidth = width; mHeight = height; } // delegate for frame callback public delegate void RecievedFrameEventHandler(byte[] data); public event RecievedFrameEventHandler RecievedFrame; private IntPtr lwndC; // Holds the unmanaged handle of the control private IntPtr mControlPtr; // Holds the managed pointer of the control private int mWidth; private int mHeight; private showVideoFrameEventHandler mFrameEventHandler; // Delegate instance for the frame callback - must keep alive! gc should NOT collect it // Close the web camera public void CloseWebcam() { thiscapDriverDisconnect(thislwndC); } // start the web camera public void StartWebCam() { byte[] lpszName = new byte[100]; byte[] lpszVer = new byte[100]; showVideocapGetDriverDescriptionA(0, lpszName, 100,lpszVer, 100); thislwndC = showVideocapCreateCaptureWindowA(lpszName, showVideoWS_VISIBLE + showVideoWS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0); if (thiscapDriverConnect(thislwndC, 0)) { thiscapPreviewRate(thislwndC, 66); thiscapPreview(thislwndC, true); showVideoBITMAPINFO bitmapinfo = new showVideoBITMAPINFO(); bitmapinfobmiHeaderbiSize = showVideoSizeOf(bitmapinfobmiHeader); bitmapinfobmiHeaderbiWidth = 352; bitmapinfobmiHeaderbiHeight = 288; bitmapinfobmiHeaderbiPlanes = 1; bitmapinfobmiHeaderbiBitCount = 24; thiscapSetVideoFormat(thislwndC, ref bitmapinfo, showVideoSizeOf(bitmapinfo)); thismFrameEventHandler = new showVideoFrameEventHandler(FrameCallBack); thiscapSetCallbackOnFrame(thislwndC, thismFrameEventHandler); showVideoSetWindowPos(thislwndC, 0, 0, 0, mWidth , mHeight , 6); } } // private functions private bool capDriverConnect(IntPtr lwnd, short i) { return showVideoSendMessage(lwnd, showVideoWM_CAP_DRIVER_CONNECT, i, 0); } private bool capdriverdisconnect(intptr lwnd) { return showVideoSendMessage(lwnd, showVideoWM_CAP_DRIVER_DISCONNECT, 0, 0); } private bool capPreview(IntPtr lwnd, bool f) { return showVideoSendMessage(lwnd, showVideoWM_CAP_SET_PREVIEW , f, 0); } private bool cappreviewrate(intptr lwnd, short wms) { return showVideoSendMessage(lwnd, showVideoWM_CAP_SET_PREVIEWRATE, wMS, 0); } private bool capSetCallbackOnFrame(IntPtr lwnd, showVideoFrameEventHandler lpProc) { return showVideoSendMessage(lwnd, showVideoWM_CAP_SET_CALLBACK_FRAME, 0, lpProc); } private bool capsetvideoformat(intptr hcapwnd, ref showvideobitmapinfo bmpformat, int capformatsize) { return showVideoSendMessage(hCapWnd, showVideoWM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat); } private void framecallback(intptr lwnd, intptr lpvhdr) { showVideoVIDEOHDR videoHeader = new showVideoVIDEOHDR(); byte[] VideoData; videoHeader = (showVideoVIDEOHDR)showVideoGetStructure(lpVHdr,videoHeader); VideoData = new byte[videoHeaderdwBytesUsed]; showVideoCopy(videoHeaderlpData ,VideoData); if (thisRecievedFrame != null) thisRecievedFrame (VideoData); } } } 具体调用 using system; using SystemDrawing; using SystemCollections; using SystemComponentModel; using SystemWindowsForms; using SystemData; using webcam; namespace webcam { /// /// Form1 的摘要说明。 /// public class Form1 : SystemWindowsFormsForm { private SystemWindowsFormsPanel panelPreview; private SystemWindowsFormsButton b_play; private SystemWindowsFormsButton b_stop; /// /// 必需的设计器变量。 /// private SystemComponentModelContainer components = null; WebCamera wc; public form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { componentsDispose(); } } baseDispose( disposing ); } #region windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码器修改 /// 此方法的内容。 /// private void InitializeComponent() { thisb_play = new SystemWindowsFormsButton(); thispanelPreview = new SystemWindowsFormsPanel(); thisb_stop = new SystemWindowsFormsButton(); thisSuspendLayout(); // // b_play // thisb_playLocation = new SystemDrawingPoint(280, 368); thisb_playName = b_play; thisb_playTabIndex = 0; thisb_playText = &Play; thisb_playClick += new SystemEventHandler(thisbutton1_Click); // // panelPreview // thispanelPreviewLocation = new SystemDrawingPoint(8, 8); thispanelPreviewName = panelPreview; thispanelPreviewSize = new SystemDrawingSize(344, 272); thispanelPreviewTabIndex = 1; // // b_stop // thisb_stopEnabled = false; thisb_stopLocation = new SystemDrawingPoint(360, 368); thisb_stopName = b_stop; thisb_stopTabIndex = 2; thisb_stopText = &Stop; thisb_stopClick += new SystemEventHandler(thisb_stop_Click); // // Form1 // thisAutoScaleBaseSize = new SystemDrawingSize(6, 14); thisClientSize = new SystemDrawingSize(464, 413); thisControlsAdd(thisb_stop); thisControlsAdd(thispanelPreview); thisControlsAdd(thisb_play); thisMaximizeBox = false; thisMinimizeBox = false; thisName = Form1; thisText = GoodView test Web Camera; thisLoad += new SystemEventHandler(thisForm1_Load); thisResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { ApplicationRun(new Form1()); } private void form1_load(object sender, systemeventargs e) { b_playEnabled = false; b_stopEnabled = true; panelPreviewSize = new Size(330,330); wc = new WebCamera( panelPreviewHandle,panelPreviewWidth,panelPreviewHeight); wcStartWebCam(); } private void button1_click(object sender, systemeventargs e) { b_playEnabled = false; b_stopEnabled = true; panelPreviewSize = new Size(330,330); wc = new WebCamera( panelPreviewHandle,panelPreviewWidth,panelPreviewHeight); wcStartWebCam(); } private void b_stop_click(object sender, systemeventargs e) { b_playEnabled = true; b_stopEnabled = false; wcCloseWebcam(); } } }

如果是Windows

9x/Me系统中,屏蔽Ctrl+Alt+Del和各种任务开关键的方法是通过下面的方法实现的:

Const

SPI_SCREENSAVERRUNNING

=

97

Private

Declare

Function

SystemParametersInfo

Lib

"user32"

Alias

_

"SystemParametersInfoA"

(ByVal

uAction

As

Long,

ByVal

uParam

As

Long,

_

lpvParam

As

Any,

ByVal

fuWinIni

As

Long)

As

Long

Dim

pOld

As

Boolean

Call

SystemParametersInfo(SPI_SCREENSAVERRUNNING,

True,

pOld,

0)

但如果是NT/2000,则非常复杂,似乎连系统钩子都不能解决,而需要采取代码注入、远程线程等手段(这些手段也是一些病毒、木马喜欢采用的)。以下有篇文章可以参考:

>

1、The DllMain function is an optional method of entry into a dynamic-link library (DLL)。(简要翻译:对于动态链接库,DllMain是一个可选的入口函数。)这句话很重要,很多初学者可能都认为一个动态链接库肯定要有DllMain函数。其实不然,像很多仅仅包含资源信息的DLL是没有DllMain函数的。

2、系统是在什么时候调用DllMain函数的呢?静态链接时,或动态链接时调用LoadLibrary和FreeLibrary都会调用DllMain函数。DllMain的第二个参数fdwReason指明了系统调用Dll的原因,它可能是::DLL_PROCESS_ATTACH、DLL_PROCESS_DETACHDLL_THREAD_ATTAC、

DLL_THREAD_DETACH。

以下从这四种情况来分析系统何时调用了DllMain。

3、DllMain函数是DLL模块的默认入口点。当Windows加载DLL模块时调用这一函数。系统首先调用全局对象的构造函数,然后调用全局函数DLLMain。DLLMain函数不仅在将DLL链接加载到进程时被调用,在DLL模块与进程分离时(以及其它时候)也被调用。

以上就是关于用C#如何实现启动摄像头全部的内容,包括:用C#如何实现启动摄像头、VB程序怎么实现禁止CTRL+ALT+DEL键、如何添加DllMain函数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9482744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-28
下一篇 2023-04-28

发表评论

登录后才能评论

评论列表(0条)

保存