首页 > 编程 > C# > 正文

C#实现控制摄像头的类

2019-10-29 21:38:38
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了C#实现控制摄像头的类,涉及C#操作摄像头的初始化、抓图、录像等功能,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#实现控制摄像头的类。分享给大家供大家参考。具体如下:

 

 
  1. /// <summary> 
  2. /// 一个控制摄像头的类 
  3. /// </summary> 
  4. public class Pick 
  5. { 
  6. private const int WM_USER = 0x400; 
  7. private const int WS_CHILD = 0x40000000; 
  8. private const int WS_VISIBLE = 0x10000000; 
  9. private const int WM_CAP_START = WM_USER; 
  10. private const int WM_CAP_STOP = WM_CAP_START + 68; 
  11. private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; 
  12. private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; 
  13. private const int WM_CAP_SAVEDIB = WM_CAP_START + 25; 
  14. private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60; 
  15. private const int WM_CAP_SEQUENCE = WM_CAP_START + 62; 
  16. private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20; 
  17. private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63; 
  18. private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51; 
  19. private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50; 
  20. private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6; 
  21. private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2; 
  22. private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3; 
  23. private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5; 
  24. private const int WM_CAP_SET_SCALE = WM_CAP_START + 53; 
  25. private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52; 
  26. private IntPtr hWndC; 
  27. private bool bStat = false; 
  28. private IntPtr mControlPtr; 
  29. private int mWidth; 
  30. private int mHeight; 
  31. private int mLeft; 
  32. private int mTop; 
  33. /// <summary> 
  34. /// 初始化摄像头 
  35. /// </summary> 
  36. /// <param name="handle">控件的句柄</param> 
  37. /// <param name="left">开始显示的左边距</param> 
  38. /// <param name="top">开始显示的上边距</param> 
  39. /// <param name="width">要显示的宽度</param> 
  40. /// <param name="height">要显示的长度</param> 
  41. public Pick(IntPtr handle, int left, int top, int width, int height) 
  42. { 
  43. mControlPtr = handle; 
  44. mWidth = width; 
  45. mHeight = height; 
  46. mLeft = left; 
  47. mTop = top; 
  48. } 
  49. [DllImport("avicap32.dll")] 
  50. private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); 
  51. [DllImport("avicap32.dll")] 
  52. private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize); 
  53. [DllImport("User32.dll")] 
  54. private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam); 
  55. /// <summary> 
  56. /// 开始显示图像 
  57. /// </summary> 
  58. public void Start() 
  59. { 
  60. if (bStat) 
  61. return; 
  62. bStat = true; 
  63. byte[] lpszName = new byte[100]; 
  64. hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0); 
  65. if (hWndC.ToInt32() != 0) 
  66. { 
  67. SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); 
  68. SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); 
  69. SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); 
  70. SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); 
  71. SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0); 
  72. SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); 
  73. SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0); 
  74. SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0); 
  75. } 
  76. return; 
  77. } 
  78. /// <summary> 
  79. /// 停止显示 
  80. /// </summary> 
  81. public void Stop() 
  82. { 
  83. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 
  84. bStat = false; 
  85. } 
  86. /// <summary> 
  87. /// 抓图 
  88. /// </summary> 
  89. /// <param name="path">要保存bmp文件的路径</param> 
  90. public void GrabImage(string path) 
  91. { 
  92. IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); 
  93. SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64()); 
  94. } 
  95. /// <summary> 
  96. /// 录像 
  97. /// </summary> 
  98. /// <param name="path">要保存avi文件的路径</param> 
  99. public void Kinescope(string path) 
  100. { 
  101. IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); 
  102. SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64()); 
  103. SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0); 
  104. } 
  105. /// <summary> 
  106. /// 停止录像 
  107. /// </summary> 
  108. public void StopKinescope() 
  109. { 
  110. SendMessage(hWndC, WM_CAP_STOP, 0, 0); 
  111. } 
  112. } 

希望本文所述对大家的C#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表