jvds

Playing with webcam, brightness and gama

3 posts in this topic

 was playing with my webcam using mylise's script from https://www.autoitscript.com/forum/topic/150536-simple-webcam-access-using-escapi/ .

I'm trying to set the contrast and brightness with the new escapi.dll, found here http://sol.gfxile.net/zip/escapi3.zip but fail to understand how to implement it, i tried to call the dll with VideoProcAmp_Gamma and CAPTURE_GAMMA but it seems I'm doing it wrong, dllcall keeps returning the default brightness value as if i didn't call the Gama parameters, of which i don't even know if im doing it the correct way.

i got the VideoProcAmp_Gamma and CAPTURE_GAMMA  from  https://github.com/jarikomppa/escapi/blob/master/escapi_dll/capture.cpp . but dont know for sure with which int i can call the dll with

;--------------------------------------------------------------------------
; ESCAPI is a very simple DLL interface to use video capture devices
; (most commonly webcams, but also video in devices and other similar things)
; Get dll from --> [url="http://sol.gfxile.net/escapi/index.html"]http://sol.gfxile.net/escapi/index.html[/url]
;
#include <GDIplus.au3>
#include <GUIConstantsEx.au3>
;--------------------------------------------------------------------------
; Local variables
Local $Width = 320 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 240 ; adjust the image size to $Width by $Height
;--------------------------------------------------------------------------
; variables required for dshow escapi
Local $tagSimpleCapParams = _
        "ptr mTargetBuf;" & _
        "int mWidth;" & _
        "int mHeight;"
Local $tSimpleCapParams = DllStructCreate($tagSimpleCapParams)
Local $tTargetBuf = DllStructCreate("BYTE[" & $Width * $Height * 4 & "]")
Global $pTargetBuf = DllStructGetPtr($tTargetBuf)
DllStructSetData($tSimpleCapParams, 1, $pTargetBuf)
DllStructSetData($tSimpleCapParams, 2, $Width)
DllStructSetData($tSimpleCapParams, 3, $Height)
Local $pSimpleCapParams = DllStructGetPtr($tSimpleCapParams)
Local $device = 0 ;change this number to select dshow device
;---------------------------------------------------------------------------
;Escapi init
Local $_escapi_Dll = DllOpen("escapi.dll")
$return = DllCall($_escapi_Dll, "int", "initCOM")
$return = DllCall($_escapi_Dll, "int:cdecl", "initCapture", "int", $device, "ptr", $pSimpleCapParams)
;---------------------------------------------------------------------------
; GUI and GDI init
Global $hwnd = GUICreate("EscApi WebCam", $Width, $Height)
GUISetState(@SW_SHOW, $hwnd)
_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)

$C_timer = TimerInit()
While 1

    ;---------------------------------------------------------------------------
    ;Get frame
    DllCall($_escapi_Dll, "none:cdecl", "doCapture", "int", 0)
    Do
        $return = DllCall($_escapi_Dll, "int:cdecl", "isCaptureDone", "int", 0)
        Sleep(100)
    Until $return[0] = 1

    ;---------------------------------------------------------------------------
    ;Display frame
    $hBMP = _WinAPI_CreateBitmap($Width, $Height, 1, 32, $pTargetBuf)
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $Width, $Height)
    _GDIPlus_BitmapDispose($hImage)
    _WinAPI_DeleteObject($hBMP)


    Local $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    Sleep(500)

    ;################################################# getCapturePropertyValue #################################################
    $Default = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", "int", $device, "int", '')
    ;extern "C" float __declspec(dllexport) getCapturePropertyValue(unsigned int deviceno, int prop)
    ConsoleWrite("$Default CAPTURE_BRIGHTNESS = " & $Default[0] & @LF)
    
    ;WHAT is WRONG HERE? CAPTURE_GAMMA
    $CAPTURE_GAMMA = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", "int", $device, "int", 'CAPTURE_GAMMA');,"int",20)
    ConsoleWrite("$CAPTURE_GAMMA = " & $CAPTURE_GAMMA[0] & @LF)
    
    ;WHAT is WRONG HERE? VideoProcAmp_Gamma
    $VideoProcAmp_Gamma = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", "int", $device, "int", 'VideoProcAmp_Gamma');,"int",20)
    ConsoleWrite("$VideoProcAmp_Gamma = " & $VideoProcAmp_Gamma[0] & @LF& @LF)

    If Round(TimerDiff($C_timer)) > 5000 Then
        $C_timer = TimerInit()

        ;WHAT is WRONG HERE? CAPTURE_BRIGHTNESS to 0.1
        $setCaptureProperty = DllCall($_escapi_Dll, "int:cdecl", "setCaptureProperty", "int", $device, "int", '', 'float', 0.1)
        ;~ extern "C" int __declspec(dllexport) setCaptureProperty(unsigned int deviceno, int prop, float value, int autoval)
        ConsoleWrite("$setCaptureProperty = " & $setCaptureProperty[0] & @LF)

    EndIf
