摄像头VideoClass.cs

分类:.NET2010-06-15 12:21 136人阅读评论(0) 收藏举报

callbackpathbytestringuserfile

[c-sharp]view plaincopy

1.using System.Drawing;  

2.using System.Drawing.Imaging;  

3.using System;  

4.using System.Runtime.InteropServices;  

5.

6.

7.

8.namespace PickHead  

9.{  

10./// <summary>

11./// 一个控制摄像头的类

12./// </summary>

13.publicclass VideoClass  

14.    {  

15.privateconstint WM_USER = 0x400;  

16.privateconstint WS_CHILD = 0x40000000;  

17.privateconstint WS_VISIBLE = 0x10000000;  

18.privateconstint WM_CAP_START = WM_USER;  

19.privateconstint WM_CAP_STOP = WM_CAP_START + 68;  

20.privateconstint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;  

21.privateconstint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;  

22.privateconstint WM_CAP_SAVEDIB = WM_CAP_START + 25;  

23.privateconstint WM_CAP_GRAB_FRAME = WM_CAP_START + 60;  

24.privateconstint WM_CAP_SEQUENCE = WM_CAP_START + 62;  

25.privateconstint WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;  

26.privateconstint WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;  

27.privateconstint WM_CAP_SET_OVERLAY = WM_CAP_START + 51;  

28.privateconstint WM_CAP_SET_PREVIEW = WM_CAP_START + 50;  

29.privateconstint WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;  

30.privateconstint WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;  

31.privateconstint WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;  

32.privateconstint WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;  

33.privateconstint WM_CAP_SET_SCALE = WM_CAP_START + 53;  

34.privateconstint WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;  

35.private IntPtr hWndC;  

36.privatebool bStat = false;  

37.

38.private IntPtr mControlPtr;  

39.privateint mWidth;  

40.privateint mHeight;  

41.privateint mLeft;  

42.privateint mTop;  

43.

44./// <summary>

45./// 初始化摄像头

46./// </summary>

47./// <param name="handle">控件的句柄</param>

48./// <param name="left">开始显示的左边距</param>

49./// <param name="top">开始显示的上边距</param>

50./// <param name="width">要显示的宽度</param>

51./// <param name="height">要显示的长度</param>

52.public VideoClass(IntPtr handle, int left, int top, int width, int height)  

53.        {  

54.            mControlPtr = handle;  

55.            mWidth = width;  

56.            mHeight = height;  

57.            mLeft = left;  

58.            mTop = top;  

59.        }  

60.

61.

62.

63.        [DllImport("avicap32.dll")]  

64.privatestaticextern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);  

65.

66.        [DllImport("avicap32.dll")]  

67.privatestaticexternint capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);  

68.

69.        [DllImport("User32.dll")]  

70.privatestaticexternbool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);  

71.

72.        [DllImport("avicap32.dll")]  

73.publicstaticexternbool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);  

74.

75./// <summary>

76./// 开始显示图像

77./// </summary>

78.publicvoid Start()  

79.        {  

80.if (bStat)  

81.return;  

82.            bStat = true;  

83.byte[] lpszName = newbyte[100];  

84.

85.            hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);  

86.

87.if (hWndC.ToInt32() != 0)  

88.            {  

89.                SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);  

90.                SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);  

91.                SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);  

92.                SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);  

93.                SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);  

94.                SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);  

95.                SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);  

96.                SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);  

97.            }  

98.return;  

99.        }  

100.

101.

102.

103./// <summary>

104./// 停止显示

105./// </summary>

106.publicvoid Stop()  

107.        {  

108.            SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);  

109.            bStat = false;  

110.        }  

111.

112.

113.

114./// <summary>

115./// 抓图

116./// </summary>

117./// <param name="path">要保存bmp文件的路径</param>

118.publicvoid GrabImage(string path)  

119.        {  

120.            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);  

121.            SendMessage(hWndC, WM_CAP_SAVEDIB, 0, hBmp.ToInt64());  

122.        }  

123.

124.

125.

126./// <summary>

127./// 录像

128./// </summary>

129./// <param name="path">要保存avi文件的路径</param>

130.publicvoid Kinescope(string path)  

131.        {  

132.            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);  

133.            SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64());  

134.            SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);  

135.        }  

136.

137.

138.

139./// <summary>

140./// 停止录像

141./// </summary>

142.publicvoid StopKinescope()  

143.        {  

144.            SendMessage(hWndC, WM_CAP_STOP, 0, 0);  

145.        }  

146.    }  

147.}  

分享到