Rubrik: Multimedia & Sound · Video | VB-Versionen: VB5, VB6 | 25.03.03 |
Videoaufnahme einer WebCam auf der Form anzeigen Dieser Tipp zeigt, wie sich LIVE-Bilder ein WebCam in einer VB-PictureBox anzeigen und speichern lassen. | ||
Autor: Arno Reh | Bewertung: | Views: 57.608 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Haben Sie auch eine WebCam daheim und würden gerne das, was sie sieht, in ihr Programm einbauen? Kein Problem: Mit ein paar API-Funktionen geht das. Man füge folgenden Code in ein Modul ein:
Option Explicit ' benötigte Deklarationen 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 = 0, _ Optional y As Long = 0, _ 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 Sub CapturePicture(nCaptureHandle As Long, _ picCapture As PictureBox) Clipboard.Clear SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0 picCapture.Picture = Clipboard.GetData End Sub
Public Sub Disconnect(nCaptureHandle As Long, _ Optional nCameraID = 0) SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _ nCameraID, 0 End Sub
Noch ein paar Erläuterungen zum Code:
In der Funktion "CreateCaptureWindow" bestimmen die Parameter nWidth und nHeight die Auflösung des Kamerabildes. Die Funktion gibt das "Video-Handle" zurück, welches für alles weitere benötigt wird. Mit "CapturePicture" wird das aktuelle Bild in die Zwischenablage kopiert und in eine Picturebox eingefügt. Und noch was: Wenn Sie mehr als nur eine Kamera am PC hängen haben, können Sie mit dem Parameter nCameraID die Nummer der Kamera angeben. Wenn Sie nur eine am Rechner hängen haben, müssen Sie dort eigentlich immer 0 angeben.
Beispiel für einen Test:
Starten Sie ein neues Projekt und setzen die Höhe der Form auf 4600 Twips. Nun fügen Sie einen CommandButton am untere Ende der Form ein. Den nennen Sie "cmdSave". Ziehen Sie noch eine PictureBox auf die Programmoberfläche. Deren Height-Eigenschaft setzen Sie auf 3600 und die Width-Eigenschaft auf 4800.
Fügen Sie den obigen Code in ein Modul ein. Dann öffnen Sie per Doppelklick auf die Form das Code-Fenster. Dort fügen Sie folgenden Code ein:
Option Explicit Dim Video_Handle as Long
Private Sub Form_Load() Picture1.Visible = False Video_Handle = CreateCaptureWindow(Me.hwnd) End Sub
Private Sub cmdSave_Click() CapturePicture Video_Handle, Picture1 SavePicture Picture1.Picture, "C:\Test.bmp" End Sub
Starten Sie das Programm. Wenn alles glatt läuft, müssten Sie Das Live-Bild der Video-Kamera auf der Form sehen und per Klick auf "Command1" das Bild unter "C:\Test.bmp" abspeichern können.