用c#调用摄像头做录像功能

来源:互联网 发布:brew install nginx 编辑:程序博客网 时间:2024/05/15 03:49
Code:
  1. using System.Runtime.InteropServices;   
  2. using System.Drawing;   
  3. using System.Drawing.Imaging;   
  4. using System;   
  5.   
  6. namespace CamTest2   
  7. {   
  8.     /// <summary>   
  9.     /// 一个控制摄像头的类   
  10.     /// </summary>   
  11.     public class Pick   
  12.     {   
  13.         private const int WM_USER = 0x400;   
  14.         private const int WS_CHILD = 0x40000000;   
  15.         private const int WS_VISIBLE = 0x10000000;   
  16.         private const int WM_CAP_START = WM_USER;   
  17.         private const int WM_CAP_STOP = WM_CAP_START + 68;   
  18.         private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;   
  19.         private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;   
  20.         private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;   
  21.         private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;   
  22.         private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;   
  23.         private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;   
  24.         private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;   
  25.         private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;   
  26.         private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;   
  27.         private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;   
  28.         private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;   
  29.         private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;   
  30.         private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;   
  31.         private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;   
  32.         private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;   
  33.         private const int WM_CAP_DLG_VIDEOFORMAT = WM_CAP_START + 41;   
  34.         private const int WM_CAP_DLG_VIDEOSOURCE = WM_CAP_START + 42;   
  35.         private const int WM_CAP_DLG_VIDEODISPLAY = WM_CAP_START + 43;   
  36.         private const int WM_CAP_DLG_VIDEOCOMPRESSION = WM_CAP_START + 46;   
  37.   
  38.         private IntPtr hWndC;   
  39.         private bool bStat = false;   
  40.   
  41.         private IntPtr mControlPtr;   
  42.         private int mWidth;   
  43.         private int mHeight;   
  44.         private int mLeft;   
  45.         private int mTop;   
  46.   
  47.         /// <summary>   
  48.         /// 初始化摄像头   
  49.         /// </summary>   
  50.         /// <param name="handle">控件的句柄</param>   
  51.         /// <param name="left">开始显示的左边距</param>   
  52.         /// <param name="top">开始显示的上边距</param>   
  53.         /// <param name="width">要显示的宽度</param>   
  54.         /// <param name="height">要显示的长度</param>   
  55.         public Pick(IntPtr handle, int left, int top, int width, int height)   
  56.         {   
  57.             mControlPtr = handle;   
  58.             mWidth = width;   
  59.             mHeight = height;   
  60.             mLeft = left;   
  61.             mTop = top;   
  62.         }   
  63.   
  64.         [DllImport("avicap32.dll")]   
  65.         private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);   
  66.   
  67.         [DllImport("avicap32.dll")]   
  68.         private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);   
  69.         [DllImport("User32.dll")]   
  70.         private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);   
  71.           
  72.   
  73.         public void capDlgVideoFormat()   
  74.         {   
  75.             Boolean capDlgVideoFormat = SendMessage(hWndC, WM_CAP_DLG_VIDEOFORMAT, 0, 0);   
  76.   
  77.         }   
  78.         public void capDlgVideoSource()   
  79.         {   
  80.             Boolean capDlgVideoSource = SendMessage(hWndC, WM_CAP_DLG_VIDEOSOURCE, 0, 0);   
  81.   
  82.         }   
  83.         public void capDlgVideoDisplay()   
  84.         {   
  85.             Boolean capDlgVideoDisplay = SendMessage(hWndC, WM_CAP_DLG_VIDEODISPLAY, 0, 0);   
  86.         }   
  87.         public void capDlgVideoCompression()   
  88.         {   
  89.             Boolean capDlgVideoCompression = SendMessage(hWndC, WM_CAP_DLG_VIDEOCOMPRESSION, 0, 0);   
  90.               
  91.         }   
  92.   
  93.         /// <summary>   
  94.         /// 开始显示图像   
  95.         /// </summary>   
  96.         public void Start()   
  97.         {   
  98.             if (bStat)   
  99.                 return;   
  100.   
  101.             bStat = true;   
  102.             byte[] lpszName = new byte[100];   
  103.   
  104.             hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);   
  105.   
  106.             if (hWndC.ToInt32() != 0)   
  107.             {   
  108.                 SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);   
  109.                 SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);   
  110.                 SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);   
  111.                 SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);   
  112.                 SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);   
  113.                 SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);   
  114.                 SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);   
  115.                 SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);   
  116.             }   
  117.   
  118.             return;   
  119.   
  120.         }   
  121.   
  122.         /// <summary>   
  123.         /// 停止显示   
  124.         /// </summary>   
  125.         public void Stop()   
  126.         {   
  127.             SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);   
  128.             bStat = false;   
  129.         }   
  130.   
  131.         /// <summary>   
  132.         /// 抓图   
  133.         /// </summary>   
  134.         /// <param name="path">要保存bmp文件的路径</param>   
  135.         public void GrabImage(string path)   
  136.         {   
  137.                
  138.         IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);   
  139.            SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());   
  140.   
  141.   
  142.         }   
  143.   
  144.         /// <summary>   
  145.         /// 录像   
  146.         /// </summary>   
  147.         /// <param name="path">要保存avi文件的路径</param>   
  148.         public void Kinescope(string path)   
  149.         {   
  150.            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);   
  151.               
  152.   
  153.              
  154.   
  155.             SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64());   
  156.             
  157.   
  158.              SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);   
  159.                
  160.         }   
  161.   
  162.         
  163.   
  164.         /// <summary>   
  165.         /// 停止录像   
  166.         /// </summary>   
  167.         public void StopKinescope()   
  168.         {   
  169.             SendMessage(hWndC, WM_CAP_STOP, 0, 0);   
  170.         }   
  171.        /* public void cap()  
  172.         {  
  173.             CAPTUREPARMS s;  
  174.      capCaptureGetSetup(m_caphwnd,&s,sizeof(CAPTUREPARMS));//取得采集参数  
  175.      s.dwRequestMicroSecPerFrame = 33333;//采集一帧花费1/30秒  
  176.     s.fAbortLeftMouse = FALSE;//压下鼠标左键不终止采集  
  177.     s.fAbortRightMouse = FALSE;//压下鼠标右键不终止采集  
  178.    s.fCaptureAudio = TRUE;//c采集音频  
  179.    s.fYield = TRUE;//使用一个独立的线程来采集视频,不使用View窗口线程  
  180.    capCaptureSetSetup(m_caphwnd,&s,sizeof(CAPTUREPARMS));//设定采集参数  
  181.  
  182.  
  183.         }*/  
  184.     }   
  185. }   
  186.   
  187.   
  188. 摄像头编程大全(源码)(c#)   
  189. using System.Runtime.InteropServices;   
  190. using System.Drawing;   
  191. using System.Drawing.Imaging;   
  192.   
  193. namespace PickHead   
  194. {   
  195. /// <summary>   
  196. /// 一个控制摄像头的类   
  197. /// </summary>   
  198. public class Pick   
  199. {   
  200. private const int WM_USER = 0x400;   
  201. private const int WS_CHILD = 0x40000000;   
  202. private const int WS_VISIBLE = 0x10000000;   
  203. private const int WM_CAP_START = WM_USER;   
  204. private const int WM_CAP_STOP = WM_CAP_START + 68;   
  205. private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;   
  206. private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;   
  207. private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;   
  208. private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;   
  209. private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;   
  210. private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;   
  211. private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;   
  212. private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;   
  213. private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;   
  214. private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;   
  215. private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;   
  216. private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;   
  217. private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;   
  218. private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;   
  219. private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;   
  220. private IntPtr hWndC;   
  221. private bool bStat = false;   
  222.   
  223. private IntPtr mControlPtr;   
  224. private int mWidth;   
  225. private int mHeight;   
  226. private int mLeft;   
  227. private int mTop;   
  228.   
  229. /// <summary>   
  230. /// 初始化摄像头   
  231. /// </summary>   
  232. /// <param name="handle">控件的句柄</param>   
  233. /// <param name="left">开始显示的左边距</param>   
  234. /// <param name="top">开始显示的上边距</param>   
  235. /// <param name="width">要显示的宽度</param>   
  236. /// <param name="height">要显示的长度</param>   
  237. public Pick(IntPtr handle, int left, int top, int width,int height)   
  238. {   
  239. mControlPtr = handle;   
  240. mWidth = width;   
  241. mHeight = height;   
  242. mLeft = left;   
  243. mTop = top;   
  244. }   
  245.   
  246. [DllImport("avicap32.dll")]   
  247. private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);   
  248.   
  249. [DllImport("avicap32.dll")]   
  250. private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );   
  251. [DllImport("User32.dll")]   
  252. private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);   
  253.   
  254. /// <summary>   
  255. /// 开始显示图像   
  256. /// </summary>   
  257. public void Start()   
  258. {   
  259. if (bStat)   
  260. return;   
  261.   
  262. bStat = true;   
  263. byte[] lpszName = new byte[100];   
  264.   
  265. hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);   
  266.   
  267. if (hWndC.ToInt32() != 0)   
  268. {   
  269. SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);   
  270. SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);   
  271. SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);   
  272. SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);   
  273. SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);   
  274. SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);   
  275. SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);   
  276. SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);   
  277. }   
  278.   
  279. return;   
  280.   
  281.   
  282. }   
  283.   
  284. /// <summary>   
  285. /// 停止显示   
  286. /// </summary>   
  287. public void Stop()   
  288. {   
  289. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);   
  290. bStat = false;   
  291. }   
  292.   
  293. /// <summary>   
  294. /// 抓图   
  295. /// </summary>   
  296. /// <param name="path">要保存bmp文件的路径</param>   
  297. public void GrabImage(string path)   
  298. {   
  299.   
  300. IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);   
  301. SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());   
  302.   
  303. }   
  304.   
  305. /// <summary>   
  306. /// 录像   
  307. /// </summary>   
  308. /// <param name="path">要保存avi文件的路径</param>   
  309. public void Kinescope(string path)   
  310. {   
  311. IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);   
  312. SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64());   
  313. SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);   
  314. }   
  315.   
  316. /// <summary>   
  317. /// 停止录像   
  318. /// </summary>   
  319. public void StopKinescope()   
  320. {   
  321. SendMessage(hWndC, WM_CAP_STOP, 0, 0);   
  322. }   
  323.   
  324. }   
  325. }   
  326.   
  327. C#捕捉视频头(源码)       
  328.        
  329.   using   System;     
  330.   using   System.Runtime.InteropServices;     
  331.        
  332.   namespace   Wuyin.ShoesManager     
  333.   {     
  334.           ///   <summary>     
  335.           ///   VedioCapture   的摘要说明。     
  336.           ///   </summary>     
  337.           public   class   VedioCapture     
  338.           {     
  339.                   private   int   hCaptureM;     
  340.                   private   bool   isUnLoad   =   false;     
  341.                   public   VedioCapture()     
  342.                   {                     
  343.                   }     
  344.                   [DllImport("avicap32.dll")]     
  345.                   private   static   extern   int   capCreateCaptureWindow(   string   strWindowName,   int   dwStyle,   int   x,   int   y   ,int   width,   int      
  346.   
  347. height   ,   int   hwdParent,   int   nID   );     
  348.                   [DllImport("user32.dll")]     
  349.                   private   static   extern   int   SendMessage(   int   hwnd   ,   int   wMsg,   int   wParam   ,   int   lParam   );     
  350.                   [DllImport("user32.dll")]     
  351.                   private   static   extern   int   SendMessage(   int   hwnd   ,   int   wMsg,   int   wParam   ,   string   lParam   );     
  352.                   [DllImport("Kernel32.dll")]     
  353.                   private   static   extern   bool   CloseHandle(   int   hObject   );     
  354.                   public   bool   Initialize(   System.Windows.Forms.Control   aContainer   ,   int   intWidth,   int   intHeight   )     
  355.                   {     
  356.                           hCaptureM   =   capCreateCaptureWindow(   "",   0x40000000   |   0x10000000,   0,0,intWidth,intHeight,aContainer.Handle.ToInt32()   ,1   );     
  357.                           if(   hCaptureM   ==   0   )   return   false;     
  358.                                
  359.                           int   ret   =   SendMessage(   hCaptureM   ,   1034,   0,0   );     
  360.                           if(   ret   ==   0   )     
  361.                           {     
  362.                                   CloseHandle(hCaptureM);     
  363.                                   return   false;     
  364.                           }     
  365.                           //WM_CAP_SET_PREVIEW     
  366.                           ret   =   SendMessage(   hCaptureM,   1074,   1,   0   );     
  367.                           if(   ret   ==   0   )     
  368.                           {     
  369.                                   this.UnLoad();     
  370.                                   return   false;     
  371.                           }     
  372.                           //WM_CAP_SET_SCALE     
  373.                           ret   =   SendMessage(   hCaptureM,   1077,   1,   0   );     
  374.                           if(   ret   ==   0   )     
  375.                           {     
  376.                                   this.UnLoad();     
  377.                                   return   false;     
  378.                           }     
  379.                           //WM_CAP_SET_PREVIEWRATE     
  380.                           ret   =   SendMessage(   hCaptureM,   1076,   66,   0   );     
  381.                           if(   ret   ==   0   )     
  382.                           {     
  383.                                   this.UnLoad();     
  384.                                   return   false;     
  385.                           }     
  386.                           return   true;     
  387.                   }     
  388.        
  389.                   public   void   SingleFrameBegin()     
  390.                   {     
  391.                           //     
  392.                           int   ret   =   SendMessage(   hCaptureM,   1094   ,   0,   0   );     
  393.                   }     
  394.                   public   void   SingleFrameEnd()     
  395.                   {     
  396.                           //     
  397.                           int   ret   =   SendMessage(   hCaptureM,   1095   ,   0,   0   );     
  398.                   }     
  399.        
  400.                   public   void   SingleFrameMode()     
  401.                   {     
  402.                           //WM_CAP_GRAB_FRAME     
  403.                           int   ret   =   SendMessage(     hCaptureM,   1084   ,   0,   0   );     
  404.                           //WM_CAP_SET_PREVIEW       
  405.                           //int   ret   =   SendMessage(     hCaptureM,   1074   ,   0,   0   );     
  406.                           //WM_CAP_SINGLE_FRAME     
  407.                           //ret   =   SendMessage(   hCaptureM,   1096   ,   0,   0   );     
  408.                   }     
  409.                   public   void   PreviewMode()     
  410.                   {     
  411.                           int   ret   =   SendMessage(   hCaptureM,   1074   ,   1,   0   );     
  412.                   }                     
  413.        
  414.                   public   void   UnLoad()     
  415.                   {     
  416.                           int   ret   =   SendMessage(   hCaptureM,   1035,   0,   0   );     
  417.                           CloseHandle(   this.hCaptureM   );     
  418.                           isUnLoad   =   true;     
  419.                   }     
  420.        
  421.                   public   void   CopyToClipBorad()     
  422.                   {     
  423.                           int   ret   =   SendMessage(     hCaptureM,   1054,   0,   0   );     
  424.                   }     
  425.        
  426.                   public   void   ShowFormatDialog()     
  427.                   {     
  428.                           int   ret   =   SendMessage(     hCaptureM,   1065,   0,   0   );     
  429.                   }     
  430.                   public   void   SaveToDIB(   string   fileName   )     
  431.                   {     
  432.                           int   ret   =   SendMessage(     hCaptureM,   1049,   0,   fileName   );     
  433.                   }     
  434.        
  435.                   public   void   ShowDisplayDialog()     
  436.                   {     
  437.                           int   ret   =   SendMessage(   hCaptureM,   1067,   0,   0   );     
  438.                   }     
  439.                   public   System.Drawing.Image   getCaptureImage()     
  440.                   {     
  441.                           System.Windows.Forms.IDataObject   iData   =   System.Windows.Forms.Clipboard.GetDataObject();     
  442.                           System.Drawing.Image   retImage   =   null;     
  443.                           if(   iData   !=     null   )       
  444.                           {     
  445.                                   if(   iData.GetDataPresent(   System.Windows.Forms.DataFormats.Bitmap   )   )     
  446.                                   {     
  447.                                           retImage   =   (System.Drawing.Image)iData.GetData(   System.Windows.Forms.DataFormats.Bitmap   );     
  448.                                   }       
  449.                                   else   if(   iData.GetDataPresent(   System.Windows.Forms.DataFormats.Dib     )   )     
  450.                                   {     
  451.                                           retImage   =   (System.Drawing.Image)iData.GetData(   System.Windows.Forms.DataFormats.Dib   );     
  452.                                   }     
  453.                           }     
  454.                           return   retImage;     
  455.                   }     
  456.        
  457.                   ~VedioCapture()     
  458.                   {     
  459.                           if(   !isUnLoad   )     
  460.                           {     
  461.                                   this.UnLoad();     
  462.                           }     
  463.                   }     
  464.           }     
  465.   }   
  466.   

 

原创粉丝点击