(转)连接摄像头(vb)
Imports System '/// <summary> '/// 初始化摄像头 '/// </summary> '/// <param name="handle">控件的句柄</param> '/// <param name="left">开始显示的左边距</param> '/// <param name="top">开始显示的上边距</param> '/// <param name="width">要显示的宽度</param> '/// <param name="height">要显示的长度</param> Public Sub New(ByVal handle As IntPtr,ByVal left As Integer,ByVal top As Integer,ByVal width As Integer,ByVal height As Integer) mControlPtr = handle mWidth = width mHeight = height mLeft = left mTop = top End Sub '/// <summary> '/// 开始显示图像 '/// </summary> Public Sub Start() If bStat Then Return End If bStat = True Dim lpszName(99) As Byte hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD Or WS_VISIBLE,mLeft,mTop,mWidth,mHeight,mControlPtr,0) If hWndC.ToInt32() <> 0 Then SendMessage(hWndC,WM_CAP_SET_CALLBACK_VIDEOSTREAM,0) SendMessage(hWndC,WM_CAP_SET_CALLBACK_ERROR,WM_CAP_SET_CALLBACK_STATUSA,WM_CAP_DRIVER_CONNECT,WM_CAP_SET_SCALE,1,WM_CAP_SET_PREVIEWRATE,66,WM_CAP_SET_OVERLAY,WM_CAP_SET_PREVIEW,0) End If Return End Sub '/// <summary> '/// 停止显示 '/// </summary> Public Sub [Stop]() SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0) bStat = False End Sub '/// <summary> '/// 抓图 '/// </summary> '/// <param name="path">要保存bmp文件的路径</param> Public Sub GrabImage(ByVal path As String) Dim hBmp As IntPtr = Marshal.StringToHGlobalAnsi(path) SendMessage(hWndC,WM_CAP_SAVEDIB,hBmp.ToInt64()) End Sub '/// <summary> '/// 录像 '/// </summary> '/// <param name="path">要保存avi文件的路径</param> Public Sub Kinescope(ByVal path As String) Dim hBmp As IntPtr = Marshal.StringToHGlobalAnsi(path) SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,hBmp.ToInt64()) SendMessage(hWndC,WM_CAP_SEQUENCE,0) End Sub '/// <summary> '/// 停止录像 '/// </summary> Public Sub StopKinescope() SendMessage(hWndC,WM_CAP_STOP,0) End Sub End Class下面这个是测试窗体Form1.vb: Public Class Form1 Inherits System.Windows.Forms.Form Private cccap As Cam #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写 dispose 以清理组件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents picView As System.Windows.Forms.PictureBox Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button Friend WithEvents Button5 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.picView = New System.Windows.Forms.PictureBox Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.Button5 = New System.Windows.Forms.Button Me.SuspendLayout() ' 'picView ' Me.picView.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right),System.Windows.Forms.AnchorStyles) Me.picView.Location = New System.Drawing.Point(16,16) Me.picView.Name = "picView" Me.picView.Size = New System.Drawing.Size(640,480) Me.picView.TabIndex = 0 Me.picView.TabStop = False ' 'Button1 ' Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left),System.Windows.Forms.AnchorStyles) Me.Button1.Location = New System.Drawing.Point(244,540) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(56,24) Me.Button1.TabIndex = 1 Me.Button1.Text = "start" ' 'Button2 ' Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left),System.Windows.Forms.AnchorStyles) Me.Button2.Location = New System.Drawing.Point(304,540) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(56,24) Me.Button2.TabIndex = 1 Me.Button2.Text = "stop" ' 'Button3 ' Me.Button3.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left),System.Windows.Forms.AnchorStyles) Me.Button3.Location = New System.Drawing.Point(380,540) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(56,24) Me.Button3.TabIndex = 1 Me.Button3.Text = "cap" ' 'Button4 ' Me.Button4.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left),System.Windows.Forms.AnchorStyles) Me.Button4.Location = New System.Drawing.Point(352,508) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(124,24) Me.Button4.TabIndex = 1 Me.Button4.Text = "色彩校正" ' 'Button5 ' Me.Button5.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left),System.Windows.Forms.AnchorStyles) Me.Button5.Location = New System.Drawing.Point(164,512) Me.Button5.Name = "Button5" Me.Button5.Size = New System.Drawing.Size(124,24) Me.Button5.TabIndex = 1 Me.Button5.Text = "画面大小" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(6,14) Me.ClientSize = New System.Drawing.Size(792,573) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.picView) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button5) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load cccap = New Cam(picView.Handle,picView.Width,picView.Height) End Sub Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click cccap.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click cccap.Stop() End Sub Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button3.Click cccap.GrabImage("c:/test.bmp") End Sub Private Sub Form1_Closed(ByVal sender As Object,ByVal e As System.EventArgs) Handles MyBase.Closed cccap = Nothing End Sub Private Sub Button4_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button4.Click cccap.capDlgVideoSource() End Sub Private Sub Button5_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button5.Click cccap.capDlgVideoFormat() End Sub End Class (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |