.Net里一个用于驱动摄像头的类(vb.net)

来源:互联网 发布:ui给程序员的规范图 编辑:程序博客网 时间:2024/05/20 07:15

Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports System.Drawing.Image
Public Class Class1

    Private Const WM_USER As Integer = &H400
    Private Const WS_CHILD As Integer = &H40000000
    Private Const WS_VISIBLE As Integer = &H10000000
    Private Const WM_CAP_START As Integer = WM_USER

    Private Const WM_CAP_STOP As Integer = WM_CAP_START + 68
    Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP_START + 10
    Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP_START + 11
    Private Const WM_CAP_SAVEDIB As Integer = WM_CAP_START + 25

    Private Const WM_CAP_GRAB_FRAME As Integer = WM_CAP_START + 60
    Private Const WM_CAP_SEQUENCE As Integer = WM_CAP_START + 62
    Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Integer = WM_CAP_START + 20
    Private Const WM_CAP_SEQUENCE_NOFILE As Integer = WM_CAP_START + 63

    Private Const WM_CAP_SET_OVERLAY As Integer = WM_CAP_START + 51
    Private Const WM_CAP_SET_PREVIEW As Integer = WM_CAP_START + 50
    Private Const WM_CAP_SET_CALLBACK_VIDEOSTREAM As Integer = WM_CAP_START + 6
    Private Const WM_CAP_SET_CALLBACK_ERROR As Integer = WM_CAP_START + 2

    Private Const WM_CAP_SET_CALLBACK_STATUSA As Integer = WM_CAP_START + 3
    Private Const WM_CAP_SET_CALLBACK_FRAME As Integer = WM_CAP_START + 5
    Private Const WM_CAP_SET_SCALE As Integer = WM_CAP_START + 53
    Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP_START + 52

    Private hWndC As IntPtr
    Private bStat As Boolean = False

    Private mControlPtr As IntPtr
    Private mWidth As Integer
    Private mHeight As Integer
    Private mLeft As Integer
    Private mTop As Integer


    '/// <summary>
    '/// 初始化摄像头
    '/// </summary>
    '/// <param name="handle">控件的句柄</param>
    '/// <param name="left">开始显示的左边距</param>
    '/// <param name="top">开始显示的上边距</param>
    '/// <param name="width">要显示的宽度</param>
    '/// <param name="height">要显示的长度</param>

    Public Sub Pick(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

    Public Declare Function capCreateCaptureWindowA Lib "avicap32.dll" Alias "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

    Public Declare Function capGetVideoFormat Lib "avicap32.dll" Alias "capGetVideoFormat" (ByVal hWnd As IntPtr, ByVal psVideoFormat As IntPtr, ByVal wSize As Integer) As Integer

    Public Declare Function SendMessage Lib "User32.dll" Alias "SendMessageA" _
            (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, _
               ByVal lParam As Long) As Boolean


    Public Sub Start()
        If bStat = True Then
            Exit Sub
        End If
        bStat = True
        Dim lpszName(100) As Byte

        hWndC = capCreateCaptureWindowA(lpszName, WS_VISIBLE, 10, 10, 500, 300, mControlPtr, 0)
        Me.Pick(hWndC, 10, 10, 100, 100)
        If hWndC.ToInt32 <> 0 Then
            SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0)
            SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0)
            SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0)
            SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0)
            SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0)
            SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0)
            SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0)
            SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0)

        End If
    End Sub

    '    /// <summary>
    '/// 停止显示
    ' /// </summary>
    Public Sub Stop1()
        SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 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, 0, 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, 0, hBmp.ToInt64())
        SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0)
    End Sub

    '    /// <summary>
    '/// 停止录像
    '/// </summary>
    Public Sub StopKinescope()
        SendMessage(hWndC, WM_CAP_STOP, 0, 0)
    End Sub

End Class

 
原创粉丝点击