利用C#摄像头编程实现驱动开发

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

目前成都创新互联公司已为1000+的企业提供了网站建设、域名、雅安服务器托管网站托管运营、企业网站设计、源城网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

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

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

另外的一些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)(视频教程)

【编辑推荐】

  1. 如何用C#和ADO.NET访问
  2. 浅析C# Switch语句
  3. C#验证输入方法详解
  4. 简单介绍C# 匿名方法
  5. C# FileSystemWatcher对象

分享题目:利用C#摄像头编程实现驱动开发
文章链接:http://www.hantingmc.com/qtweb/news9/519059.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联