Jump to content

Take webcam screenshot


Recommended Posts

Is there any way to take screenshot from webcam without any manual interaction? Well, ofc I found the following topic:

'?do=embed' frameborder='0' data-embedContent>>

Here's the simple script I wrote:

#include "Webcam.au3"
#include <WindowsConstants.au3>
#include <GUIConstants.au3>

$gui = GUICreate("Webcam", 640, 480, 100, 100)
$ope = _WebcamOpen($gui, 0, 0, 640, 480, 0) ;PORT GOES HERE! 0-3
GUISetState(@SW_SHOW)

Sleep(5000)
_WebcamClose($ope)

After starting the following screen appears:

qehan.png

After applying and OK (since this is the only one cam I have) the following screen comes:

3PAs.png

I tried to change ports, but still nothing. Tried to compile & Run as admin, but the same comes...

Any idea what could cause this and how to solve?

Thanks!

Link to comment
Share on other sites

This normally works for me to view the webcam, i think Luducos was the origanal author, i use this to test whether a webcam works or not after a format, maybe you can adapt it

Look at the notes on WebCamOpen about the black screen error

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=Webcam Test.exe
#AutoIt3Wrapper_UseUpx=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include-once

$WM_CAP_START = 0x400
$WM_CAP_UNICODE_START = $WM_CAP_START + 100
$WM_CAP_PAL_SAVEA = $WM_CAP_START + 81
$WM_CAP_PAL_SAVEW = $WM_CAP_UNICODE_START + 81
$WM_CAP_UNICODE_END = $WM_CAP_PAL_SAVEW
$WM_CAP_ABORT = $WM_CAP_START + 69
$WM_CAP_DLG_VIDEOCOMPRESSION = $WM_CAP_START + 46
$WM_CAP_DLG_VIDEODISPLAY = $WM_CAP_START + 43
$WM_CAP_DLG_VIDEOFORMAT = $WM_CAP_START + 41
$WM_CAP_DLG_VIDEOSOURCE = $WM_CAP_START + 42
$WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
$WM_CAP_DRIVER_DISCONNECT = $WM_CAP_START + 11
$WM_CAP_DRIVER_GET_CAPS = $WM_CAP_START + 14
$WM_CAP_DRIVER_GET_NAMEA = $WM_CAP_START + 12
$WM_CAP_DRIVER_GET_NAMEW = $WM_CAP_UNICODE_START + 12
$WM_CAP_DRIVER_GET_VERSIONA = $WM_CAP_START + 13
$WM_CAP_DRIVER_GET_VERSIONW = $WM_CAP_UNICODE_START + 13
$WM_CAP_EDIT_COPY = $WM_CAP_START + 30
$WM_CAP_END = $WM_CAP_UNICODE_END
$WM_CAP_FILE_ALLOCATE = $WM_CAP_START + 22
$WM_CAP_FILE_GET_CAPTURE_FILEA = $WM_CAP_START + 21
$WM_CAP_FILE_GET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 21
$WM_CAP_FILE_SAVEASA = $WM_CAP_START + 23
$WM_CAP_FILE_SAVEASW = $WM_CAP_UNICODE_START + 23
$WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
$WM_CAP_FILE_SAVEDIBW = $WM_CAP_UNICODE_START + 25
$WM_CAP_FILE_SET_CAPTURE_FILEA = $WM_CAP_START + 20
$WM_CAP_FILE_SET_CAPTURE_FILEW = $WM_CAP_UNICODE_START + 20
$WM_CAP_FILE_SET_INFOCHUNK = $WM_CAP_START + 24
$WM_CAP_GET_AUDIOFORMAT = $WM_CAP_START + 36
$WM_CAP_GET_CAPSTREAMPTR = $WM_CAP_START + 1
$WM_CAP_GET_MCI_DEVICEA = $WM_CAP_START + 67
$WM_CAP_GET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 67
$WM_CAP_GET_SEQUENCE_SETUP = $WM_CAP_START + 65
$WM_CAP_GET_STATUS = $WM_CAP_START + 54
$WM_CAP_GET_USER_DATA = $WM_CAP_START + 8
$WM_CAP_GET_VIDEOFORMAT = $WM_CAP_START + 44
$WM_CAP_GRAB_FRAME = $WM_CAP_START + 60
$WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
$WM_CAP_PAL_AUTOCREATE = $WM_CAP_START + 83
$WM_CAP_PAL_MANUALCREATE = $WM_CAP_START + 84
$WM_CAP_PAL_OPENA = $WM_CAP_START + 80
$WM_CAP_PAL_OPENW = $WM_CAP_UNICODE_START + 80
$WM_CAP_PAL_PASTE = $WM_CAP_START + 82
$WM_CAP_SEQUENCE = $WM_CAP_START + 62
$WM_CAP_SEQUENCE_NOFILE = $WM_CAP_START + 63
$WM_CAP_SET_AUDIOFORMAT = $WM_CAP_START + 35
$WM_CAP_SET_CALLBACK_CAPCONTROL = $WM_CAP_START + 85
$WM_CAP_SET_CALLBACK_ERRORA = $WM_CAP_START + 2
$WM_CAP_SET_CALLBACK_ERRORW = $WM_CAP_UNICODE_START + 2
$WM_CAP_SET_CALLBACK_FRAME = $WM_CAP_START + 5
$WM_CAP_SET_CALLBACK_STATUSA = $WM_CAP_START + 3
$WM_CAP_SET_CALLBACK_STATUSW = $WM_CAP_UNICODE_START + 3
$WM_CAP_SET_CALLBACK_VIDEOSTREAM = $WM_CAP_START + 6
$WM_CAP_SET_CALLBACK_WAVESTREAM = $WM_CAP_START + 7
$WM_CAP_SET_CALLBACK_YIELD = $WM_CAP_START + 4
$WM_CAP_SET_MCI_DEVICEA = $WM_CAP_START + 66
$WM_CAP_SET_MCI_DEVICEW = $WM_CAP_UNICODE_START + 66
$WM_CAP_SET_OVERLAY = $WM_CAP_START + 51
$WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
$WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
$WM_CAP_SET_SCALE = $WM_CAP_START + 53
$WM_CAP_SET_SCROLL = $WM_CAP_START + 55
$WM_CAP_SET_SEQUENCE_SETUP = $WM_CAP_START + 64
$WM_CAP_SET_USER_DATA = $WM_CAP_START + 9
$WM_CAP_SET_VIDEOFORMAT = $WM_CAP_START + 45
$WM_CAP_SINGLE_FRAME = $WM_CAP_START + 72
$WM_CAP_SINGLE_FRAME_CLOSE = $WM_CAP_START + 71
$WM_CAP_SINGLE_FRAME_OPEN = $WM_CAP_START + 70
$WM_CAP_STOP = $WM_CAP_START + 68
#include <GUIConstants.au3>
$avi = DllOpen("avicap32.dll")
$user = DllOpen("user32.dll")
;~ #include <Webcam.au3>
#include <WindowsConstants.au3>

