Hallo,
ich soll als Praktikant ein Programm schreiben, welches mir Bilder von einer Webcam einliest und diese dann auswertet. Mein Sub zum Bild einlesen ist in eine Schleife eingebaut, die solange läuft bis ein bestimmtes Ereignis eintritt. Das Problem ist, dass nach einem zweiten Schleifendurchlauf (und auch bei den folgenden) das aktuelle Bild von der Webcam nicht mehr verwendet wird. Das Programm kommt somit in eine Endlosschleife...
Ich bin noch nicht all zu gut vertraut mit der Programmierung in VB. Vielleicht kann mir jemand helfen und sagen wie ich es schaffe, das ich bei jedem weiteren Schleifendurchlauf ein aktuelles Bild von der Webcam bekomme, mit dem ich weiterarbeiten kann?
Dim iDevice As Integer = 0 ' Current device ID
Dim hHwnd As Integer ' Handle to preview window
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd _
As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As _
Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As _
Integer
Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As _
Boolean
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _
(ByVal lpszWindowName As String, ByVal dwStyle As Integer, _
ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
ByVal nHeight As Short, ByVal hWndParent As Integer, _
ByVal nID As Integer) As Integer
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver _
As Short, _
ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As _
String, _
ByVal cbVer As Integer) As Boolean
Private Sub OpenPreviewWindow()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
'
'Open Preview window in picturebox
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, _
640, _
480, picCapture.Handle.ToInt32, 0)
'
'Connect to device
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
'
'Set the preview scale
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
'
'Set the preview rate in milliseconds
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
'
'Start previewing the image from the camera
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
'
'Resize window to fit in picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, _
picCapture.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
btnStart.Enabled = True
Else
'...
End If
End Sub Bei diesem Sub tritt meiner Meinung nach das Problem auf. Ich lösche zwar das Clipboard, aber beim abrufen das Bildes, erhalte ich das Alte Bild wieder und speichere nicht das aktuelle ab.
Private Sub TakePicture()
Dim data As IDataObject
Dim bmap As Image
'
Clipboard.Clear()
SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
btnStart.Enabled = False
bmap.Save("C:\Users\USERX\Überwachung\aktuellesBild.jpg")
End If
End Sub Hier ist das Sub TakePicture() in der Schleife eingebaut.
Private Sub RunApplication()
Do
Timer1.Start()
Me.Cursor = Cursors.WaitCursor
TakePicture() 'take a picture
EditPicture()
CheckPosition()
If bl_checked = False Then
System.Threading.Thread.Sleep(300)
End If
Me.Cursor = Cursors.Default
Loop Until bl_checked = True 'go on with the
' application
End Sub Vielen Dank für eure Hilfe. |