VB程序员博客

VB程序开发

我拼凑了一个摄像头照相代码,但照相并给IMAGE1(在“员工信息”窗体中)赋值后,用DISCONNECT 截断与摄像头的联接后,原摄像头处显示黑屏,怎么也去不掉,因为在其他窗体中,不能用UNLOAD ME ,怎么办?

以下为模块:

Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
  Alias "capCreateCaptureWindowA" ( _
  ByVal lpszWindowName As String, _
  ByVal dwStyle As Long, _
  ByVal x As Long, _
  ByVal y As Long, _
  ByVal nWidth As Long, _
  ByVal nHeight As Long, _
  ByVal hWndParent As Long, _
  ByVal nID As Long) As Long

Private Const WS_CHILD = &H40000000
Private Const WS_VISIBLE = &H10000000
Private Const WM_USER = &H400
Private Const WM_CAP_START = &H400
Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

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

Private Preview_Handle As Long

Public Function CreateCaptureWindow( _
  hWndParent As Long, _
  Optional x As Long = 750, _
  Optional y As Long = 120, _
  Optional nWidth As Long = 320, _
  Optional nHeight As Long = 240, _
  Optional ncameraid As Long = 0) As Long

  Preview_Handle = capCreateCaptureWindow("Video", _
    WS_CHILD + WS_VISIBLE, x, y, _
    nWidth, nHeight, hWndParent, 1)

  SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, ncameraid, 0
  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

  CreateCaptureWindow = Preview_Handle
End Function

Public Function CapturePicture(ncapturehandle As Long) As StdPicture
  Clipboard.Clear
  SendMessage ncapturehandle, WM_CAP_EDIT_COPY, 0, 0
  Set CapturePicture = Clipboard.GetData
End Function

Public Sub Disconnect(ncapturehandle As Long, _
  Optional ncameraid = 0)

  SendMessage ncapturehandle, WM_CAP_DRIVER_DISCONNECT, _
    ncameraid, 0
   
 
End Sub

以下为command1 command2 的命令代码,“员工信息”为IMAGE1所在的窗体名称

Private Sub command1_click()
  On Error Resume Next
If Dir$("e:摄像头文件\kkk.jpg") = "" Then
MsgBox "请先采集照片!"
Command1.SetFocus
Else
Set Image1.Picture = LoadPicture("e:摄像头文件\kkk.jpg")
Kill "e:摄像头文件\kkk.jpg"
Command2.Enabled = True
Clipboard.Clear
Disconnect (Video_Handle)
Video_Handle = 0
End If
End Sub

Private Sub command2_click()
  On Error Resume Next
Video_Handle = CreateCaptureWindow(员工信息.hwnd)

If Dir$("e:摄像头文件\kkk.jpg") = "" Then
    Dim x As StdPicture
    Set x = CapturePicture(Video_Handle)
    SavePicture x, "e:摄像头文件\kkk.jpg"
    Command2.Enabled = False
    Else
    Command1.SetFocus
    End If
   
End Sub

command1  command2  的代码有一些修改,但仍不能解决问题,以下为修改后的代码:

Private Sub command1_click()
  On Error Resume Next
    Dim x As StdPicture
    Set x = CapturePicture(Video_Handle)
    Set Image1.Picture = x
Disconnect (Video_Handle)
Video_Handle = 0
Command2.Enabled = True
Command1.Enabled = False
Clipboard.Clear
End Sub

Private Sub command2_click()
  On Error Resume Next
Video_Handle = CreateCaptureWindow(员工信息.hwnd)
Command2.Enabled = False
Command1.Enabled = True
End Sub

解决了,我新建了一个窗口,专门用来照相。


标签: , , ,