$gui = GUICreate("Test", 322, 242)
$camera = _WebcamOpen($gui, 1, 1, 320, 240)
GUISetState(@SW_SHOW)
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then
        _WebcamClose($camera)
        DllClose($avi)
        DllClose($user)
        Exit
    EndIf
    Sleep(100)
WEnd

;===============================================================================
;
; Description:      Open's a webcam preview screen in your gui
; Syntax:           _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight, $sPort=0)
; 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
;                   $sPort     - Specify wich webcam port you want to use (for multiple webcams)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns id needed for other controls
;                   On Failure - Returns -1
; Author(s):        Ludocus
; Note(s):          If a black/blank screen shows up, try using different $sPort's (0 to 3)
;
;===============================================================================
Func _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight, $sPort = 0)
    $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD, $WS_VISIBLE), "int", $sLeft, "int", $sTop, "int", $sWidth, "int", $sHeight, "hwnd", $sHwnd, "int", 1)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", $sPort, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_SET_PREVIEWRATE, "int", 1, "int", 0)
    If @error Then Return -1
    Return $cap[0]
EndFunc   ;==>_WebcamOpen

;===============================================================================
;
; 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_SET_CALLBACK_FRAME, "int", 0, "int", 0)
    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   ;==>_WebcamSnap

