zoukankan      html  css  js  c++  java
  • 拍照

    Imports System
    Imports System.Runtime.InteropServices
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Class Camera
        Private Const WM_CAP_START = WM_USER
        Private Const WM_CAP_STOP = WM_CAP_START + 68
        Private Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10
        Private Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11
        Private Const WM_CAP_SAVEDIB = WM_CAP_START + 25
        Private Const WM_CAP_GRAB_FRAME = WM_CAP_START + 60
        Private Const WM_CAP_SEQUENCE = WM_CAP_START + 62
        Private Const WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20
        Private Const WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63
        Private Const WM_CAP_SET_OVERLAY = WM_CAP_START + 51
        Private Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50
        Private Const WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6
        Private Const WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2
        Private Const WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3
        Private Const WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5
        Private Const WM_CAP_SET_SCALE = WM_CAP_START + 53
        Private Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52
        Sub New(ByVal I As PictureBox)
            o = I
        End Sub
        Dim o As PictureBox
        Dim M_Handle As IntPtr

        Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
        Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Integer)
        Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
        Alias "capCreateCaptureWindowA" (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 Integer, ByVal nID As Integer) As Integer
        Private Const WS_CHILD = &H40000000
        Private Const WS_VISIBLE = &H10000000
        Private Const WM_USER = &H400

        Private Declare Function SendMessage Lib "user32" _
        Alias "SendMessageA" (ByVal hwnd As Integer, _
        ByVal wMsg As Integer, ByVal wParam As Integer, _
        ByVal lParam As String) As Integer


        Public Function GrabImage() As Bitmap
            'paht:要保存bmp文件的路径 

            SendMessage(M_Handle, WM_CAP_EDIT_COPY, 0, 0)
            Return Clipboard.GetImage
        End Function
        Function CreateCaptureWindow(ByVal hWndParent As PictureBox, _
            Optional ByVal x As Integer = 0, Optional ByVal y As Integer = 0, _
            Optional ByVal nWidth As Integer = 320, Optional ByVal nHeight As Integer = 240, _
            Optional ByVal nCameraID As Integer = 0) As Integer
            Dim Preview_Handle As Integer
            Preview_Handle = capCreateCaptureWindow("Video", _
            WS_CHILD + WS_VISIBLE, x, y, _
            hWndParent.Width, hWndParent.Height, hWndParent.Handle, 0)
            Dim BOOL As Boolean
            BOOL = SendMessage(Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0) 'ncameraid(视频只有一个为0,多个以此类推)
            If (BOOL = False) Then
                MsgBox("没有找到视频设备!")
            End If
            SendMessage(Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0)
            SendMessage(Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0)
            SendMessage(Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0)
            M_Handle = Preview_Handle
        End Function

        Dim blnRunning As Boolean = False
        Public Sub Disconnect()
            SendMessage(M_Handle, WM_CAP_DRIVER_DISCONNECT, 0, 0)
        End Sub
        Dim blnRecording As Boolean = False

        '录像 
        Public Sub KineScope(ByVal path As String)
            If blnRecording Then
                Return
            Else
                blnRecording = True
            End If

            'path:要保存avi文件的路径 
            Dim hBmp As IntPtr = Marshal.StringToHGlobalAnsi(path)
            SendMessage(M_Handle, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt64())
            SendMessage(M_Handle, WM_CAP_SEQUENCE, 0, 0)
        End Sub
        Public Sub StopKinescope()
            If blnRecording Then
                SendMessage(M_Handle, WM_CAP_STOP, 0, 0)
            End If
            blnRecording = False
        End Sub
    End Class

    ‘以上是视频接口类

    VB程序窗体程序如下:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            L.CreateCaptureWindow(PictureBox1)
        End Sub

    拍照可以用这个方法
            Me.PictureBox2.BackgroundImage = L.GrabImage()
    保存可以用这个方法
            PictureBox2.BackgroundImage.Save(System.IO.Directory.GetCurrentDirectory & "database p" & i & ".jpg")
    注:是这样,picturebbox1是视频显示,picturebbox2是拍取到的照片,你应该能用得上

  • 相关阅读:
    对于大规模机器学习的理解和认识
    Failed to initialize NVML: GPU access blocked by the operating system
    ubuntu 当中添加开机启动服务
    洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way(贪心)
    注意注意!
    洛谷P5092 [USACO2004OPEN]Cube Stacking 方块游戏 (带权并查集)
    loj10017. 「一本通 1.2 练习 4」传送带(三分套三分)
    POJ1475 Pushing Boxes(BFS套BFS)
    CF451E Devu and Flowers(组合数)
    POJ2311 Cutting Game(博弈论)
  • 原文地址:https://www.cnblogs.com/hhq80/p/3176358.html
Copyright © 2011-2022 走看看