Jump to content

Simple Webcam access using Escapi


mylise
 Share

Recommended Posts

This is a down to earth DLL to access webcams in Direct Show. The only hick is it lacks access to webcam's adjustments.

You can find more information here: http://sol.gfxile.net/escapi/index.html

Here is a simple script showing how to use it. Have fun!

;--------------------------------------------------------------------------
; 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 = 640 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 480  ; 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)
;---------------------------------------------------------------------------
;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)

While 1
Local $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
sleep(50)
WEnd
;--------------------------------------------------------------------------
;Clean up
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)
Edited by mylise
Link to comment
Share on other sites

Thank you Wakillon for pointing out the errors. Next time I will try the scripts after cleaning them up!

I did forget two lines:

#include <GUIConstantsEx.au3>

Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)

P.S. The script above has been edited and fixed.

Edited by mylise
Link to comment
Share on other sites

;--------------------------------------------------------------------------
; 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 = 640 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 480  ; 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
Local $sver = "--ERROR--"
Local $tCam = DllStructCreate("CHAR v[20]") ;buffer used to get ESCAPI device name
DllStructSetData($tCam, 1, $sver)
Local $pCam = DllStructGetPtr($tCam)
;---------------------------------------------------------------------------
;Escapi init and get Information
local $_escapi_Dll = DllOpen("escapi.dll")
Local $CamInfo
;ESCAPI version
$return = DllCall($_escapi_Dll,"int","ESCAPIDLLVersion")
$CamInfo &= "Dll version = " & Hex($return[0],3) & @CRLF
$return = DllCall($_escapi_Dll,"int","initCOM")
; get number of available devices
$return = DllCall($_escapi_Dll,"int","countCaptureDevices")
$CamInfo &= "Number of devices = " & $return[0] & @CRLF
; get selected device name, $device = 0
DllCall($_escapi_Dll,"none:cdecl","getCaptureDeviceName", "uint" , $device, "ptr" , $pCam  , "int" , 20)
Local $vCam = DllStructGetData($tCam , 1)
$CamInfo &= "First device name = " & $vCam & @CRLF
MsgBox(0,"ESCAPI info", $CamInfo)
$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)
;---------------------------------------------------------------------------
;Get frame
DllCall($_escapi_Dll,"none:cdecl","doCapture", "int", $device)
Do
$return = DllCall($_escapi_Dll,"int:cdecl","isCaptureDone", "int", $device)
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)
While 1
Local $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
sleep(50)
WEnd
;--------------------------------------------------------------------------
;Clean up
$return = DllCall($_escapi_Dll,"none:cdecl","deinitCapture", "int", $device)
DllClose($_escapi_Dll)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)

Edited by mylise
Link to comment
Share on other sites

  • 7 months later...

Yo, sorry for Grave digging the thread, but I am really interessted in this.

I have problems with the SnapShot function.

I like the snapshotfunction to have the device ID as prameter... but it won't work... why?

Here is my code:

;--------------------------------------------------------------------------
; 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]
; http://www.autoitscript.com/forum/topic/150536-simple-webcam-access-using-escapi/


#include <GDIplus.au3>
#Include <GUIConstantsEx.au3>


; ================================= DIRECTSHOW VARIABLES ==================================
Local $Width = 640 ;Escapi seems to use 640x480 (or lower) as webcam resolution and will
Local $Height = 480  ; adjust the image size to $Width by $Height
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 $CountCams ; Count connected webcam devices
Local $device = 0 ;change this number to select dshow device
Local $sver = "--ERROR--"
Local $tCam = DllStructCreate("CHAR v[20]") ;buffer used to get ESCAPI device name
DllStructSetData($tCam, 1, $sver)
Local $pCam = DllStructGetPtr($tCam)
local $_escapi_Dll = DllOpen("escapi.dll")
Local $CamInfo
;==========================================================================================

Global $hwnd = GUICreate("Webcam Manager", $Width, $Height)
GUISetState(@SW_SHOW,$hwnd)
_GDIPlus_Startup()
Global $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hwnd)

;==========================================================================================

Func GetDLLVersion()
    $return = DllCall($_escapi_Dll,"int","initCOM")
    $return = DllCall($_escapi_Dll,"int","ESCAPIDLLVersion")
    $CamInfo &= "Dll version = " & Hex($return[0],3) & @CRLF
    Return $CamInfo
EndFunc

Func CountWebcams()
    $return = DllCall($_escapi_Dll,"int","initCOM")
    $return = DllCall($_escapi_Dll,"int","countCaptureDevices")
    $CamInfo &= $return[0] & @CRLF
    Return $CamInfo
EndFunc


Func GetWebcamName($DevNo)
    $return = DllCall($_escapi_Dll,"int","initCOM")
    DllCall($_escapi_Dll,"none:cdecl","getCaptureDeviceName", "uint" , $DevNo, "ptr" , $pCam  , "int" , 20)
    Local $vCam = DllStructGetData($tCam , 1)
    $CamInfo = $vCam & @CRLF
    Return $CamInfo
EndFunc

If CountWebcams() > 0 Then
    For $DevNo = 0 to CountWebcams() -1
        MsgBox(0,'Number ' & $DevNo,GetWebcamName($DevNo))
    Next
EndIf

Snapshot(1)

Func Snapshot($Index)
    DllCall($_escapi_Dll,"none:cdecl","doCapture", "int", $Index)
Do
$return = DllCall($_escapi_Dll,"int:cdecl","isCaptureDone", "int", $Index)
sleep(100)
Until $return[0] = 1
EndFunc
;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)
While 1
Local $msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
sleep(50)
WEnd

;Clean up
$return = DllCall($_escapi_Dll,"none:cdecl","deinitCapture", "int", $device)
DllClose($_escapi_Dll)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_Shutdown()
GUIDelete($hwnd)

Link to comment
Share on other sites

  • 3 months later...

Thanks for this :)

I'm finally getting full 30fps from my webcam no matter the lighting, it updates fast without waiting for auto white balance to settle, so much smoother than avicap.

Word of warning though, be sure to keep a SLEEP() in there somethere, as without the webcam becomes very hot, I only noticed as I'm using a webcam module salvaged from an old laptop, and converted it into an external usb cam, so just hold the module in my hand, and it burned hot without a SLEEP(), but with a small Sleep(10) it ran cool again. :)

Some guy's script + some other guy's script = my script!

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...