;===============================================================================
;
; 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_END, "int", 0, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0)
    DllClose($user)
    If @error Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>_WebcamClose

;===============================================================================
;
; Description:      Starts recording the webcam to a file
; Syntax:           _WebcamRecordStart($sFile, $sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
;                   $sFile     - File to save the movie to (*.avi)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          Stop recording by: _WebcamRecordStop($Id)
;
;===============================================================================
Func _WebcamRecordStart($sFile, $sId)
    DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $sFile)
    DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0)
    If @error Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>_WebcamRecordStart

;===============================================================================
;
; Description:      Stops recording.
; Syntax:           _WebcamRecordStop($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 _WebcamRecordStop($sId)
    DllCall($user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_STOP, "int", 0, "int", 0)
    If @error Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc   ;==>_WebcamRecordStop
Link to comment
Share on other sites

As I wrote upper, I tried all the 4 ports. The UDF is all the same, just with a different format. I tried - Still not working.

The interesting thing is that it works sometimes. I'll find what makes it work, but I've got no idea :/

Maybe is there a service in windows which one I accidently turned off? (I used some tune-up softwares)

Edited by Unc3nZureD
Link to comment
Share on other sites

Well, I can't really understand... Probably my fault :)

Let me give you a small overview:

Skype off:

- Everything works perfectly, except the webcam UDF. It gives thoose window which I linked at the 1st post

Skype on:

- Still everything works, including the webcam UDF. The new window shows up and it displays my cam.

Link to comment
Share on other sites

Quite interesting... I tried the 1st one (I had already had a look at them, but all 3 looked all the same, but with different looking) and it worked. After that just for test I started my "original", and it didn't work. Hmmm.... Let me start the good one again! And it didn't work again... lol... kinda dafuq....

Edit:

Hmm, The 2nd example seems to be exactly my one. The problem is that I haven't got that webcamdell2.exe. Well let's see if google can help :D (Anyways it seems to be only working on dell machines since webcamDELL2. Who knows...)

Edited by Unc3nZureD
Link to comment
Share on other sites

Well, this window that asks you to choose the video source appears in 2 cases for me (Win7 and Win8):

1- if another application uses the webcam while you're trying to open it.

2- sometimes if your OS is x64, you need to compile your script as x64.

qehan.png

Edited by EKY32

[font="'trebuchet ms', helvetica, sans-serif;"]Please mark the answer of your question if you found it.[/font]

Link to comment
Share on other sites

I tried to compile as x64, but still not working properly.

It does the following:

- Start 1st -> Works perfectly

- Start 2nd -> Same error

- Wait some time, or restart, or idk (couldn't experience yet) it works again.

Edited by Unc3nZureD
Link to comment
Share on other sites

I get the same thing it only ever works once, but because i only test once its never been a problem.

It works after a reboot so if i ever need to i just reboot and try again

Its very rare i get one that it doesn't work but it only works once for the rest

Link to comment
Share on other sites

I have found the solution Try this Script 
Note This problem occurs only with a built-in Webcam. 
#include <ClipBoard.au3>
#include <GDIPlus.au3>
OnAutoItExitRegister("__Dllclose")

Global $avi, $user

__Webcamini()

If FileDelete('Snap') Then DirCreate("Snap")

Local $startwebcam = __startwebcam()
If @error Then
    MsgBox(16, "Error", "Failed to connect to device!")
    Exit
EndIf

$tim = TimerInit()
For $i = 1 To 10
    __SenToFile($startwebcam, "Snap\Snap_" & $i & ".bmp")
    __SaveImage("Snap\Snap_" & $i & ".bmp", "Snap\Snap_FromBMPFile" & $i & ".jpg", 400, 300, "By Mrbenkali !", 0, 0, 30) ; save to jpg
Next
MsgBox(0, '', TimerDiff($tim))

$tim = TimerInit()
For $i = 1 To 10
    __SenTobmpclip($startwebcam)
    __SaveImage("", "Snap\Snap_FromBMPclip" & $i & ".jpg", 400, 300, "By Mrbenkali !", 0, 0, 30) ; save to jpg
Next
MsgBox(0, '', TimerDiff($tim))

__cloosewebcam($startwebcam)

__Dllclose()

Func __startwebcam()
    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", 0, "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", 0, "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   ;==>__startwebcam
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   ;==>__SenTobmpclip
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   ;==>__SenToFile
Func __cloosewebcam($cap)
    Local Const $WM_CAP_STOP = 1024 + 100
    Local Const $WM_CAP_DRIVER_DISCONNECT = 1024 + 11
    DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STOP, "int", 0, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0)
    WinKill(HWnd($cap))
EndFunc   ;==>__cloosewebcam
Func __Webcamini()
    $avi = DllOpen("avicap32.dll")
    $user = DllOpen("user32.dll")
EndFunc   ;==>__Webcamini
Func __Dllclose()
    DllClose($user)
    DllClose($avi)
EndFunc   ;==>__Dllclose
Func __SaveImage($sFilePath, $sFile, $Width, $Height, $stext, $_X, $_Y, $_Z)
    Local $Result, $ret, $sCLSID, $hImage, $hBitmap = 0
    _GDIPlus_Startup()
    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
    $ret = DllCall($ghGDIPDll, "int", "GdipGetImageThumbnail", "ptr", $hImage, "int", $Width, "int", $Height, "ptr*", 0, "ptr", 0, "ptr", 0)
    _GDIPlus_BitmapDispose($hImage)
    $hImage = $ret[4]

    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)
    Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $stext, $hFont, $tLayout, $hFormat)
    _GDIPlus_GraphicsDrawStringEx($hGraphic, $stext, $hFont, $aInfo[0], $hFormat, $hBrush2)

    $sCLSID = _GDIPlus_EncodersGetCLSID("JPG")
    $Result = _GDIPlus_ImageSaveToFileEx($hImage, $sFile, $sCLSID, 0)
    _GDIPlus_BitmapDispose($hImage)

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_StringFormatDispose($hFormat)
    _GDIPlus_FontDispose($hFont)
    _GDIPlus_FontFamilyDispose($hFamily)
    _GDIPlus_GraphicsDispose($hGraphic)

    _GDIPlus_Shutdown()
    ClipPut('')
    Return $Result