WEnd

;--------------------------------------------------------------------------
;Clean up
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)

i can change the brightness with the propgui.exe which is inside the dll examples folder, and then the script picks the changed value up with the dllcall, but was not able to set it from autoit nor pick up the Gama or other settings

Share this post


Link to post
Share on other sites



Take a look to escapi.h for declare CAPTURE_GAMMA

;~ CAPTURE_PROPERTIES
Global Enum _
    $CAPTURE_BRIGHTNESS, _
    $CAPTURE_CONTRAST, _
    $CAPTURE_HUE, _
    $CAPTURE_SATURATION, _
    $CAPTURE_SHARPNESS, _
    $CAPTURE_GAMMA, _
    $CAPTURE_COLORENABLE, _
    $CAPTURE_WHITEBALANCE, _
    $CAPTURE_BACKLIGHTCOMPENSATION, _
    $CAPTURE_GAIN, _
    $CAPTURE_PAN, _
    $CAPTURE_TILT, _
    $CAPTURE_ROLL, _
    $CAPTURE_ZOOM, _
    $CAPTURE_EXPOSURE, _
    $CAPTURE_IRIS, _
    $CAPTURE_FOCUS, _
    $CAPTURE_PROP_MAX

 


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hello JVDS

Have tested your script and it worked nice now.

see your script running as wanted now:

;--------------------------------------------------------------------------
; ESCAPI is a very simple DLL interface to use video capture devices
; (most commonly webcams, but also video in devices and other similar things)
; Get dll from --> [url="http://sol.gfxile.net/escapi/index.html"]http://sol.gfxile.net/escapi/index.html[/url]
;

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=projector.ico
#AutoIt3Wrapper_Res_Fileversion=1.1.0.0
#AutoIt3Wrapper_Res_SaveSource=y
#AutoIt3Wrapper_Res_Language=1031
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GDIplus.au3>
#include <GUIConstantsEx.au3>
;--------------------------------------------------------------------------
; Local variables
Local $Width = 320 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 240 ; adjust the image size to $Width by $Height
;--------------------------------------------------------------------------
; variables required for dshow escapi
Local $tagSimpleCapParams = _
        "ptr mTargetBuf;" & _
        "int mWidth;" & _
        "int mHeight;"
Local $tSimpleCapParams = DllStructCreate($tagSimpleCapParams)
Local $tTargetBuf = DllStructCreate("BYTE[" & $Width * $Height * 4 & "]")
Global $pTargetBuf = DllStructGetPtr($tTargetBuf)
DllStructSetData($tSimpleCapParams, 1, $pTargetBuf)
DllStructSetData($tSimpleCapParams, 2, $Width)
DllStructSetData($tSimpleCapParams, 3, $Height)
Local $pSimpleCapParams = DllStructGetPtr($tSimpleCapParams)
Local $device = 0 ;change this number to select dshow device
;---------------------------------------------------------------------------
;Escapi init
Local $_escapi_Dll = DllOpen("escapi.dll")
$return = DllCall($_escapi_Dll, "int", "initCOM")
$return = DllCall($_escapi_Dll, "int:cdecl", "initCapture", "int", $device, "ptr", $pSimpleCapParams)
;---------------------------------------------------------------------------
; GUI and GDI init
Global $hwnd = GUICreate("EscApi WebCam", $Width, $Height)
GUISetState(@SW_SHOW, $hwnd)
_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)

