Jump to content

Webcam UDF


LIMITER
 Share

Recommended Posts

hey

I just registered at the forum because of my new project. In the last two years I made a few programs for my daily work..

These are my first steps with DLL's included.

In the last two day's I looked after a solution to take snapshots of my webcam in an hidden programm. (In the end it should ask through FTP if there is an 1 or 0 in my trigger.txt. If it is 1 I changed it because my notebook is stolen. Then the little new program should catch a lot of info's about 'new' the user and store it on my webspace)

Everything is fine, but two problems are still there..

1. The scripts working nice when I'm booting my system and run it the first time. The second time the screen's black till I reboot the machine. (logout changes nothing)

=>

Hi!

I tested on four computers.

For the first, it's OK, whenever.

For other (three), if works only once. When I re-run the script, the webcam does not open, until I restart the computer.

I tried also to close/re-open a session, but the problem remains.

The first CPU is on W7

Other : W7 , W7 , XP

Any idea?

2. How to campture a snapshot without any GUI? My program should be hidden as described at the top.

=>

Some thoughts on your UDF

First, is there a way to capture some output without having to open a GUI control. Because if I don't call _Webcam() the array $cap is never declared.

And as far as i understand, the _WebcamSnapShot() does read the output from the previously created controll.

Link to comment
Share on other sites

1. The scripts working nice when I'm booting my system and run it the first time. The second time the screen's black till I reboot the machine. (logout changes nothing)

=>

As someone else mentioned,

You need to destroy the gui used for webcam.

Call GUIDelete() before WebcamStop().

I'm not sure this UDF properly creates/manages the Webcam, it might need some tweaking as it appears to be about 2 years old or so.

2. How to campture a snapshot without any GUI? My program should be hidden as described at the top.

=>

Have you tried modifying the library to call GUISetState with @SW_HIDE or doing that part manually? Again, a different Webcam UDF or library might work better for you here.

Link to comment
Share on other sites

thank you for the fast answer.

the original post with the tipp to delete the GUI was because of an other problem. not because the black screen, but I tested it and nothing changed.

I tested three UDF's since yesterday. but everytime the same two problems come up.

well... at the moment all webcam-screens are black. after reboot, too.

now my headache are to hard .. tomorrow is a new day

Link to comment
Share on other sites

  • 1 month later...

Is there any way to make a snapshot and save it to the memory (and not to the HDD)??

Func _WebcamSnapShot($file = $snapfile)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $file)
    GUICtrlCreatePic ($file, 0, 580, 500, 300)
EndFunc

