c#摄像头编程

摄像头编程

安装摄像头后,一般可以找到一个avicap32.dll文件

这是一个关于设想头的类

using system;
using System.Runtime.InteropServices;
namespace webcam
{
///
/// avicap 的摘要说明。
///
public class showVideo
{
// showVideo calls
[DllImport("avicap32.dll")] public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
[DllImport("avicap32.dll")] public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
[DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
[DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);
[DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam);
[DllImport("User32.dll")] public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);
[DllImport("User32.dll")] public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("avicap32.dll")]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(LayoutKind.Sequential)] public struct VIDEOHDR
{
[MarshalAs(UnmanagedType.I4)] public int lpData;
[MarshalAs(UnmanagedType.I4)] public int dwBufferLength;
[MarshalAs(UnmanagedType.I4)] public int dwBytesUsed;
[MarshalAs(UnmanagedType.I4)] public int dwTimeCaptured;
[MarshalAs(UnmanagedType.I4)] public int dwUser;
[MarshalAs(UnmanagedType.I4)] public int dwFlags;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)] public int[] dwReserved;
}
[structlayout(layoutkind.sequential)] public struct bitmapinfoheader
{
[MarshalAs(UnmanagedType.I4)] public Int32 biSize ;
[MarshalAs(UnmanagedType.I4)] public Int32 biWidth ;
[MarshalAs(UnmanagedType.I4)] public Int32 biHeight ;
[MarshalAs(UnmanagedType.I2)] public short biPlanes;
[MarshalAs(UnmanagedType.I2)] public short biBitCount ;
[MarshalAs(UnmanagedType.I4)] public Int32 biCompression;
[MarshalAs(UnmanagedType.I4)] public Int32 biSizeImage;
[MarshalAs(UnmanagedType.I4)] public Int32 biXPelsPerMeter;
[MarshalAs(UnmanagedType.I4)] public Int32 biYPelsPerMeter;
[MarshalAs(UnmanagedType.I4)] public Int32 biClrUsed;
[MarshalAs(UnmanagedType.I4)] public Int32 biClrImportant;
}
[structlayout(layoutkind.sequential)] public struct bitmapinfo
{
[MarshalAs(UnmanagedType.Struct, SizeConst=40)] public BITMAPINFOHEADER bmiHeader;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)] public Int32[] bmiColors;
}

public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr);

// Public methods
public static object GetStructure(IntPtr ptr,valueType structure)
{
return Marshal.PtrToStructure(ptr,structure.GetType());
}

public static object GetStructure(int ptr,valueType structure)
{
return GetStructure(new IntPtr(ptr),structure);
}

public static void Copy(IntPtr ptr,byte[] data)
{
Marshal.Copy(ptr,data,0,data.Length);
}

public static void Copy(int ptr,byte[] data)
{
Copy(new IntPtr(ptr),data);
}

public static int SizeOf(object structure)
{
return Marshal.SizeOf(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 showVideo.FrameEventHandler mFrameEventHandler; // Delegate instance for the frame callback - must keep alive! gc should NOT collect it

// Close the web camera
public void CloseWebcam()
{
this.capDriverDisconnect(this.lwndC);
}

// start the web camera
public void StartWebCam()
{
byte[] lpszName = new byte[100];
byte[] lpszVer = new byte[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.BITMAPINFO bitmapinfo = new showVideo.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, ref bitmapinfo, showVideo.SizeOf(bitmapinfo));
this.mFrameEventHandler = new showVideo.FrameEventHandler(FrameCallBack);
this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler);
showVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth , mHeight , 6);
}
}
// private functions
private bool capDriverConnect(IntPtr lwnd, short i)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_CONNECT, i, 0);
}
private bool capdriverdisconnect(intptr lwnd)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_DISCONNECT, 0, 0);
}

private bool capPreview(IntPtr lwnd, bool f)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEW , f, 0);
}
private bool cappreviewrate(intptr lwnd, short wms)
{
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEWRATE, wMS, 0);
}

private bool capSetCallbackOnFrame(IntPtr lwnd, showVideo.FrameEventHandler lpProc)
{ 
return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_CALLBACK_FRAME, 0, lpProc);
}
private bool capsetvideoformat(intptr hcapwnd, ref showvideo.bitmapinfo bmpformat, int capformatsize)
{
return showVideo.SendMessage(hCapWnd, showVideo.WM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat);
}
private void framecallback(intptr lwnd, intptr lpvhdr)
{
showVideo.VIDEOHDR videoHeader = new showVideo.VIDEOHDR();
byte[] VideoData;
videoHeader = (showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr,videoHeader);
VideoData = new byte[videoHeader.dwBytesUsed];
showVideo.Copy(videoHeader.lpData ,VideoData);
if (this.RecievedFrame != null)
this.RecievedFrame (VideoData);
}
}
}

