;Question to Mr. https://www.autoitscript.com/forum/ #include #include #include #NoTrayIcon OnAutoItExitRegister("_EXITLib") Opt("GUIONEVENTMODE", True) Global $avi, $user, $Startcap, $startwebcam, $RecordSt, $Dirsv, $lastGetport, $listeDrivercapt, $Sav, $i, $Exittest __Webcamini() _GDIPlus_Startup() $Dirsv = @DesktopDir & "\Webcam_Snap" ; Thu muc luu tru If Not FileExists($Dirsv) Then DirCreate($Dirsv) $Dirsv = FileGetShortName($Dirsv) Global $Form1 = GUICreate("Webcam Snap", 400, 400) GUISetOnEvent(-3, "_EXIT") Global $Combo1 = GUICtrlCreateCombo("", 9, 8, 300, 25, 2097155) Global $Button1 = GUICtrlCreateButton("ON", 315, 6, 82, 25) GUICtrlSetOnEvent($Button1, "__Start") Global $Checkbox1 = GUICtrlCreateCheckbox("Capture", 315, 47, 72, 17) GUICtrlCreateGraphic(9, 64, 300, 300) GUICtrlSetColor(-1, 0x000000) Global $Inputtext = GUICtrlCreateInput("Text on picture!", 9, 35, 300, 21) GUISetState() $listeDrivercapt = __GetlisteDrivercapt() _Cheklist() While 1 If $Startcap Then __Snapwebcam() EndIf WEnd Func __Snapwebcam() If BitAND(GUICtrlRead($Checkbox1), 1) Then $i += 1 $Sav = $Dirsv & "\" & GUICtrlRead($Inputtext) & "_" & $i & ".bmp" Else $Sav = "" EndIf __SenTobmpclip($startwebcam) __SaveImage("", 300, 300, GUICtrlRead($Inputtext) & @CRLF & "Good time " & @CRLF & _Date(), 0, 0, 10, $Sav, $Form1) EndFunc Func _Date() Return "[" & @MDAY & "\" & @MON & "\" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC & "]" EndFunc Func __Start() $Exittest = 1 If $Startcap Then If $RecordSt Then _WebcamRecordStop($startwebcam) $RecordSt = 0 GUICtrlSetState($Checkbox2, 4) EndIf __Stopwebcam($startwebcam) $Startcap = 0 GUICtrlSetData($Button1, "Start") Else Local $Getport = _Cheklist(GUICtrlRead($Combo1)) If @error Then Return If $startwebcam <> "" And $Getport = $lastGetport Then __Starttwebcam($startwebcam) Else $startwebcam = __startwebcam($Getport) If @error Then $startwebcam = "" MsgBox(16, "Error", "Failed to connect to device.", 0, $Form1) Return EndIf $lastGetport = $Getport EndIf $Startcap = 1 GUICtrlSetData($Button1, "OFF") EndIf EndFunc Func _Cheklist($op = "") For $i = 1 To $listeDrivercapt[0] If $op = "" Then GUICtrlSetData($Combo1, $listeDrivercapt[$i], $listeDrivercapt[1]) Else If $op = $listeDrivercapt[$i] Then Return $i - 1 EndIf EndIf Next Return SetError(2) EndFunc Func __RecordSt() If $Startcap = 0 Then MsgBox(16, "Error", "Click Start, prior to using video recording.", 0, $Form1) GUICtrlSetState($Checkbox2, 4) Return EndIf If $RecordSt Then _WebcamRecordStop($startwebcam) $RecordSt = 0 Else Local $sPath = FileSaveDialog('Save to...', @WorkingDir, 'Record (*.Avi)', 24, '', $Form1) If @error Then Return WinSetTitle($Form1, '', 'Wait..') _WebcamRecordStart($startwebcam, $sPath) MsgBox(0, '', 'Start webcam video recording...' & @CRLF & 'Click "OK"', 0, $Form1) $RecordSt = 1 EndIf EndFunc Func _EXIT() Exit EndFunc Func _EXITLib() If $RecordSt Then _WebcamRecordStop($startwebcam) If $Startcap Then __Stopwebcam($startwebcam) __Disconwebcam($startwebcam) __Dllclose() _GDIPlus_Shutdown() EndFunc Func __startwebcam($Port = 0) Local Const $WS_CHILD = 0x40000000 Local Const $WM_CAP_DRIVER_CONNECT = 1024 + 10 Local $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", $WS_CHILD, "int", 0, _ "int", 0, "int", 0, "int", 0, "hwnd", GUICreate("", 0, 0), "int", 1) $cap = $cap[0] Local $wb_connect $wb_connect = DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_CONNECT, "int", $Port, "int", 0) ;;Here If $wb_connect[0] = 0 Then For $p = 1 To 20 ;?????????????????????????????????????????? $wb_connect = DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_CONNECT, "int", $Port, "int", 0) If $wb_connect[0] = 1 Then ExitLoop If $p = 20 Then WinKill(HWnd($cap)) ; Error connecting to device close window Return SetError(1) EndIf Next EndIf GUISetState(@SW_DISABLE) Return $cap EndFunc Func __SenTobmpclip($cap) Local Const $WM_CAP_GRAB_FRAME_NOSTOP = 1024 + 61 Local Const $WM_CAP_EDIT_COPY = 1024 + 30 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_EDIT_COPY, "int", 0, "int", 0) EndFunc Func __SenToFile($cap, $savto) Local Const $WM_CAP_GRAB_FRAME_NOSTOP = 1024 + 61 Local Const $WM_CAP_FILE_SAVEDIBA = 1024 + 25 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $savto) EndFunc Func _WebcamRecordStart($cap, $savto) Local Const $WM_CAP_FILE_SET_CAPTURE_FILEA = 1024 + 20 Local Const $WM_CAP_SEQUENCE = 1024 + 62 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $savto) DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0) EndFunc Func _WebcamRecordStop($cap) Local Const $WM_CAP_STOPRC = 1024 + 68 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STOPRC, "int", 0, "int", 0) EndFunc Func __Starttwebcam($cap) Local Const $WM_CAP_STArt = 1024 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STArt, "int", 1, "int", 0) ;DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STArt, "int", 0, "int", 0) EndFunc Func __Stopwebcam($cap) Local Const $WM_CAP_STOP = 1024 + 181 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STOP, "int", 0, "int", 0) EndFunc Func __Disconwebcam($cap) Local Const $WM_CAP_DRIVER_DISCONNECT = 1024 + 11 DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0) WinKill(HWnd($cap)) EndFunc Func __Webcamini() $avi = DllOpen("avicap32.dll") $user = DllOpen("user32.dll") EndFunc Func __Dllclose() DllClose($user) DllClose($avi) EndFunc Func __GetlisteDrivercapt() Local $StructName = DllStructCreate("char[1024]") Local $aRslt, $Arrymdriv[1] For $i = 0 To 99 $aRslt = DllCall($avi, "bool", "capGetDriverDescriptionA", "dword", $i, "ptr", DllStructGetPtr($StructName), "dword", DllStructGetSize($StructName), "int", 0, "int", 0) If @error Then Return SetError(1) If $aRslt[0] Then ReDim $Arrymdriv[$i + 2] $Arrymdriv[0] = $i + 1 $Arrymdriv[$i + 1] = DllStructGetData($StructName, 1) Else ExitLoop EndIf Next $StructName = 0 Return $Arrymdriv EndFunc Func __SaveImage($sFilePath, $Width, $Height, $stext, $_X, $_Y, $_Z, $sFile, $hGUI) Local $Result, $ret, $sCLSID, $hImage, $hBitmap = 0 If $sFilePath = "" Then _ClipBoard_Open(0) $hBitmap = _ClipBoard_GetDataEx($CF_BITMAP) _ClipBoard_Close() If Not $hBitmap Then Return 0 EndIf $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap) _WinAPI_DeleteObject($hBitmap) Else $hImage = _GDIPlus_ImageLoadFromFile($sFilePath) EndIf $hImage = _GDIPlus_ScaleImage($hImage, $Width, $Height) If $stext <> "" Then Local $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) Local $hFamily = _GDIPlus_FontFamilyCreate("Arial") Local $hFont = _GDIPlus_FontCreate($hFamily, $_Z, 1) Local $hFormat = _GDIPlus_StringFormatCreate(0x4000) Local $hBrush2 = _GDIPlus_BrushCreateSolid(0xffffffff) Local $hPen = _GDIPlus_PenCreate(0xC4000000, 1) Local $tLayout = _GDIPlus_RectFCreate($_X, $_Y, $Width, $Height) Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $stext, $hFont, $tLayout, $hFormat) _GDIPlus_GraphicsDrawStringEx($hGraphic, $stext, $hFont, $aInfo[0], $hFormat, $hBrush2) _GDIPlus_PenDispose($hPen) _GDIPlus_BrushDispose($hBrush2) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_GraphicsDispose($hGraphic) EndIf If $hGUI <> "" Then $hGraphicGui = _GDIPlus_GraphicsCreateFromHWND($hGUI) _GDIPlus_GraphicsDrawImage($hGraphicGui, $hImage, 0, 0) _GDIPlus_GraphicsDispose($hGraphicGui) EndIf If $sFile <> "" Then $sCLSID = _GDIPlus_EncodersGetCLSID("JPG") $Result = _GDIPlus_ImageSaveToFileEx($hImage, $sFile, $sCLSID, 0) EndIf _GDIPlus_ImageDispose($hImage) ClipPut('') Return $Result EndFunc ;==>__SaveImage Func _GDIPlus_ScaleImage($hImage, $iW, $iH) ;coded by UEZ 2012 Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromScan0", "int", $iW, "int", $iH, "int", 0, "int", 0x0026200A, "ptr", 0, "int*", 0) $hBitmap = $hBitmap[6] Local $hBmpCtxt = _GDIPlus_ImageGetGraphicsContext($hBitmap) _GDIPlus_GraphicsDrawImageRect($hBmpCtxt, $hImage, 0, 0, $iW, $iH) _GDIPlus_ImageDispose($hImage) _GDIPlus_GraphicsDispose($hBmpCtxt) Return $hBitmap EndFunc