$C_timer = TimerInit()
While 1

    ;---------------------------------------------------------------------------
    ;Get frame
    DllCall($_escapi_Dll, "none:cdecl", "doCapture", "int", 0)
    Do
        $return = DllCall($_escapi_Dll, "int:cdecl", "isCaptureDone", "int", 0)
        Sleep(100)
    Until $return[0] = 1

    ;---------------------------------------------------------------------------
    ;Display frame
    $hBMP = _WinAPI_CreateBitmap($Width, $Height, 1, 32, $pTargetBuf)
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

    _GDIPlus_GraphicsDrawImageRect($hGraphics, $hImage, 0, 0, $Width, $Height)
    _GDIPlus_BitmapDispose($hImage)
    _WinAPI_DeleteObject($hBMP)


    Local $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    Sleep(500)

    ;################################################# getCapturePropertyValue #################################################
    $Default = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", "int", $device, "int", 0) ; SOLVED
    ;extern "C" float __declspec(dllexport) getCapturePropertyValue(unsigned int deviceno, int prop)
    ConsoleWrite("$Default CAPTURE_BRIGHTNESS = " & $Default[0] & @LF)

    ;SOLVED CAPTURE_GAMMA
    $CAPTURE_GAMMA = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", _
    "int", $device, "int", 5)
    ConsoleWrite("$CAPTURE_GAMMA = " & $CAPTURE_GAMMA[0] & @LF)

    ;SOLVED VideoProcAmp_Gamma
    $VideoProcAmp_Gamma = DllCall($_escapi_Dll, "float:cdecl", "getCapturePropertyValue", _
    "int", $device, "int", 5)
    ConsoleWrite("$VideoProcAmp_Gamma = " & $VideoProcAmp_Gamma[0] & @LF & @LF)

    If Round(TimerDiff($C_timer)) > 5000 Then
        $C_timer = TimerInit()

        ;SOLVED CAPTURE_BRIGHTNESS to 0.1
        $setCaptureProperty = DllCall($_escapi_Dll, "int:cdecl", "setCaptureProperty", _
        "int", $device, "int", 0, 'float', 0.1, "int", 0)
;~ extern "C" int __declspec(dllexport) setCaptureProperty(unsigned int deviceno, int prop, float value, int autoval)
        ConsoleWrite("$setCaptureProperty = " & $setCaptureProperty[0] & @LF) ; Do not know what the return value means ?

    EndIf
WEnd

;--------------------------------------------------------------------------
;Clean up
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)

I had a similiar issue.

I found the following solution by writing

the PropValues as int values like 0 1 2 3 4 instead of using constructs like 'VideoProcAmp_Gamma'

see $iProp within the for next loop.

See the following code snippet about i have addressed the props to read out.

Local $eEND_PROP = 17
Local $aPropVal ; [0] = Value; [1] = DeviceId ; [2] = PropId
Local $aPropAuto ; [0] = Value; [1] = DeviceId ; [2] = PropId
For $iProp = 0 To $eEND_PROP
    $aPropVal = DllCall($_escapi_Dll, "FLOAT:cdecl", "getCapturePropertyValue", "int", $device, "int", $iProp)
    $aPropAuto = DllCall($_escapi_Dll, "INT:cdecl", "getCapturePropertyAuto", "int", $device, "int", $iProp)
    ConsoleWrite(" Prop: " & $aPropVal[2] & _
            " Auto: " & $aPropAuto[0] & _
            " DEVICE: " & $aPropVal[1] & _
            " VALUE: " & $aPropVal[0] & _
            @CRLF)
Next

 

Also i'am able to "setCaptureProperty"

I used the following by example simple statement.
;                                                                                                                   CAMERA       Prop         Value       Auto
$aSetProp = DllCall($_escapi_Dll, "int:cdecl", "setCaptureProperty", "int", $device, "int", 0, "float", 0.3, "int", 0)

Add the brightnes prop id:  "int",0
and add the auto prop at the end "int",0

That should help.

see result from ConsoleWindow:
Prop: 0 Auto: 0 DEVICE: 0 VALUE: 0.296875

Value of 0.3 means dark and 0.9 light.

Have tested it with 2 webcams at the same time on my laptop running WIN10 64

Tested under Autoscript 64 as well 32 with the related ESCAPI.DLL

 Starting AutoIt3Wrapper v.14.801.2025.0 SciTE v.3.4.4.0   Keyboard:00000407  OS:WIN_81/  CPU:X64 OS:X64    Environment(Language:0407)

 SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\juergen\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper

>Running:(3.3.12.0):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "X:\Project\WebCamShutter\ESCAPI.au3"