时间: 2024-08-30 13:29:43

c#摄像头编程的相关文章

嵌入式开发-嵌入式摄像头编程要涉及到哪些知识?该如何着手编程?

问题描述 嵌入式摄像头编程要涉及到哪些知识?该如何着手编程? Linux下摄像头相关操作编程,非USB摄像头,使用C语言编程,应该具有的编程思想是什么. 解决方案 嵌入式Linux下Camera编程--V4L2 Linux摄像头编程

usb 摄像头编程

问题描述 小弟做了一个摄像头监控程序usb无驱的调试预览时一直黑屏,抓拍图片保存无图像,但录像保存后却正常...请问一下是什么原因哪?多多指教 解决方案 解决方案二:专业打捞沉贴解决方案三:和我的情况一样usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;namespaceWindowsApplication2{//视频API调用publicclassVi

网络摄像头编程

问题描述 哪位高手可以教我做像CSDN中一样的网络摄像头拍照功能加我QQ117724278.非常感谢 解决方案

c#摄像头编程问题

问题描述 像大多数人一样调用avicap32.dll,API,然后连录像,关闭视频,拍照等.为什么拖动窗口不流畅,有点卡,不能像QQ视频那样呢?求指教 解决方案 解决方案二:顶,求解决!急解决方案三:你用异步方式来执行就好了解决方案四: 解决方案五:LZ你真逗你是如何知道,大多数人都使用avicap32.dll呢?解决方案六:好吧,我是菜鸟,好像用你那个要安装AForge吧!有下载地址吗?谢谢了解决方案七:谢谢,你这个太好了

利用Delphi编程控制摄像头

你的电脑有没有摄像头?看到别人用QQ玩视屏你会不会去想怎么实现的?这里介绍使用DELPHI使用MS的AVICAP32.DLL就可轻松的实现对摄像头编程,如果再加上你的网络编程水平,实现一个视屏聊天就不成什么问题了. 看看下面代码的代码: const WM_CAP_START = WM_USER;const WM_CAP_STOP = WM_CAP_START + 68;const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;const WM_CAP_DRI

非常好!!!Linux源代码阅读——环境准备【转】

Linux源代码阅读--环境准备 转自:http://home.ustc.edu.cn/~boj/courses/linux_kernel/0_prepare.html 目录 Linux 系统环境准备 定制安装 Ubuntu 安装工具链 编译 Linux 内核 默认编译 自定义编译 模拟执行 Linux 用 qemu 模拟 Hello World 系统 准备源码阅读环境 vim 基本设置 在 vim 中使用 cscope 在 vim 中使用 ctags 使用 taglist 显示 symbol

Visual C++编程实现摄像头视频捕捉

前言 DirectShow是微软公司提供的一套在Windows平台上进行流媒体处理的开发包,与DirectX开发包一起发布.DirectShow为多媒体流的捕捉和回放提供了强有力的支持.用DirectShow开发应用程序,我们可以很方便地从支持WDM驱动模型的采集卡上捕获数据,并且进行相应的后期处理乃至存储到文件中. DirectShow是基于COM的,为了编写DirectShow应用程序,需要了解COM客户程序编写的基础知识.DirectShow提供了大量的接口,但在编程中发现还是不够方便,如

弱弱地问一下:可以用电脑opencv编程,用手机做摄像头来识别物体吗?

问题描述 弱弱地问一下:可以用电脑opencv编程,用手机做摄像头来识别物体吗? 大致有哪几模块或步骤(手机仅仅做摄像头,需要在安卓手机上编程吗)?谢谢大神们 解决方案 参考:http://jingyan.baidu.com/article/1876c852d587a9890b1376a3.html

驱动开发-虚拟摄像头开发——如何在C#编程中调用虚拟摄像头驱动

问题描述 虚拟摄像头开发--如何在C#编程中调用虚拟摄像头驱动 我现在在做一个C#的桌面软件,需要使用虚拟摄像头,查了些关于虚拟摄像头开发的资料,稍微了解了一点,但是还是不是很清楚具体该怎么着手做. 请问下如何对虚拟摄像头采用什么编程语言,什么开发工具比较合适,谢谢~