Imports System.Runtime.InteropServices
Imports System.IO
Public Class Form1
'定义常量
Private Const WS_Child As Integer = &H40000000
Private Const WS_Visible As Integer = &H10000000
Private Const WS_Text As Integer = &HC00000
Private Const WS_ThickFrame As Integer = &H40000
Private Const WM_User As Integer = &H400
Private Const WM_CAP_Connect As Integer = WM_User + 10
Private Const WM_CAP_DisConnect As Integer = WM_User + 11
Private Const WM_CAP_Set_PreView As Integer = WM_User + 50
Private Const WM_CAP_Set_Overlay As Integer = WM_User + 51
Private Const WM_CAP_Set_PreViewRate As Integer = WM_User + 52
Private Const WM_CAP_Edit_Copy As Integer = WM_User + 30
Private Const WM_CAP_Sequence As Integer = WM_User + 62
Private Const WM_Cap_File_Set_File As Integer = WM_User + 20
Private Const WM_Cap_File_Get_File As Integer = WM_User + 21
Private Const WM_CAP_SINGLE_FRAME_OPEN As Integer = &H4000 + 60
Private Const WM_CAP_SINGLE_FRAME_CLOSE As Integer = &H4000 + 61
Private Const WM_CAP_SINGLE_FRAME As Integer = &H4000 + 70
'声明外部函数
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
<DllImport("avicap32.dll", CharSet:=CharSet.Auto)>
Private Shared Function capCreateCaptureWindow(ByVal lpszWindowName As String, 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
End Function
Private ctCapWin As IntPtr
Private ctAviPath As String
Private ctPicPath As String
Private ctConnect As Boolean
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Me.Text = "摄像头控制"
Button1.Text = "连接"
Button2.Text = "断开"
Button3.Text = "截图"
Button4.Text = "录像"
'读出用户设置
Call ReadSaveSet()
KjEnabled(True)
End Sub
Private Sub ReadSaveSet(Optional ByVal IsSave As Boolean = False)
'保存或读出用户设置的图片和视频默认保存目录
Dim nKey As String = "摄像头控制程序"
Dim nSub As String = "UserOpt"
If IsSave Then
SaveSetting(nKey, nSub, "AviPath", ctAviPath)
SaveSetting(nKey, nSub, "PicPath", ctPicPath)
Else
ctAviPath = GetSetting(nKey, nSub, "AviPath", "")
ctPicPath = GetSetting(nKey, nSub, "PicPath", "")
End If
End Sub
Private Sub Form_Unload(ByVal Cancel As Integer)
Call ReadSaveSet(True) '保存用户设置
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建视频窗口和连接摄像头
Dim nStyle As Integer
Dim T As Integer
If ctCapWin = IntPtr.Zero Then '创建一个视频窗口,大小:640*480
T = (Button1.Top + Button1.Height * 1.1) '视频窗口垂直位置:像素
'nStyle = WS_Child + WS_Visible + WS_Text + WS_ThickFrame '窗口(在 Form1 内)+可见+标题栏+边框
nStyle = WS_Child + WS_Visible '视频窗口无标题栏和边框
'nStyle = WS_Visible '视频窗口为独立窗口,关闭主窗口视频窗口也会自动关闭
ctCapWin = capCreateCaptureWindow("我创建的视频窗口", nStyle, 0, T, 640, 480, Me.Handle, 0)
End If
'视频窗口连接到摄像头,如无后面两条语句视频窗口画面不会变化
SendMessage(ctCapWin, WM_CAP_Connect, IntPtr.Zero, IntPtr.Zero) '连接摄像头
SendMessage(ctCapWin, WM_CAP_Set_PreView, 1, IntPtr.Zero) '第三个参数:1 - 预览模式有效, 0 - 预览模式无效
SendMessage(ctCapWin, WM_CAP_Set_PreViewRate, 30, IntPtr.Zero) '第三个参数:设置预览显示频率为每秒 30 帧
ctConnect = True
KjEnabled(True)
'请检检查摄像头连接,并确定没有其他用户和程序使用。"
End Sub
…
…
…
🎉精彩不容错过!新文章震撼登场,却因代码不全让人意犹未尽?别担心!我们为你准备了软件截图,让你先睹为快,感受其强大魅力。
✨如果你渴望解锁全部精彩内容,那么强烈建议你下载完整源代码。它将为你打开一扇通往编程新世界的大门,让你深入探索其中的奥秘,挖掘无限潜力。
🔗源代码下载网址:[https://download.csdn.net/download/qq_32257509/89603383]。赶紧行动起来,下载完整源代码,开启你的编程之旅吧!