>Running:(3.3.12.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "X:\Project\WebCamShutter\ESCAPI.au3"

Hope that helps

PS: I'am on the way to include sliders into my  script to adjust properties.

--Juergen

Edited by JuergenAlfing

Share this post


Link to post
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

  • Similar Content

    • DrAhmed
      By DrAhmed
      Hey everyone,
      I am looking for a simple function to take a single snapshot from webcam and save  it as a picture
      I've searched in google and most of UDF's I foud were outdated and not working, others are very complicated to use .
      It would be cool if it can control the quality of saved picture like in screenshots
      _ScreenCapture_SetJPGQuality($Quality) I would appreciate it If someone can help
      thanks in advance
    • FireFox
      By FireFox
      I recently bought an IP Camera to monitor my house. There is a web access to view the video stream but not to record it.

      Here is the IP Camera in question, it should work with every other similar product.

      So I made a simple example which does the job.

      Here is the code :
      #include <GUIConstants.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <WinAPI.au3> #include <GDIPlus.au3> #include <Memory.au3> #region Global Vars Global Const $sProgramTitle = "IP Camera stream + record" ;EDIT THE FOLLOWING LINE ONLY: Global Const $iIPAddress = "192.168.1.99", $iPort = 99, $shtauth = "yourauth==" Global Const $STM_SETIMAGE = 0x0172 Global $blRecording = False, $blGUIMinimized = False Global Const $sRecordDir = @ScriptDir & "\ip_camera_stream" Global $bRecvtmp = Binary(""), $bStream = $bRecvtmp Global $iImgLen = 0, $iStreamLen = 0, $iWritten = 0, $iEOH = 0, $iContLenPos = 0, $hImgFile = 0, $pBuffer = 0, $iImgCount = 0 Global Const $iContLengthLen = StringLen("Content-Length: ") Global $sStream = "", $sTrim2ContLen = "" Global $hBMP = 0, $hGraphics = 0, $hHBITMAP2 = 0, $hFamily = 0, $hFont = 0, $tLayout = "", $hFormat = 0, $hBrush = 0 #endregion Global Vars TCPStartup() Global $iSocket = TCPConnect($iIPAddress, $iPort) If @error Then MsgBox(16, $sProgramTitle, "Could not connect !") Exit -1 EndIf TCPSend($iSocket, _ "GET /videostream.cgi HTTP/1.1" & @CRLF & _ "Host: " & $iIPAddress & ":" & $iPort & @CRLF & _ "Connection: keep-alive" & @CRLF & _ "Authorization: Basic " & $shtauth & @CRLF & @CRLF) #region GUI Global $hGUI = 0, $pPic = 0, $hPic = 0, $btnRecord = 0 $hGUI = GUICreate($sProgramTitle, 640, 525) $pPic = GUICtrlCreatePic("", 0, 0, 640, 480, $SS_BITMAP) GUICtrlSetState($pPic, $GUI_DISABLE) $hPic = GUICtrlGetHandle($pPic) $btnRecord = GUICtrlCreateButton("Record", 10, 490, 80, 26) GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND") GUISetState(@SW_SHOW, $hGUI) #endregion GUI _GDIPlus_Startup() $hFamily = _GDIPlus_FontFamilyCreate("Arial") $hFont = _GDIPlus_FontCreate($hFamily, 17) $tLayout = _GDIPlus_RectFCreate(10, 10, 100, 40) $hFormat = _GDIPlus_StringFormatCreate() $hBrush = _GDIPlus_BrushCreateSolid(0xAFFF0000) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $btnRecord If $blRecording Then GUICtrlSetData($btnRecord, "Record") Else If Not FileExists($sRecordDir) Then DirCreate($sRecordDir) GUICtrlSetData($btnRecord, "Stop recording") EndIf $blRecording = Not $blRecording EndSwitch $bRecvtmp = TCPRecv($iSocket, 4096, 1) ;4kb If @error Then ExitLoop If Not BinaryLen($bRecvtmp) Then ContinueLoop $bStream &= $bRecvtmp If $iImgLen = 0 Then $sStream = BinaryToString($bStream) $iContLenPos = StringInStr($sStream, "Content-Length: ", 2) $iEOH = StringInStr($sStream, @CRLF & @CRLF, 2, 1, $iContLenPos) If $iEOH = 0 Or $iContLenPos = 0 Then ContinueLoop $sTrim2ContLen = StringTrimLeft($sStream, $iContLenPos + $iContLengthLen - 1) $iImgLen = Number(StringLeft($sTrim2ContLen, StringInStr($sTrim2ContLen, @CR, 2) - 1)) $bStream = BinaryMid($bStream, $iEOH + 4) EndIf If $iImgLen = 0 Then ContinueLoop $iStreamLen = BinaryLen($bStream) If $iStreamLen < $iImgLen Then ContinueLoop If Not $blGUIMinimized Then $hBMP = Load_BMP_From_Mem($bStream) If $blRecording Then $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBMP) _GDIPlus_GraphicsDrawStringEx($hGraphics, "[•REC]", $hFont, $tLayout, $hFormat, $hBrush) EndIf $hHBITMAP2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP) _WinAPI_DeleteObject(_SendMessage($hPic, $STM_SETIMAGE, 0, $hHBITMAP2)) _GDIPlus_ImageDispose($hBMP) If $blRecording Then _GDIPlus_GraphicsDispose($hGraphics) _WinAPI_DeleteObject($hHBITMAP2) EndIf If $blRecording Then $pBuffer = DllStructCreate("byte[" & $iImgLen & "]") If $iStreamLen > $iImgLen Then DllStructSetData($pBuffer, 1, BinaryMid($bStream, 1, $iImgLen)) $bStream = BinaryMid($bStream, $iImgLen) Else DllStructSetData($pBuffer, 1, $bStream) $bStream = Binary("") EndIf $hImgFile = _WinAPI_CreateFile($sRecordDir & "\snap_" & StringFormat("%.4d", $iImgCount) & ".jpg", 3, 4, 4) _WinAPI_WriteFile($hImgFile, DllStructGetPtr($pBuffer), $iImgLen, $iWritten) _WinAPI_CloseHandle($hImgFile) $iImgCount += 1 EndIf $iImgLen = 0 WEnd _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_BrushDispose($hBrush) _GDIPlus_Shutdown() TCPCloseSocket($iSocket) TCPShutdown() Func WM_SYSCOMMAND($hWnd, $iMsg, $wParam, $lParam) Local Const $SC_MINIMIZE = 0xF020, $SC_RESTORE = 0xF120 Switch BitAND($wParam, 0xFFF0) Case $SC_MINIMIZE, $SC_RESTORE $blGUIMinimized = Not $blGUIMinimized EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_SYSCOMMAND Func Load_BMP_From_Mem($bImage) ;_GDIPlus_BitmapCreateFromMemory2     ;Author: UEZ     ;Modified: ProgAndy, Yashied, FireFox     If Not IsBinary($bImage) Then Return 0     Local $memBitmap = Binary($bImage)     Local $iLen = BinaryLen($memBitmap)     Local $GMEM_MOVEABLE = 0x0002     Local $aResult = DllCall("kernel32.dll", "handle", "GlobalAlloc", "uint", $GMEM_MOVEABLE, "ulong_ptr", $iLen)     Local $hData = $aResult[0]     $aResult = DllCall("kernel32.dll", "ptr", "GlobalLock", "handle", $hData)     If @error Then Return 0     Local $tMem = DllStructCreate("byte[" & $iLen & "]", $aResult[0])     DllStructSetData($tMem, 1, $memBitmap)     DllCall("kernel32.dll", "bool", "GlobalUnlock", "handle", $hData)     If @error Then Return 0     $aResult = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $hData, "int", True, "ptr*", 0)     $hStream = $aResult[3]     If @error Then Return 0     $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0)     If @error Then Return 0     DllCall('oleaut32.dll', 'long', 'DispCallFunc', 'ptr', $hStream, 'ulong_ptr', 8 * (1 + @AutoItX64), 'uint', 4, 'ushort', 23, 'uint', 0, 'ptr', 0, 'ptr', 0, 'str', '')     Return $aResult[2] EndFunc   ;==>Load_BMP_From_Mem _
      Advanced example :
      Preview :



      Attachment :
      IP Camera.au3
      IP Camera.au3 (Previous: 34 downloads)
       
      Enjoy, I'm watching U !
    • DCCD
      By DCCD
      Hi everybody, I have some code for acquiring webcam image using [WM_CAP_DRIVER_CONNECT], Unfortunately [WM_CAP_DRIVER_CONNECT] return nothing.

      I have been reading about [WM_CAP_DRIVER_CONNECT] on MSDN LINK, they said

      So my question is, How can I get returned value from [WM_CAP_DRIVER_CONNECT].?

      Any help would be greatly appreciated.