1
Imports System
2
Imports System.Runtime.InteropServices
3
Imports System.Drawing
4
Imports System.Drawing.Imaging
5
6
Public Class CamClass Cam
7
8
Private Const WM_USER As Integer
=
&
H400
9
Private Const WS_CHILD As Integer
=
&
H40000000
10
Private Const WS_VISIBLE As Integer
=
&
H10000000
11
Private Const WM_CAP_START As Integer
=
WM_USER
12
Private Const WM_CAP_STOP As Integer
=
WM_CAP_START
+
68
13
Private Const WM_CAP_DRIVER_CONNECT As Integer
=
WM_CAP_START
+
10
14
Private Const WM_CAP_DRIVER_DISCONNECT As Integer
=
WM_CAP_START
+
11
15
Private Const WM_CAP_SAVEDIB As Integer
=
WM_CAP_START
+
25
16
Private Const WM_CAP_GRAB_FRAME As Integer
=
WM_CAP_START
+
60
17
Private Const WM_CAP_SEQUENCE As Integer
=
WM_CAP_START
+
62
18
Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Integer
=
WM_CAP_START
+
20
19
Private Const WM_CAP_SEQUENCE_NOFILE As Integer
=
WM_CAP_START
+
63
20
Private Const WM_CAP_SET_OVERLAY As Integer
=
WM_CAP_START
+
51
21
Private Const WM_CAP_SET_PREVIEW As Integer
=
WM_CAP_START
+
50
22
Private Const WM_CAP_SET_CALLBACK_VIDEOSTREAM As Integer
=
WM_CAP_START
+
6
23
Private Const WM_CAP_SET_CALLBACK_ERROR As Integer
=
WM_CAP_START
+
2
24
Private Const WM_CAP_SET_CALLBACK_STATUSA As Integer
=
WM_CAP_START
+
3
25
Private Const WM_CAP_SET_CALLBACK_FRAME As Integer
=
WM_CAP_START
+
5
26
Private Const WM_CAP_SET_SCALE As Integer
=
WM_CAP_START
+
53
27
Private Const WM_CAP_SET_PREVIEWRATE As Integer
=
WM_CAP_START
+
52
28
29
Public Const WM_CAP_GET_CAPSTREAMPTR
=
WM_CAP_START
+
1
30
31
Public Const WM_CAP_SET_CALLBACK_STATUS
=
WM_CAP_START
+
3
32
Public Const WM_CAP_SET_CALLBACK_YIELD
=
WM_CAP_START
+
4
33
Public Const WM_CAP_SET_CALLBACK_WAVESTREAM
=
WM_CAP_START
+
7
34
Public Const WM_CAP_GET_USER_DATA
=
WM_CAP_START
+
8
35
Public Const WM_CAP_SET_USER_DATA
=
WM_CAP_START
+
9
36
37
Public Const WM_CAP_DRIVER_GET_NAME
=
WM_CAP_START
+
12
38
Public Const WM_CAP_DRIVER_GET_VERSION
=
WM_CAP_START
+
13
39
Public Const WM_CAP_DRIVER_GET_CAPS
=
WM_CAP_START
+
14
40
41
Public Const WM_CAP_FILE_SET_CAPTURE_FILE
=
WM_CAP_START
+
20
42
Public Const WM_CAP_FILE_GET_CAPTURE_FILE
=
WM_CAP_START
+
21
43
Public Const WM_CAP_FILE_ALLOCATE
=
WM_CAP_START
+
22
44
Public Const WM_CAP_FILE_SAVEAS
=
WM_CAP_START
+
23
45
Public Const WM_CAP_FILE_SET_INFOCHUNK
=
WM_CAP_START
+
24
46
Public Const WM_CAP_FILE_SAVEDIB
=
WM_CAP_START
+
25
47
48
Public Const WM_CAP_EDIT_COPY
=
WM_CAP_START
+
30
49
50
Public Const WM_CAP_SET_AUDIOFORMAT
=
WM_CAP_START
+
35
51
Public Const WM_CAP_GET_AUDIOFORMAT
=
WM_CAP_START
+
36
52
53
Public Const WM_CAP_DLG_VIDEOFORMAT
=
WM_CAP_START
+
41
54
Public Const WM_CAP_DLG_VIDEOSOURCE
=
WM_CAP_START
+
42
55
Public Const WM_CAP_DLG_VIDEODISPLAY
=
WM_CAP_START
+
43
56
Public Const WM_CAP_GET_VIDEOFORMAT
=
WM_CAP_START
+
44
57
Public Const WM_CAP_SET_VIDEOFORMAT
=
WM_CAP_START
+
45
58
Public Const WM_CAP_DLG_VIDEOCOMPRESSION
=
WM_CAP_START
+
46
59
60
Public Const WM_CAP_GET_STATUS
=
WM_CAP_START
+
54
61
Public Const WM_CAP_SET_SCROLL
=
WM_CAP_START
+
55
62
63
Public Const WM_CAP_GRAB_FRAME_NOSTOP
=
WM_CAP_START
+
61
64
65
Public Const WM_CAP_SET_SEQUENCE_SETUP
=
WM_CAP_START
+
64
66
Public Const WM_CAP_GET_SEQUENCE_SETUP
=
WM_CAP_START
+
65
67
Public Const WM_CAP_SET_MCI_DEVICE
=
WM_CAP_START
+
66
68
Public Const WM_CAP_GET_MCI_DEVICE
=
WM_CAP_START
+
67
69
Public Const WM_CAP_ABORT
=
WM_CAP_START
+
69
70
71
Public Const WM_CAP_SINGLE_FRAME_OPEN
=
WM_CAP_START
+
70
72
Public Const WM_CAP_SINGLE_FRAME_CLOSE
=
WM_CAP_START
+
71
73
Public Const WM_CAP_SINGLE_FRAME
=
WM_CAP_START
+
72
74
75
Public Const WM_CAP_PAL_OPEN
=
WM_CAP_START
+
80
76
Public Const WM_CAP_PAL_SAVE
=
WM_CAP_START
+
81
77
Public Const WM_CAP_PAL_PASTE
=
WM_CAP_START
+
82
78
Public Const WM_CAP_PAL_AUTOCREATE
=
WM_CAP_START
+
83
79
Public Const WM_CAP_PAL_MANUALCREATE
=
WM_CAP_START
+
84
80
81
'
// Following added post VFW 1.1
82
Public Const WM_CAP_SET_CALLBACK_CAPCONTROL
=
WM_CAP_START
+
85
83
84
'
// Defines end of the message range
85
Public Const WM_CAP_END
=
WM_CAP_SET_CALLBACK_CAPCONTROL
86
87
Private hWndC As IntPtr
88
Private bStat As Boolean
=
False
89
90
Private mControlPtr As IntPtr
91
Private mWidth As Integer
92
Private mHeight As Integer
93
Private mLeft As Integer
94
Private mTop As Integer
95
96
<
DllImport(
"
avicap32.dll
"
)
>
_
97
Private Shared Function capCreateCaptureWindowA()Function capCreateCaptureWindowA(ByVal lpszWindowName() As Byte, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As IntPtr, ByVal nID As Integer) As IntPtr
98
End Function
99
100
<
DllImport(
"
avicap32.dll
"
)
>
_
101
Private Shared Function capGetVideoFormat()Function capGetVideoFormat(ByVal hWnd As IntPtr, ByVal psVideoFormat As IntPtr, ByVal wSize As Integer) As Integer
102
End Function
103
104
<
DllImport(
"
User32.dll
"
)
>
_
105
Private Shared Function SendMessage()Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Long) As Boolean
106
End Function
107
108
Function capDlgVideoFormat()Function capDlgVideoFormat() As Boolean
109
capDlgVideoFormat
=
SendMessage(hWndC, WM_CAP_DLG_VIDEOFORMAT,
0
,
0
)
110
End Function
111
Function capDlgVideoSource()Function capDlgVideoSource() As Boolean
112
capDlgVideoSource
=
SendMessage(hWndC, WM_CAP_DLG_VIDEOSOURCE,
0
,
0
)
113
End Function
114
Function capDlgVideoDisplay()Function capDlgVideoDisplay() As Boolean
115
capDlgVideoDisplay
=
SendMessage(hWndC, WM_CAP_DLG_VIDEODISPLAY,
0
,
0
)
116
End Function
117
Function capDlgVideoCompression()Function capDlgVideoCompression() As Boolean
118
capDlgVideoCompression
=
SendMessage(hWndC, WM_CAP_DLG_VIDEOCOMPRESSION,
0
,
0
)
119
End Function
120
121
'
接上节
122
123
'
/// <summary>
124
'
/// 初始化摄像头
125
'
/// </summary>
126
'
/// <param name="handle">控件的句柄</param>
127
'
/// <param name="left">开始显示的左边距</param>
128
'
/// <param name="top">开始显示的上边距</param>
129
'
/// <param name="width">要显示的宽度</param>
130
'
/// <param name="height">要显示的长度</param>
131
Public Sub New()Sub New(ByVal handle As IntPtr, ByVal left As Integer, ByVal top As Integer, ByVal width As Integer, ByVal height As Integer)
132
mControlPtr
=
handle
133
mWidth
=
width
134
mHeight
=
height
135
mLeft
=
left
136
mTop
=
top
137
End Sub
138
139
'
/// <summary>
140
'
/// 开始显示图像
141
'
/// </summary>
142
Public Sub Start()Sub Start()
143
If bStat Then
144
Return
145
End If
146
bStat
=
True
147
Dim lpszName(
99
) As Byte
148
149
hWndC
=
capCreateCaptureWindowA(lpszName, WS_CHILD Or WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr,
0
)
150
151
If hWndC.ToInt32()
<>
0
Then
152
SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM,
0
,
0
)
153
SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR,
0
,
0
)
154
SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA,
0
,
0
)
155
SendMessage(hWndC, WM_CAP_DRIVER_CONNECT,
0
,
0
)
156
SendMessage(hWndC, WM_CAP_SET_SCALE,
1
,
0
)
157
SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE,
66
,
0
)
158
SendMessage(hWndC, WM_CAP_SET_OVERLAY,
1
,
0
)
159
SendMessage(hWndC, WM_CAP_SET_PREVIEW,
1
,
0
)
160
End If
161
162
Return
163
End Sub
164
165
'
/// <summary>
166
'
/// 停止显示
167
'
/// </summary>
168
Public Sub [()Sub [Stop]()
169
SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT,
0
,
0
)
170
bStat
=
False
171
End Sub
172
173
'
/// <summary>
174
'
/// 抓图
175
'
/// </summary>
176
'
/// <param name="path">要保存bmp文件的路径</param>
177
Public Sub GrabImage()Sub GrabImage(ByVal path As String)
178
Dim hBmp As IntPtr
=
Marshal.StringToHGlobalAnsi(path)
179
SendMessage(hWndC, WM_CAP_SAVEDIB,
0
, hBmp.ToInt64())
180
End Sub
181
182
'
/// <summary>
183
'
/// 录像
184
'
/// </summary>
185
'
/// <param name="path">要保存avi文件的路径</param>
186
Public Sub Kinescope()Sub Kinescope(ByVal path As String)
187
Dim hBmp As IntPtr
=
Marshal.StringToHGlobalAnsi(path)
188
SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA,
0
, hBmp.ToInt64())
189
SendMessage(hWndC, WM_CAP_SEQUENCE,
0
,
0
)
190
End Sub
191
192
'
/// <summary>
193
'
/// 停止录像
194
'
/// </summary>
195
Public Sub StopKinescope()Sub StopKinescope()
196
SendMessage(hWndC, WM_CAP_STOP,
0
,
0
)
197
End Sub
198
End Class
199
200
下面这个是测试窗体Form1.vb:
201
202
Public Class Form1Class Form1
203
Inherits System.Windows.Forms.Form
204
205
Private cccap As Cam
206
Windows 窗体设计器生成的代码#Region
"
Windows 窗体设计器生成的代码
"
207
208
Public Sub New()Sub New()
209
MyBase.New()
210
211
'
该调用是 Windows 窗体设计器所必需的。
212
InitializeComponent()
213
214
'
在 InitializeComponent() 调用之后添加任何初始化
215
216
End Sub
217
218
'
窗体重写 dispose 以清理组件列表。
219
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
220
If disposing Then
221
If Not (components Is Nothing) Then
222
components.Dispose()
223
End If
224
End If
225
MyBase.Dispose(disposing)
226
End Sub
227
228
'
Windows 窗体设计器所必需的
229
Private components As System.ComponentModel.IContainer
230
231
'
注意: 以下过程是 Windows 窗体设计器所必需的
232
'
可以使用 Windows 窗体设计器修改此过程。
233
'
不要使用代码编辑器修改它。
234
Friend WithEvents Button1 As System.Windows.Forms.Button
235
Friend WithEvents picView As System.Windows.Forms.PictureBox
236
Friend WithEvents Button2 As System.Windows.Forms.Button
237
Friend WithEvents Button3 As System.Windows.Forms.Button
238
Friend WithEvents Button4 As System.Windows.Forms.Button
239
Friend WithEvents Button5 As System.Windows.Forms.Button
240
<
System.Diagnostics.DebuggerStepThrough()
>
Private Sub InitializeComponent()Sub InitializeComponent()
241
Me.picView
=
New System.Windows.Forms.PictureBox
242
Me.Button1
=
New System.Windows.Forms.Button
243
Me.Button2
=
New System.Windows.Forms.Button
244
Me.Button3
=
New System.Windows.Forms.Button
245
Me.Button4
=
New System.Windows.Forms.Button
246
Me.Button5
=
New System.Windows.Forms.Button
247
Me.SuspendLayout()
248
'
249
'
picView
250
'
251
Me.picView.Anchor
=
CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
252
Or System.Windows.Forms.AnchorStyles.Left) _
253
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
254
Me.picView.Location
=
New System.Drawing.Point(
16
,
16
)
255
Me.picView.Name
=
"
picView
"
256
Me.picView.Size
=
New System.Drawing.Size(
640
,
480
)
257
Me.picView.TabIndex
=
0
258
Me.picView.TabStop
=
False
259
'
260
'
Button1
261
'
262
Me.Button1.Anchor
=
CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
263
Me.Button1.Location
=
New System.Drawing.Point(
244
,
540
)
264
Me.Button1.Name
=
"
Button1
"
265
Me.Button1.Size
=
New System.Drawing.Size(
56
,
24
)
266
Me.Button1.TabIndex
=
1
267
Me.Button1.Text
=
"
start
"
268
'
269
'
Button2
270
'
271
Me.Button2.Anchor
=
CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
272
Me.Button2.Location
=
New System.Drawing.Point(
304
,
540
)
273
Me.Button2.Name
=
"
Button2
"
274
Me.Button2.Size
=
New System.Drawing.Size(
56
,
24
)
275
Me.Button2.TabIndex
=
1
276
Me.Button2.Text
=
"
stop
"
277
'
278
'
Button3
279
'
280
Me.Button3.Anchor
=
CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
281
Me.Button3.Location
=
New System.Drawing.Point(
380
,
540
)
282
Me.Button3.Name
=
"
Button3
"
283
Me.Button3.Size
=
New System.Drawing.Size(
56
,
24
)
284
Me.Button3.TabIndex
=
1
285
Me.Button3.Text
=
"
cap
"
286
'
287
'
Button4
288
'
289
Me.Button4.Anchor
=
CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
290
Me.Button4.Location
=
New System.Drawing.Point(
352
,
508
)
291
Me.Button4.Name
=
"
Button4
"
292
Me.Button4.Size
=
New System.Drawing.Size(
124
,
24
)
293
Me.Button4.TabIndex
=
1
294
Me.Button4.Text
=
"
色彩校正
"
295
'
296
'
Button5
297
'
298
Me.Button5.Anchor
=
CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
299
Me.Button5.Location
=
New System.Drawing.Point(
164
,
512
)
300
Me.Button5.Name
=
"
Button5
"
301
Me.Button5.Size
=
New System.Drawing.Size(
124
,
24
)
302
Me.Button5.TabIndex
=
1
303
Me.Button5.Text
=
"
画面大小
"
304
'
305
'
Form1
306
'
307
Me.AutoScaleBaseSize
=
New System.Drawing.Size(
6
,
14
)
308
Me.ClientSize
=
New System.Drawing.Size(
792
,
573
)
309
Me.Controls.Add(Me.Button1)
310
Me.Controls.Add(Me.picView)
311
Me.Controls.Add(Me.Button2)
312
Me.Controls.Add(Me.Button3)
313
Me.Controls.Add(Me.Button4)
314
Me.Controls.Add(Me.Button5)
315
Me.Name
=
"
Form1
"
316
Me.Text
=
"
Form1
"
317
Me.ResumeLayout(False)
318
319
End Sub
320
321
#End Region
322
323
Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
324
cccap
=
New Cam(picView.Handle,
0
,
0
, picView.Width, picView.Height)
325
End Sub
326
327
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
328
cccap.Start()
329
End Sub
330
331
Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
332
cccap.Stop()
333
End Sub
334
335
Private Sub Button3_Click()Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
336
cccap.GrabImage(
"
c:/test.bmp
"
)
337
End Sub
338
339
Private Sub Form1_Closed()Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
340
cccap
=
Nothing
341
End Sub
342
343
Private Sub Button4_Click()Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
344
cccap.capDlgVideoSource()
345
End Sub
346
347
Private Sub Button5_Click()Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
348
cccap.capDlgVideoFormat()
349
End Sub
350
End Class
351
c# 调用摄像头抓图(转)
最新推荐文章于 2025-06-23 16:13:54 发布