利用C#摄像头编程实现驱动开发 & 相关资料

C#摄像头编程代码经改造和调试,如下:

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

(2)这是一个关于C#摄像头编程的类:

  1. using System; 
  2. using System.Runtime.InteropServices; 
  3.  
  4. namespace webcam 
  5. ///  
  6. /// avicap 的摘要说明。 
  7. ///  
  8.  public class showVideo 
  9.   // showVideo calls 
  10.   [DllImport("avicap32.dll")]  
  11.   public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); 
  12.   [DllImport("avicap32.dll")]  
  13.   public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer); 
  14.   [DllImport("User32.dll")]  
  15.   public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);  
  16.   [DllImport("User32.dll")]  
  17.   public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);  
  18.   [DllImport("User32.dll")]  
  19.   public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, FrameEventHandler lParam);  
  20.   [DllImport("User32.dll")]  
  21.   public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam); 
  22.   [DllImport("User32.dll")]  
  23.   public static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags); 
  24.   [DllImport("avicap32.dll")] 
  25.   public static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize ); 
  26.   
  27.   // Constants 
  28.   public const int WM_USER = 0x400; 
  29.   public const int WS_CHILD = 0x40000000; 
  30.   public const int WS_VISIBLE = 0x10000000; 
  31.   public const int SWP_NOMOVE = 0x2; 
  32.   public const int SWP_NOZORDER = 0x4; 
  33.   public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10; 
  34.   public const int WM_CAP_DRIVER_DISCONNECT = WM_USER + 11; 
  35.   public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER + 5; 
  36.   public const int WM_CAP_SET_PREVIEW = WM_USER + 50; 
  37.   public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52; 
  38.   public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45; 
  39.       
  40.   // Structures 
  41.   [StructLayout(LayoutKind.Sequential)] public struct VIDEOHDR 
  42.   { 
  43.    [MarshalAs(UnmanagedType.I4)] public int lpData; 
  44.    [MarshalAs(UnmanagedType.I4)] public int dwBufferLength; 
  45.    [MarshalAs(UnmanagedType.I4)] public int dwBytesUsed; 
  46.    [MarshalAs(UnmanagedType.I4)] public int dwTimeCaptured; 
  47.    [MarshalAs(UnmanagedType.I4)] public int dwUser; 
  48.    [MarshalAs(UnmanagedType.I4)] public int dwFlags; 
  49.    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)] public int[] dwReserved; 
  50.   } 
  51.   
  52.   [StructLayout(LayoutKind.Sequential)] public struct BITMAPINFOHEADER 
  53.   { 
  54.    [MarshalAs(UnmanagedType.I4)] public Int32 biSize ; 
  55.    [MarshalAs(UnmanagedType.I4)] public Int32 biWidth ; 
  56.    [MarshalAs(UnmanagedType.I4)] public Int32 biHeight ; 
  57.    [MarshalAs(UnmanagedType.I2)] public short biPlanes; 
  58.    [MarshalAs(UnmanagedType.I2)] public short biBitCount ; 
  59.    [MarshalAs(UnmanagedType.I4)] public Int32 biCompression; 
  60.    [MarshalAs(UnmanagedType.I4)] public Int32 biSizeImage; 
  61.    [MarshalAs(UnmanagedType.I4)] public Int32 biXPelsPerMeter; 
  62.    [MarshalAs(UnmanagedType.I4)] public Int32 biYPelsPerMeter; 
  63.    [MarshalAs(UnmanagedType.I4)] public Int32 biClrUsed; 
  64.    [MarshalAs(UnmanagedType.I4)] public Int32 biClrImportant; 
  65.   }  
  66.   
  67.   [StructLayout(LayoutKind.Sequential)] public struct BITMAPINFO 
  68.   { 
  69.    [MarshalAs(UnmanagedType.Struct, SizeConst=40)] public BITMAPINFOHEADER bmiHeader; 
  70.    [MarshalAs(UnmanagedType.ByValArray, SizeConst=1024)] public Int32[] bmiColors; 
  71.   } 
  72.       
  73.   public delegate void FrameEventHandler(IntPtr lwnd, IntPtr lpVHdr); 
  74.       
  75.   // Public methods 
  76.   public static object GetStructure(IntPtr ptr,ValueType structure) 
  77.   { 
  78.    return Marshal.PtrToStructure(ptr,structure.GetType()); 
  79.   } 
  80.    
  81.   public static object GetStructure(int ptr,ValueType structure) 
  82.   { 
  83.    return GetStructure(new IntPtr(ptr),structure); 
  84.   } 
  85.       
  86.   public static void Copy(IntPtr ptr,byte[] data) 
  87.   { 
  88.    Marshal.Copy(ptr,data,0,data.Length); 
  89.   } 
  90.       
  91.   public static void Copy(int ptr,byte[] data) 
  92.   { 
  93.    Copy(new IntPtr(ptr),data); 
  94.   } 
  95.       
  96.   public static int SizeOf(object structure) 
  97.   { 
  98.    return Marshal.SizeOf(structure);  
  99.   } 
  100.  
  101.   //Web Camera Class 
  102.  public class WebCamera 
  103.   // Constructur 
  104.   public WebCamera(IntPtr handle, int width,int height) 
  105.   { 
  106.    mControlPtr = handle; 
  107.    mWidth = width; 
  108.    mHeight = height; 
  109.   } 
  110.       
  111.   // delegate for frame callback 
  112.   public delegate void RecievedFrameEventHandler(byte[] data); 
  113.   public event RecievedFrameEventHandler RecievedFrame; 
  114.       
  115.   private IntPtr lwndC; // Holds the unmanaged handle of the control 
  116.   private IntPtr mControlPtr; // Holds the managed pointer of the control 
  117.   private int mWidth; 
  118.   private int mHeight; 
  119.       
  120.   private showVideo.FrameEventHandler mFrameEventHandler; // Delegate instance for the frame callback - must keep alive! gc should NOT collect it 
  121.       
  122.   // Close the web camera 
  123.   public void CloseWebcam() 
  124.   { 
  125.    this.capDriverDisconnect(this.lwndC); 
  126.   } 
  127.       
  128.   // start the web camera 
  129.   public void StartWebCam() 
  130.   { 
  131.    byte[] lpszName = new byte[100]; 
  132.    byte[] lpszVer = new byte[100]; 
  133.            
  134.    showVideo.capGetDriverDescriptionA(0, lpszName, 100,lpszVer, 100); 
  135.    this.lwndC = showVideo.capCreateCaptureWindowA(lpszName, showVideo.WS_VISIBLE + showVideo.WS_CHILD, 0, 0, mWidth, mHeight, mControlPtr, 0); 
  136.            
  137.    if (this.capDriverConnect(this.lwndC, 0)) 
  138.    { 
  139.     this.capPreviewRate(this.lwndC, 66); 
  140.     this.capPreview(this.lwndC, true); 
  141.     showVideo.BITMAPINFO bitmapinfo = new showVideo.BITMAPINFO();  
  142.     bitmapinfo.bmiHeader.biSize = showVideo.SizeOf(bitmapinfo.bmiHeader); 
  143.     bitmapinfo.bmiHeader.biWidth = 352; 
  144.     bitmapinfo.bmiHeader.biHeight = 288; 
  145.     bitmapinfo.bmiHeader.biPlanes = 1; 
  146.     bitmapinfo.bmiHeader.biBitCount = 24; 
  147.     this.capSetVideoFormat(this.lwndC, ref bitmapinfo, showVideo.SizeOf(bitmapinfo)); 
  148.     this.mFrameEventHandler = new showVideo.FrameEventHandler(FrameCallBack); 
  149.     this.capSetCallbackOnFrame(this.lwndC, this.mFrameEventHandler); 
  150.     showVideo.SetWindowPos(this.lwndC, 0, 0, 0, mWidth , mHeight , 6); 
  151.    }  
  152.   } 
  153.   
  154.   // private functions 
  155.   private bool capDriverConnect(IntPtr lwnd, short i) 
  156.   { 
  157.    return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_CONNECT, i, 0); 
  158.   } 
  159.   
  160.   private bool capDriverDisconnect(IntPtr lwnd) 
  161.   { 
  162.    return showVideo.SendMessage(lwnd, showVideo.WM_CAP_DRIVER_DISCONNECT, 0, 0); 
  163.   } 
  164.       
  165.   private bool capPreview(IntPtr lwnd, bool f) 
  166.   { 
  167.    return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEW , f, 0); 
  168.   } 
  169.   
  170.   private bool capPreviewRate(IntPtr lwnd, short wMS) 
  171.   { 
  172.    return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_PREVIEWRATE, wMS, 0); 
  173.   } 
  174.       
  175.   private bool capSetCallbackOnFrame(IntPtr lwnd, showVideo.FrameEventHandler lpProc) 
  176.   {      
  177.    return showVideo.SendMessage(lwnd, showVideo.WM_CAP_SET_CALLBACK_FRAME, 0, lpProc); 
  178.   } 
  179.   
  180.   private bool capSetVideoFormat(IntPtr hCapWnd, ref showVideo.BITMAPINFO BmpFormat, int CapFormatSize) 
  181.   { 
  182.    return showVideo.SendMessage(hCapWnd, showVideo.WM_CAP_SET_VIDEOFORMAT, CapFormatSize, ref BmpFormat); 
  183.   } 
  184.   
  185.   private void FrameCallBack(IntPtr lwnd, IntPtr lpVHdr) 
  186.   { 
  187.    showVideo.VIDEOHDR videoHeader = new showVideo.VIDEOHDR(); 
  188.    byte[] VideoData; 
  189.    videoHeader = (showVideo.VIDEOHDR)showVideo.GetStructure(lpVHdr,videoHeader); 
  190.    VideoData = new byte[videoHeader.dwBytesUsed]; 
  191.    showVideo.Copy(videoHeader.lpData ,VideoData); 
  192.    if (this.RecievedFrame != null
  193.     this.RecievedFrame (VideoData); 
  194.   } 
  195.  
  196. 具体调用如下: 
  197.  
  198. using System; 
  199. using System.Drawing; 
  200. using System.Collections; 
  201. using System.ComponentModel; 
  202. using System.Windows.Forms; 
  203. using System.Data; 
  204. using webcam; 
  205.  
  206. namespace webcam 
  207. ///  
  208. /// Form1 的摘要说明。 
  209. ///  
  210.  public class Form1 : System.Windows.Forms.Form 
  211.   private System.Windows.Forms.Panel panelPreview; 
  212.   private System.Windows.Forms.Button b_play; 
  213.   private System.Windows.Forms.Button b_stop; 
  214.   ///  
  215.   /// 必需的设计器变量。 
  216.   ///  
  217.   private System.ComponentModel.Container components = null
  218.   WebCamera wc; 
  219.  
  220.   public Form1() 
  221.   { 
  222.    // 
  223.    // Windows 窗体设计器支持所必需的 
  224.    // 
  225.    InitializeComponent(); 
  226.  
  227.    // 
  228.    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
  229.    // 
  230.   } 
  231.  
  232.   ///  
  233.   /// 清理所有正在使用的资源。 
  234.   ///  
  235.   protected override void Dispose( bool disposing ) 
  236.   { 
  237.    if( disposing ) 
  238.    { 
  239.     if (components != null)  
  240.     { 
  241.      components.Dispose(); 
  242.     } 
  243.    } 
  244.    base.Dispose( disposing ); 
  245.   } 
  246.  
  247.   #region Windows 窗体设计器生成的代码 
  248.   ///  
  249.   /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
  250.   /// 此方法的内容。 
  251.   ///  
  252.   private void InitializeComponent() 
  253.   { 
  254.    this.b_play = new System.Windows.Forms.Button(); 
  255.    this.panelPreview = new System.Windows.Forms.Panel(); 
  256.    this.b_stop = new System.Windows.Forms.Button(); 
  257.    this.SuspendLayout(); 
  258.    //  
  259.    // b_play 
  260.    //  
  261.    this.b_play.Location = new System.Drawing.Point(280, 368); 
  262.    this.b_play.Name = "b_play"; 
  263.    this.b_play.TabIndex = 0; 
  264.    this.b_play.Text = "&Play"; 
  265.    this.b_play.Click += new System.EventHandler(this.button1_Click); 
  266.    //  
  267.    // panelPreview 
  268.    //  
  269.    this.panelPreview.Location = new System.Drawing.Point(8, 8); 
  270.    this.panelPreview.Name = "panelPreview"; 
  271.    this.panelPreview.Size = new System.Drawing.Size(344, 272); 
  272.    this.panelPreview.TabIndex = 1; 
  273.    //  
  274.    // b_stop 
  275.    //  
  276.    this.b_stop.Enabled = false
  277.    this.b_stop.Location = new System.Drawing.Point(360, 368); 
  278.    this.b_stop.Name = "b_stop"; 
  279.    this.b_stop.TabIndex = 2; 
  280.    this.b_stop.Text = "&Stop"; 
  281.    this.b_stop.Click += new System.EventHandler(this.b_stop_Click); 
  282.    //  
  283.    // Form1 
  284.    //  
  285.    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
  286.    this.ClientSize = new System.Drawing.Size(464, 413); 
  287.    this.Controls.Add(this.b_stop); 
  288.    this.Controls.Add(this.panelPreview); 
  289.    this.Controls.Add(this.b_play); 
  290.    this.MaximizeBox = false
  291.    this.MinimizeBox = false
  292.    this.Name = "Form1"; 
  293.    this.Text = "GoodView test Web Camera"; 
  294.    this.Load += new System.EventHandler(this.Form1_Load); 
  295.    this.ResumeLayout(false); 
  296.  
  297.   } 
  298.   #endregion 
  299.  
  300.   ///  
  301.   /// 应用程序的主入口点。 
  302.   ///  
  303.   [STAThread] 
  304.   static void Main()  
  305.   { 
  306.    Application.Run(new Form1()); 
  307.   } 
  308.  
  309.   private void Form1_Load(object sender, System.EventArgs e) 
  310.   { 
  311.    b_play.Enabled = false
  312.    b_stop.Enabled = true
  313.    panelPreview.Size = new Size(330,330); 
  314.    wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height); 
  315.    wc.StartWebCam(); 
  316.   } 
  317.  
  318.   private void button1_Click(object sender, System.EventArgs e) 
  319.   { 
  320.    b_play.Enabled = false
  321.    b_stop.Enabled = true
  322.    panelPreview.Size = new Size(330,330); 
  323.    wc = new WebCamera( panelPreview.Handle,panelPreview.Width,panelPreview.Height); 
  324.    wc.StartWebCam(); 
  325.   } 
  326.  
  327.   private void b_stop_Click(object sender, System.EventArgs e) 
  328.   { 
  329.    b_play.Enabled = true
  330.    b_stop.Enabled = false
  331.    wc.CloseWebcam(); 
  332.   } 