I want to take a snapshot and draw it in the GUI to work with it (measuring distances between two points) and the code above is very slowly for my pourposes (on the final script I'm going to need to take a snapshot from 1000 to 300 ms)

Thanks for the UDF.

Link to comment
Share on other sites

  • 5 months later...

As someone else mentioned,

I'm not sure this UDF properly creates/manages the Webcam, it might need some tweaking as it appears to be about 2 years old or so.

Have you tried modifying the library to call GUISetState with @SW_HIDE or doing that part manually? Again, a different Webcam UDF or library might work better for you here.

ha so old topic 'D

but i will add some own input,

yeah i have same black screen issue, but when i unplug camera and plug to other USB port its will works without reboot ...

maybe we have to flush USB dev or what ?!

Link to comment
Share on other sites

  • 2 months later...

only I am getting errors in any webcam scripts?

C:\Program Files (x86)\AutoIt3\Include\Webcam.au3(122,91) : WARNING: $WS_CHILD: possibly used before declaration.
$cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files (x86)\AutoIt3\Include\Webcam.au3(122,103) : WARNING: $WS_VISIBLE: possibly used before declaration.
$cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Program Files (x86)\AutoIt3\Include\Webcam.au3(122,91) : ERROR: $WS_CHILD: undeclared global variable.
$cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\lenovo\Documents\ai3\webcma.au3 - 1 error(s), 2 warning(s)

I was trying to fix it, declaring $WS_CHILD and visible as "", 1 or 0 and combinations. I also tried 1 and 0 instead of BitOR($WS_CHILD,$WS_VISIBLE) but it didnt work.

I am using Win7 x64. I was trying also to update my webcam drivers but they are fresh. any help?

I have the same problem as Splash.

Edited by cagiva
Link to comment
Share on other sites

  • 2 months later...

Modified Cam snapshot function to convert the image to preferred format. *Tested with PNG, JPG, and BMP. Also fixed any errors the UDF was giving. Also added timestamp switch to capture method. :unsure:

P.S. Yes, I know there were 10 more optimal ways to do the modifications I've done... but here it is anyway. :>

#include-once
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Date.au3>


;~ #####################################################
;~ ###                                               ###
;~ ###                  Webcam UDF                   ###
;~ ###                                               ###
;~ ### Functions : _WebcamInit()                     ###
;~ ###             _Webcam()                         ###
;~ ###             _WebcamStop()                     ###
;~ ###             _WebcamSnapShot()                 ###
;~ ###                                               ###
;~ ###              Made by L|M|TER          ###
;~ ### --------------------------------------------- ###
;~ ###                                               ###
;~ ###           Copyright ©2008 - L|M|TER          ###
;~ ###      Updated and modified - BinaryBrother             ###
;~ #####################################################

;~ Declaring Variables

$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
$cap = ""
$avi = ""
$user = ""
$snapfile = @ScriptDir & "\snapshot.bmp"

;~ ##########################################################
;~ Function Name : _WebcamInit()
;~ Description : Starts the webcam image capturing session
;~ Author : L|M|TER
;~ ##########################################################

Func _WebcamInit()
    $avi = DllOpen("avicap32.dll")
    $user = DllOpen("user32.dll")
EndFunc   ;==>_WebcamInit

;~ ##########################################################
;~ Function Name : _Webcam($gui,$h,$w,$l,$t)
;~ Description : Creates a webcam preview window
;~ Parameter(s):
;~  $gui - The gui where the webcam window should be created
;~  $h - The height of the webcam window
;~  $w - The width of the webcam window
;~  $l - The left position of the webcam window
;~  $t - The top position of the webcam window
;~ NOTE : All parameters required !
;~ Author : L|M|TER
;~ ##########################################################

Func _Webcam($gui, $w, $h, $l, $t)
    $cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD, $WS_VISIBLE), "int", $l, "int", $t, "int", $w, "int", $h, "hwnd", $gui, "int", 1)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_CONNECT, "int", 0, "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)
EndFunc   ;==>_Webcam

;~ ##########################################################
;~ Function Name : _WebcamStop()
;~ Description : Closes the webcam image capturing session
;~ Author : L|M|TER
;~ ##########################################################

Func _WebcamStop()
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_END, "int", 0, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_DRIVER_DISCONNECT, "int", 0, "int", 0)
    DllClose($user)
    DllClose($avi)
EndFunc   ;==>_WebcamStop

;~ ##########################################################
;~ Function Name : _WebcamSnapShot($file)
;~ Description : Takes a snapshot
;~ Parameter(s):
;~  $file (Optional) - The path to the file where the snapshot will be saved (Default : @ScriptDir & "\snapshot.bmp")
;~ Author : L|M|TER
;~ ##########################################################