EndFunc   ;==>__SaveImage
Edited by Mrbenkali
Link to comment
Share on other sites

Here is an example 

Sanstitre2f21a.jpg

#include <ClipBoard.au3>
#include <GDIPlus.au3>

OnAutoItExitRegister("_EXITLib")
Opt("GUIONEVENTMODE", True)

Global $avi, $user, $Startcap, $startwebcam, $RecordSt, $Dirsv, $lastGetport, $listeDrivercapt, $Sav, $i, $Exittest

__Webcamini()
_GDIPlus_Startup()

$Dirsv = @ScriptDir & "\Webcam_Snap"
If Not FileExists($Dirsv) Then DirCreate($Dirsv)
$Dirsv = FileGetShortName($Dirsv)

Global $Form1 = GUICreate("Form1", 473, 376)
GUISetOnEvent(-3, "_EXIT")
Global $Combo1 = GUICtrlCreateCombo("", 89, 8, 273, 25, 2097155)
Global $Button1 = GUICtrlCreateButton("Start", 377, 6, 82, 25)
GUICtrlSetOnEvent($Button1, "__Start")
GUICtrlCreateLabel("Select camera", 14, 10, 72, 17)
Global $Checkbox1 = GUICtrlCreateCheckbox("Save Pic", 331, 37, 65, 17)
Global $Checkbox2 = GUICtrlCreateCheckbox("Record", 401, 37, 57, 17)
GUICtrlSetOnEvent($Checkbox2, "__RecordSt")
GUICtrlCreateGraphic(9, 64, 455, 304)
GUICtrlSetColor(-1, 0x000000)
Global $Inputtext = GUICtrlCreateInput("  By Celtic88 !", 14, 35, 297, 21)
GUISetState()