另外的一些C#摄像头编程资料:

Motion Detection Algorithms http://www.codeproject.com/cs/media/Motion_Detection.asp (英文)

Motion detection using web cam http://www.codeproject.com/cs/media/motion_detection_wc.asp (英文)

AVPhone Controls  http://www.banasoft.net/avphone3/avphone3.htm(英文)

Visual C#使用DirectX实现视频播放 http://www.chinaitpower.net/2006Aug/2006-08-19/212417.html (C#)

使用.NET实现视频播放 http://www.chinaitpower.net/A200507/2005-07-27/167331.html

嵌入式Web视频点播系统实现方法 http://www.java-asp.net/java/200504/t_14439.html(JAVA)

以下是FLASH方面的解决方案:

关于FMS视频在线录制的学习记录... http://pigz.cn/blog/article.asp?id=131

[教程]利用FMS做在线视频录制 http://www.cincn.com/article.asp?id=15&page=2

在线录制系统 http://blog.chinaunix.net/u/17508/showart.php?id=202778

WPF中的解决方案:(使用WPF解决视频的相关问题最方便!)

Building an Interactive 3D Video Player http://www.contentpresenter.com/contentpresenter_3DTools.wmv)(视频教程)

原文地址:https://www.cnblogs.com/zhcnblog/p/2714966.html