Func _WebcamSnapShot($Dest = $snapfile, $AddTimeStamp = false)
    $RandomFileName = Random(9000, 99999, 1) & ".bmp"
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0)
    DllCall($user, "int", "SendMessage", "hWnd", $cap[0], "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $RandomFileName)
    _ConvertImage($RandomFileName, $Dest, $AddTimeStamp)
    FileDelete($RandomFileName)
EndFunc   ;==>_WebcamSnapShot

;~ ##########################################################
;~ Function Name : _ConvertImage($file)  (Handles Timestamping as well)
;~ Description : Converts image
;~ Parameter(s):
;~  $Src - The path to the file where the snapshot was saved in BMP
;~  $Dest - Destination file path with preferred extension
;~  $AddTimeStamp - Bool add timestamp
;~ Author : BinaryBrother (Internal use only)
;~ ##########################################################

Func _ConvertImage($Src, $Dest, $AddTimeStamp = False)
    _GDIPlus_Startup()
    $hImage = _GDIPlus_ImageLoadFromFile($Src)
    If $AddTimeStamp Then
        $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage)
        $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)
        $hFormat = _GDIPlus_StringFormatCreate()
        $hFamily = _GDIPlus_FontFamilyCreate("Arial")
        $hFont = _GDIPlus_FontCreate($hFamily, 16, 1)
        $tLayout = _GDIPlus_RectFCreate(500, 415, 150, 100)
        _GDIPlus_GraphicsDrawStringEx($hGraphic, _NowDate() & @CRLF & _NowTime(), $hFont, $tLayout, $hFormat, $hBrush)
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
        _GDIPlus_BrushDispose($hBrush)
        _GDIPlus_GraphicsDispose($hGraphic)
    EndIf
    _GDIPlus_ImageSaveToFile($hImage, $Dest)
    _GDIPlus_ImageDispose($hImage)
    _GDIPlus_Shutdown()
EndFunc   ;==>_ConvertImage

Webcam.au3

Edited by BinaryBrother

SIGNATURE_0X800007D NOT FOUND

Link to comment
Share on other sites

Modified Cam snapshot function to convert the image to preferred format. *Tested with PNG, JPG, and BMP. Also fixed any errors the UDF was giving. Also added timestamp switch to capture method. :unsure:

P.S. Yes, I know there were 10 more optimal ways to do the modifications I've done... but here it is anyway. :>

I'm surprised no one commented, these are some helpful changes. I wouldn't worry about how optimal it is.

Link to comment
Share on other sites

Modified Cam snapshot function to convert the image to preferred format. *Tested with PNG, JPG, and BMP. Also fixed any errors the UDF was giving. Also added timestamp switch to capture method. :unsure:

P.S. Yes, I know there were 10 more optimal ways to do the modifications I've done... but here it is anyway. :>

I'm surprised no one commented, these are some helpful changes. I wouldn't worry about how optimal it is.

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

Hello !

I got this error when running the example provided :

C:\Users\Moi\Documents\Webcam.au3 (122) : ==> Variable used without being declared.:

$cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", $l, "int", $t, "int", $w, "int", $h, "hwnd", $gui, "int", 1)

$cap = DllCall($avi, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR(^ ERROR

Is it a compatibility issue (I'm using Windows 7) ? Or something else ?

Thanks for your answer !

Link to comment
Share on other sites

  • 1 year later...

i working on program eye detecting ... so i need some func return a string like this

800 x 600

asdaslkdjaskljdlkasjdklasjlkdj......

sadjaskljdlkasjdklaskldjlaskjd......

........

sakdjaslkdjaskljdklasjdlkasjkd.....

3 byte give me a color of a pixel

Can anyone help me or show me the way to find out ?

Link to comment
Share on other sites

  • 6 months later...

INTERESTING DISCOVERY.

Script works fine but camera is not activated unless i open it in another application such as Skype, then script is able to capture images. Otherwise they are not generated at all and are 0kb's file size.

 

Question.

Is there a way to unload my camera prior to this script activating it ? I think its locked or something and needs to be unlocked first.

I ask because: everytime script tries to capture anything, i get this wondow "Video Source" and i have to select which device to use.

I only have 1 device and it is selected by default but that makes no difference, images are still not generated.

My computer is now restored to a state where webcam is not working. If i attemtp using camera with Skype, it will get unlocked and script will take pictures just fine but i have not done that yet so i can unlock it via the script.

 

Any ideas how to unload/unlock/close or what ever SKYPE does to my webcam that makes it WORKING again ?

Thanks

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 months later...

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