#Region - TimeStamp ; 2011-08-23 16:48:25 #EndRegion - TimeStamp #include #include #include ; ============================================================================= ; Titel..........: SimpleWebcamApp.au3 ; Beschreibung...: Webcam Ein/Aus, Live/Standbild, Schnappschuß (*.bmp) ==> im @ScriptDir ; Version AutoIt.: 3.3.6.1 ; Author.........: BugFix ; ...............: Webcam Funktionen von Ludocus und pierrotm777 ; ...............: Modifiziert BugFix ; ============================================================================= Opt('GuiOnEventMode', 1) Opt('MustDeclareVars', 1) Global $avicap = DllOpen("avicap32.dll") Global $user = DllOpen("user32.dll") Global $hWnd_Camera, $idCam, $isCamera = 0, $btCamera, $btFoto, $rLive $hWnd_Camera = GUICreate('Webcam', 340, 348, -1, -1) GUISetOnEvent($GUI_EVENT_CLOSE, '_exit', $hWnd_Camera) ; == Capturing für Fenster aktivieren: $idCam = _WebcamOpen($hWnd_Camera, 10, 10, 320, 240) $btFoto = GUICtrlCreateButton('Foto', 10, 260, 48, 22) GUICtrlSetOnEvent(-1, '_Snap') GUICtrlSetState(-1, $GUI_DISABLE) $btCamera = GUICtrlCreateButton('Kamera EIN', 10, 315, 120, 25) GUICtrlSetOnEvent(-1, '_CameraOnOff') GUICtrlSetColor(-1, 0x008000) GUICtrlSetFont(-1, 10, 600) $rLive = GUICtrlCreateRadio('', 155, 322, 17) GUICtrlSetState(-1, $GUI_CHECKED) GUICtrlSetOnEvent(-1, '_PreviewOnOff') ControlFocus($hWnd_Camera, '', $rLive) GUICtrlCreateLabel('Live', 175, 319, 60) GUICtrlSetColor(-1, 0x008000) GUICtrlSetFont(-1, 10, 600) GUICtrlCreateRadio('', 230, 322, 17) GUICtrlSetOnEvent(-1, '_PreviewOnOff') GUICtrlCreateLabel('Standbild', 250, 319, 75) GUICtrlSetColor(-1, 0x008000) GUICtrlSetFont(-1, 10, 600) WinSetOnTop($hWnd_Camera, '', 1) GUISetState(@SW_SHOW, $hWnd_Camera) While True Sleep(50) WEnd Func _exit() _WebcamClose($idCam) Exit EndFunc Func _CameraOnOff() ; == Kamera (de)aktivieren - Standbild An/Aus If $isCamera = 0 Then GUICtrlSetData($btCamera, 'Kamera AUS') GUICtrlSetColor($btCamera, 0xFF0000) GUICtrlSetState($btFoto, $GUI_ENABLE) _WebcamActive($idCam) Else GUICtrlSetData($btCamera, 'Kamera EIN') GUICtrlSetColor($btCamera, 0x008000) GUICtrlSetState($btFoto, $GUI_DISABLE) _WebcamInactive($idCam) EndIf $isCamera = BitXOR($isCamera, 1) If $isCamera = 1 Then _PreviewOnOff() EndFunc Func _PreviewOnOff() ; == Livebild An/Aus If Not $isCamera Then _WebcamActive($idCam) $isCamera = 1 GUICtrlSetData($btCamera, 'Kamera AUS') GUICtrlSetColor($btCamera, 0xFF0000) GUICtrlSetState($btFoto, $GUI_ENABLE) EndIf If BitAND(GUICtrlRead($rLive), $GUI_CHECKED) Then _WebcamPreview($idCam) Else _WebcamStopPreview($idCam) EndIf EndFunc Func _Snap() Local $sFile = @YEAR & @MON & @MDAY & @HOUR & @MIN & @SEC & '.bmp', $sSave, $mb If _WebcamSnap($idCam, @ScriptDir & '\' & $sFile) = 1 Then WinSetOnTop($hWnd_Camera, '', 0) While True Do $sSave = InputBox('Schnappschuß speichern', 'Bitte Dateinamen eingeben') If $sSave = '' Then $mb = MsgBox(262180, 'Kein Dateiname vergeben', 'Eingabe Wiederholen?') Until $sSave <> '' Or $mb = 7 If $sSave = '' Or $mb = 7 Then WinSetOnTop($hWnd_Camera, '', 1) FileDelete(@ScriptDir & '\' & $sFile) Return EndIf If StringRight($sSave, 4) <> '.bmp' Then $sSave &= '.bmp' If FileExists(@ScriptDir & '\' & $sSave) Then MsgBox(262192, 'Fehler', $sSave & ' existiert bereits!', 3) Else ExitLoop EndIf WEnd FileMove(@ScriptDir & '\' & $sFile, @ScriptDir & '\' & $sSave) EndIf WinSetOnTop($hWnd_Camera, '', 1) EndFunc ; ==================== webcam - UDF von Ludocus, BugFix ========================================================================================== ;=============================================================================== ; Description: Open's a webcam preview screen in your gui ; Syntax: _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight) ; Parameter(s): $sHwnd - The handle of the gui ; $sLeft - Left coord. of the preview screen ; $sTop - Top coord. of the preview screen ; $sWidth - Width of the preview screen ; $sHeight - Height of the preview screen ; Requirement(s): A webcam ; Return Value(s): On Success - Returns id needed for other controls ; On Failure - Returns -1 ; Author(s): Ludocus ; Note(s): None ;=============================================================================== Func _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight) Local $cap = DllCall($avicap, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", $sLeft, "int", $sTop, "int", $sWidth, "int", $sHeight, "hwnd", $sHwnd, "int", 1) If @error Then Return -1 Return $cap[0] EndFunc ;=============================================================================== ; Description: Closes the preview screen created with _WebcamOpen ; Syntax: _WebcamClose($sId) ; Parameter(s): $sId - Id (returned from _WebcamOpen) ; Requirement(s): A webcam ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; Author(s): Ludocus ; Note(s): None ;=============================================================================== Func _WebcamClose($sId) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 0, "int", 0);ferme le preview DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "int", 0);ajout DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_END, "int", 0, "int", 0) DllClose($user) If @error Then Return 0 Else Return 1 EndIf EndFunc ;=============================================================================== ; Function Name....: _WebcamActive ; Description......: Connects the capture driver for specified webcam-id (default 0) to the capture window (like camera switch on) ; shows freeze image ; Parameter(s).....: $sId - Id (returned from _WebcamOpen) ; $WebCamId - specified webcam-id (default 0) ; Author(s)........: BugFix ( bugfix@autoit.de ) ;=============================================================================== Func _WebcamActive($sId, $WebCamId=0) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_CONNECT, "int", $WebCamId , "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0) EndFunc ;=============================================================================== ; Function Name....: _WebcamInactive ; Description......: Disonnects the capture driver from the capture window (like camera switch off) ; Parameter(s).....: $sId - Id (returned from _WebcamOpen) ; Author(s)........: BugFix ( bugfix@autoit.de ) ;=============================================================================== Func _WebcamInactive($sId) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0) EndFunc ;=============================================================================== ; Function Name....: _WebcamPreview ; Description......: shows live image in the capture window ; Parameter(s).....: $sId - Id (returned from _WebcamOpen) ; Requirement(s)...: Run _WebcamActive before ; Author(s)........: Ludocus ;=============================================================================== Func _WebcamPreview($sId) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEWRATE, "int", 15, "int", 0) EndFunc ;=============================================================================== ; Function Name....: _WebcamStopPreview ; Description......: stops the live image in the capture window ; Parameter(s).....: $sId - Id (returned from _WebcamOpen) ; Author(s)........: Ludocus ;=============================================================================== Func _WebcamStopPreview($sId) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 0, "int", 0) EndFunc ;=============================================================================== ; Description: Creates a Snapshot from a webcam ; Syntax: _WebcamSnap($sId, $sFile) ; Parameter(s): $sId - Id (returned from _WebcamOpen) ; $sFile - File to save the snapshot to (*.bmp) ; Requirement(s): A webcam ; Return Value(s): On Success - Returns 1 ; On Failure - Returns 0 ; Author(s): Ludocus ; Note(s): None ;=============================================================================== Func _WebcamSnap($sId, $sFile) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $sFile) If @error Then Return 0 Else Return 1 EndIf EndFunc