$listeDrivercapt = __GetlisteDrivercapt()
_Cheklist()

While 1
    If BitAND(GUICtrlRead($Checkbox1), 1) Then
        $i += 1
        $Sav = $Dirsv & "\Snap_" & $i & "_.jpg"
    Else
        $Sav = ""
    EndIf
    Send('{PRINTSCREEN}')
    __SaveImage("", 455, 304, GUICtrlRead($Inputtext) & @CRLF & "  Test", 0, 0, 20, $Sav, $Form1)
    Sleep(100)
    If $Exittest Then ExitLoop
WEnd

While 1
    If $Startcap Then
        __Snapwebcam()
    Else
        Sleep(100)
    EndIf
WEnd

Func __Snapwebcam()
    If BitAND(GUICtrlRead($Checkbox1), 1) Then
        $i += 1
        $Sav = $Dirsv & "\Snap_" & $i & "_.jpg"
    Else
        $Sav = ""
    EndIf
    ;__SenToFile($startwebcam, $Dirsv & "\Snap.bmp")
    ;__SaveImage($Dirsv & "\Snap.bmp", 455, 304, GUICtrlRead($Inputtext) & @CRLF & _Date(), 0, 0, 20, $Sav, $Form1)
    ;FileDelete($Dirsv & "\Snap.bmp")
    __SenTobmpclip($startwebcam)
    __SaveImage("", 455, 304, GUICtrlRead($Inputtext) & @CRLF & _Date(), 0, 0, 20, $Sav, $Form1)
EndFunc   ;==>__Snapwebcam
Func _Date()
    Return "  [" & @MDAY & "\" & @MON & "\" & @YEAR & "  " & @HOUR & ":" & @MIN & ":" & @SEC & "] "
EndFunc   ;==>_Date
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, "Stop")
    EndIf
EndFunc   ;==>__Start
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   ;==>_Cheklist
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   ;==>__RecordSt
Func _EXIT()
    Exit
EndFunc   ;==>_EXIT
Func _EXITLib()
    If $RecordSt Then _WebcamRecordStop($startwebcam)
    If $Startcap Then __Stopwebcam($startwebcam)
    __Disconwebcam($startwebcam)
    __Dllclose()
    _GDIPlus_Shutdown()
EndFunc   ;==>_EXITLib

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   ;==>__startwebcam
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   ;==>__SenTobmpclip
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   ;==>__SenToFile

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   ;==>_WebcamRecordStart
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   ;==>_WebcamRecordStop

Func __Starttwebcam($cap)
    Local Const $WM_CAP_STArt = 1024
    DllCall($user, "int", "SendMessage", "hWnd", $cap, "int", $WM_CAP_STArt, "int", 0, "int", 0)
EndFunc   ;==>__Starttwebcam
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   ;==>__Stopwebcam
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   ;==>__Disconwebcam
Func __Webcamini()
    $avi = DllOpen("avicap32.dll")
    $user = DllOpen("user32.dll")
EndFunc   ;==>__Webcamini
Func __Dllclose()
    DllClose($user)
    DllClose($avi)
EndFunc   ;==>__Dllclose
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   ;==>__GetlisteDrivercapt
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   ;==>_GDIPlus_ScaleImage
Edited by Mrbenkali
Link to comment
Share on other sites

  • 2 months later...

I have a built in webcam and i too get this problem.

If i let Skype to use my webcam, problem goes away forever but right now i dont. I preserve the problem to find a solution without user interaction.

Is there a small code i can run to make it work again ?

Small code without GUI and such thing that would utilize Webcam.UDF

Thank you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...