Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Save Webcam Picture *Without* Select Source Dialog?


  • Please log in to reply
14 replies to this topic
WebCama
  • Guests
  • Last active:
  • Joined: --
Hi, I'm using this code:
; http://ej.bantz.com/video/
; http://ej.bantz.com/video/detail/

; http://www.dotnet4all.com/dotnet-code/2004/12/video-capture.html

/*
Full blown app possible settings/options (inspired by Dorgem)

* Video stream:

resolution
pixel depth and compression
size (bytes)

* Capture:

1 - file

filetype (avi/pictures)
compression
framerate
location/path
watermark
auto-name
refreshrate (replace or add to max)

2 - ftp (file settings +)

url:port
log

* Motion detection:

sensitivity
action (run, email)

* Multiple camera support ???

*/

; Basic Demo - v 0.2

hModule := DllCall("LoadLibrary", "str", "avicap32.dll")

Gui, Add, GroupBox, x4 y4 w492 h100, Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 vCapDriversLV, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll
Gui, Add, Button, x412 y50 w80 h24 gRefreshDrivers, Refresh
Gui, Add, Button, x412 y76 w80 h24 gSelectDriver vSelectDriverB, Select


; --- Video preview section of Gui
Gui, Add, GroupBox, x4 y108 w492 h262, Video

; Video preview placeholder
Gui, Add, Text, x10 y124 w320 h240 vVidPlaceholder
GuiControl, +0x7, VidPlaceholder ; frame
Gui, Font, s32
Gui, Add, Text, x32 y200 w280 h64 Center, Preview
Gui, Font

Gui, Add, CheckBox, x340 y120 w100 h24 vPreviewToggleState gPreviewToggle, Preview video
Gui, Add, Button, x440 y120 w50 h24 gCopyToClipBoard, Copy
Gui, Add, Button, x400 y160 w90 h24 gSenToFile, &Send to File

/*
; --- Presets section
Gui, Add, GroupBox, x336 y144 w154 h220, Presets

Gui, Add, DropDownList, x342 y164 w142 h30 r30 vPresetChoice, Create new preset...||My save to picture set|My save to avi set|My save to ftp|My motion detection set

Gui, Add, Button, x342 y190 w70 h24 vSelectPresetB, Select
Gui, Add, Button, x414 y190 w70 h24 vNewPresetB, Edit

Gui, Add, Text, x342 y220 w142 h106 vPresetInfoT,

Gui, Add, Button, x342 y334 w70 h24 vRunPresetB, Run
Gui, Add, Button, x414 y334 w70 h24 vStopPresetB, Stop

*/

; --- Statusbar
Gui, Add, StatusBar,, Done
SB_SetIcon("shell32.dll", 222, 1)

; Get drivers
GoSub, RefreshDrivers

; Get handle of Gui
Gui +LastFound
hwndParent := WinExist()

; Show
Gui, Show, w500 h400, Video For Windows for AutoHotkey - VFW4AHK

;Gosub, ConnectToDriver

Return

PreviewToggle:
msgbox Only once!
;  If PreviewToggleState
    GoSub ConnectToDriver
;  Else
;    GoSub DisconnectDriver
Return


ConnectToDriver:
  ; --- Connect and preview
  capHwnd := Cap_CreateCaptureWindow(hwndParent, 10, 124, 320, 240)
  ;ToolTip % errorlevel
 

  WM_USER = 0x0400
  WM_CAP_START := WM_USER
  WM_CAP_GRAB_FRAME_NOSTOP := WM_USER + 61
  WM_CAP_FILE_SAVEDIB := WM_CAP_START + 25


  WM_CAP := 0x400
  WM_CAP_DRIVER_CONNECT := WM_CAP + 10
  WM_CAP_DRIVER_DISCONNECT := WM_CAP + 11
  WM_CAP_EDIT_COPY := WM_CAP + 30
  WM_CAP_SET_PREVIEW := WM_CAP + 50
  WM_CAP_SET_PREVIEWRATE := WM_CAP + 52
  WM_CAP_SET_SCALE := WM_CAP + 53
 
  ; Connect to driver
  if SelectedDriver =
  {
    MsgBox, 16, Error!, You didn't select a video driver
    Return
  }
  SendMessage, WM_CAP_DRIVER_CONNECT, %SelectedDriver%, 0, , ahk_id %capHwnd%
  ;ToolTip % errorlevel
 
  ; Set the preview scale
  SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
  ;ToolTip % errorlevel
 
  ; Set the preview rate in milliseconds
  SendMessage, WM_CAP_SET_PREVIEWRATE, 66, 0, , ahk_id %capHwnd%
  ;ToolTip % errorlevel
 
  ; Start previewing the image from the camera
  SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
  ;ToolTip % errorlevel
 
  ToolTip
Return



CopyToClipBoard:
  SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
Return

SenToFile:
    imagefile = C:\image.bmp
    SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
    ToolTip % errorlevel
return


DisconnectDriver:
  SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
Return


RefreshDrivers:
  foundDriver = 0
  LV_Delete()
  Loop
  {
    thisInfo := Cap_GetDriverDescription(A_Index-1)
    If thisInfo
    {
      foundDriver = 1
      LV_Add("", A_Index-1, thisInfo)
    }
    Else
      Break
  }
  If !foundDriver
  {
    LV_Delete()
    LV_Add("", "", "Could not get video drivers")
    GuiControl, Disable, CapDriversLV
    GuiControl, Disable, SelectDriverB
  }
Return
 
 
SelectDriver:
  FocusedRowNumber := LV_GetNext(0, "F")  ; Find the focused row.
  if not FocusedRowNumber  ; No row is focused.
    return
  LV_GetText(SelectedDriver, FocusedRowNumber, 1)
Return







/*
'// The two functions exported by AVICap
Declare Function capCreateCaptureWindowA Lib "avicap32.dll" ( _
    ByVal lpszWindowName As String, _
    ByVal dwStyle As Long, _
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Integer, _
    ByVal hWndParent As Long, ByVal nID As Long) As Long
   
Declare Function capGetDriverDescriptionA Lib "avicap32.dll" ( _
    ByVal wDriver As Integer, _
    ByVal lpszName As String, _
    ByVal cbName As Long, _
    ByVal lpszVer As String, _
    ByVal cbVer As Long) As Boolean
*/

Cap_CreateCaptureWindow(hWndParent, x, y, w, h)
{
  WS_CHILD := 0x40000000
  WS_VISIBLE := 0x10000000
 
  lpszWindowName := "test"
 
  lwndC := DLLCall("avicap32.dll\capCreateCaptureWindowA"
                  , "Str", lpszWindowName
                  , "UInt", WS_VISIBLE | WS_CHILD ; dwStyle
                  , "Int", x
                  , "Int", y
                  , "Int", w
                  , "Int", h
                  , "UInt", hWndParent
                  , "Int", 0)
 
  ;msgbox % lwndC " | " errorlevel " | " lpszWindowName " | " hwndParent
  Return lwndC
}

Cap_GetDriverDescription(wDriver)
{
  VarSetCapacity(lpszName, 100)
  VarSetCapacity(lpszVer, 100)
  res := DLLCall("avicap32.dll\capGetDriverDescriptionA"
                  , "Short", wDriver
                  , "Str", lpszName
                  , "Int", 100
                  , "Str", lpszVer
                  , "Int", 100)
  If res
    capInfo := lpszName ; " | " lpszVer
  Return capInfo
}

GuiClose:
  GoSub, DisconnectDriver
  DllCall("FreeLibrary", "str", hModule)
  ExitApp
Return

!r::Reload ; for debugging
(Source)
Which implements a WebCam viewing control into an AHK GUI. Very useful. The above version also has a "Save to file" button which is also very cool.

I was wondering if the process could be completed without showing the following dialog when clicking the "Preview" checkbox. The big goal is to be able to complete the processes with my own GUI, with no driver selection or preview check or camera selection,etc.

Posted Image



Thanks in advance. :)

fures
  • Members
  • 88 posts
  • Last active: Dec 16 2015 06:44 AM
  • Joined: 31 Jan 2008
This is cool 8) - I was just looking for something like this.

Some ammendments:
You'd better read variable from the form with ControlGet because you dont ever submit the form, so variables do not get values otherwise.
PreviewToggle:
;msgbox Only once!
ControlGet,PreviewToggleState,Checked,,Button5,A
  If PreviewToggleState
    GoSub ConnectToDriver
  Else
    GoSub DisconnectDriver
Return

You could also add a g-label to your listview in the Gui to make doubleclisk work as a selection (not jus the button)
Gui, Add, GroupBox, x4 y4 w492 h100, Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 gSelectDriver vCapDriversLV, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll

I really encourage you to go on working and implement the video capture feature.

By the way: With the changes I recommend, I do not have the Video Source selector window appearing in my system.

More of this please...

Conquer
  • Members
  • 385 posts
  • Last active: Jan 10 2013 02:14 AM
  • Joined: 27 Jun 2006
First off, thanks for the code.

Second, fures, when I use your suggestions, that window (in the screenshot above) does not show (thanks!), but the script's [Save] button no longer works. :cry:


:? Maybe someone could shed some light on this?

fures
  • Members
  • 88 posts
  • Last active: Dec 16 2015 06:44 AM
  • Joined: 31 Jan 2008
Strange with the Save button you mention...
There seems to be not any Save button in the script.
Can you specify what you mean by the Save button?

NLI-Conquer
  • Guests
  • Last active:
  • Joined: --
Sry, meant the button that is called "Send To File"
My mistake.

Gui, Add, Button, x400 y160 w90 h24 gSenToFile, &Send to File"


WebCama
  • Guests
  • Last active:
  • Joined: --
"Send To File" Button no longer works!

fures
  • Members
  • 88 posts
  • Last active: Dec 16 2015 06:44 AM
  • Joined: 31 Jan 2008
Here's my version:

Send to BMP
Send to JPG (with a little help from irfanview)
Separate preview window
Automatic device selection (if you don't select, the first will be selected)
Change size and fps "on the fly" - recreates preview window

hModule := DllCall("LoadLibrary", "str", "avicap32.dll")

WDT := 320
HGT := 240
FPS := 15
fileJPG = vidcap.jpg
fileBMP = vidcap.bmp

Gui, Add, GroupBox, x4 y4 w492 h100, Available Video Drivers
Gui, Add, ListView, x8 y20 w400 h80 gSelectDriver vCapDriversLV, Index|Name
Gui, Add, Picture, x434 y16 w32 h32 Icon204, %A_WinDir%\system32\shell32.dll
Gui, Add, Button, x412 y50 w80 h24 gRefreshDrivers, Refresh
Gui, Add, Button, x412 y76 w80 h24 gSelectDriver vSelectDriverB, Select

; --- Video preview section of Gui
Gui, Add, GroupBox, x4 y108 w492 h262, Video
Gui, Add, CheckBox, x10 y120 w100 h24 vPreviewToggleState gPreviewToggle, Preview video
Gui, Add, Text, x10 y160, width:
Gui, Add, Edit, x70 y160 w30 vWDT gdoWDT, %WDT%
Gui, Add, Text, x10 y190, height:
Gui, Add, Edit, x70 y190 w30 vHGT gdoGHT, %HGT%
Gui, Add, Text, x10 y220, fps:
Gui, Add, Edit, x70 y220 w30 vFPS gdoFPS, %FPS%
Gui, Add, Button, x110 y190 h24 gPreviewToggle, Change
Gui, Add, Button, x400 y160 h24 gCopyToClipBoard, Copy
Gui, Add, Text, x240 y193, Desktop\
Gui, Add, Edit, x290 y190 w100 vfileJPG gdoJPG, %fileJPG%
Gui, Add, Button, x400 y190 h24 gSenToFile2, Send to &JPG
Gui, Add, Text, x240 y223, Desktop\
Gui, Add, Edit, x290 y220 w100 vfileBMP gdoBMP, %fileBMP%
Gui, Add, Button, x400 y220 h24 gSenToFile, Send to &BMP

GoSub, RefreshDrivers

Gui, Show, x200 w500 h400, Video For Windows for AutoHotkey - VFW4AHK

Return



doWDT:
	ControlGetText,WDT,Edit1,A
Return

doGHT:
	ControlGetText,HGT,Edit2,A
Return

doJPG:
	ControlGetText,fileJPG,Edit4,A
Return

doBMP:
	ControlGetText,fileBMP,Edit5,A
Return

doFPS:
	ControlGetText,FPS,Edit3,A
Return

PreviewToggle:
  ControlGet,PreviewToggleState,Checked,,Button5,A
  If PreviewToggleState
  {
	Gui, 2:Destroy
	Gui, 2:Add, Text, x0 y0 w%WDT% h%HGT% vVidPlaceholder
	GuiControl, +0x7, VidPlaceholder ; frame
	Gui 2:+LastFound
	hwndParent := WinExist()
	Gui, 2:Show, x750 w%WDT% h%HGT%, Viewer
    GoSub ConnectToDriver
  }
  Else
  {
	Gui, 2:Destroy
    GoSub DisconnectDriver
  }
Return


ConnectToDriver:
  ; --- Connect and preview - hwnd, x, y, w, h
  capHwnd := Cap_CreateCaptureWindow(hwndParent, 0, 0, WDT, HGT)

  WM_USER = 0x0400
  WM_CAP_START := WM_USER
  WM_CAP_GRAB_FRAME_NOSTOP := WM_USER + 61
  WM_CAP_FILE_SAVEDIB := WM_CAP_START + 25

  WM_CAP := 0x400
  WM_CAP_DRIVER_CONNECT := WM_CAP + 10
  WM_CAP_DRIVER_DISCONNECT := WM_CAP + 11
  WM_CAP_EDIT_COPY := WM_CAP + 30
  WM_CAP_SET_PREVIEW := WM_CAP + 50
  WM_CAP_SET_PREVIEWRATE := WM_CAP + 52
  WM_CAP_SET_SCALE := WM_CAP + 53
 
  ; Connect to driver
  if SelectedDriver =
  {
	if foundDriver
		SelectedDriver = 0
	else
	{
	    MsgBox, 16, Error!, You didn't select a video driver`, and there seems to be no driver present.
	    Return
	}
  }
  SendMessage, WM_CAP_DRIVER_CONNECT, %SelectedDriver%, 0, , ahk_id %capHwnd%
 
  ; Set the preview scale
  SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
 
  ; Set the preview rate in milliseconds
  MSC := round((1/FPS)*1000)
  SendMessage, WM_CAP_SET_PREVIEWRATE, MSC, 0, , ahk_id %capHwnd%
 
  ; Start previewing the image from the camera
  SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
 
Return



CopyToClipBoard:
  SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
Return


SenToFile2:
	SendMessage, WM_CAP_EDIT_COPY, 0, 0, , ahk_id %capHwnd%
    RunWait, C:\Program Files\IrfanView\i_view32.exe /clippaste /convert=%A_Desktop%\%fileJPG%   ;copies from clipboard to file
Return


SenToFile:
    imagefile = %A_Desktop%\%fileBMP%
    SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
return


DisconnectDriver:
  SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
Return


RefreshDrivers:
  foundDriver = 0
  LV_Delete()
  Loop
  {
    thisInfo := Cap_GetDriverDescription(A_Index-1)
    If thisInfo
    {
      foundDriver = 1
      LV_Add("", A_Index-1, thisInfo)
    }
    Else
      Break
  }
  If !foundDriver
  {
    LV_Delete()
    LV_Add("", "", "Could not get video drivers")
    GuiControl, Disable, CapDriversLV
    GuiControl, Disable, SelectDriverB
  }
Return
 
 
SelectDriver:
  FocusedRowNumber := LV_GetNext(0, "F")  ; Find the focused row.
  if not FocusedRowNumber  ; No row is focused.
    return
  LV_GetText(SelectedDriver, FocusedRowNumber, 1)
Return


Cap_CreateCaptureWindow(hWndParent, x, y, w, h)
{
  WS_CHILD := 0x40000000
  WS_VISIBLE := 0x10000000
 
  lpszWindowName := "test"
 
  lwndC := DLLCall("avicap32.dll\capCreateCaptureWindowA"
                  , "Str", lpszWindowName
                  , "UInt", WS_VISIBLE | WS_CHILD ; dwStyle
                  , "Int", x
                  , "Int", y
                  , "Int", w
                  , "Int", h
                  , "UInt", hWndParent
                  , "Int", 0)
 
  Return lwndC
}


Cap_GetDriverDescription(wDriver)
{
  VarSetCapacity(lpszName, 100)
  VarSetCapacity(lpszVer, 100)
  res := DLLCall("avicap32.dll\capGetDriverDescriptionA"
                  , "Short", wDriver
                  , "Str", lpszName
                  , "Int", 100
                  , "Str", lpszVer
                  , "Int", 100)
  If res
    capInfo := lpszName ; " | " lpszVer
  Return capInfo
}


GuiClose:
  GoSub, DisconnectDriver
  DllCall("FreeLibrary", "str", hModule)
  ExitApp
Return


Has somebody got any idea how to save into a video file (with compression)?

WebCama
  • Guests
  • Last active:
  • Joined: --
The Select Source dialog is still showing..... :?

fures
  • Members
  • 88 posts
  • Last active: Dec 16 2015 06:44 AM
  • Joined: 31 Jan 2008
It's a pitty :(

What you can do is a dirty window-killing:
You start a timer with e.g. 50ms just to check and kill the window you are so :evil: about:
SetTimer, WinKill, 50
Return

WinKill:
IfWinExist, Video Source
WinClose
Return

Not tested though...

I know it's not very elegant, but who cares if it works?

WebCama
  • Guests
  • Last active:
  • Joined: --
Save or Preview no longer functions if I have an external script killing the dialog. :(


Any AHK gurus have any ideas????

Bump
  • Guests
  • Last active:
  • Joined: --
Buuump.

tarek
  • Guests
  • Last active:
  • Joined: --
SetControlDelay -1
ControlClick, OK, Video Source,,,, NA

  • Guests
  • Last active:
  • Joined: --

SetControlDelay -1
ControlClick, OK, Video Source,,,, NA


im sure they waited 2 years for your response :)

matt4068
  • Members
  • 6 posts
  • Last active: Aug 22 2013 06:21 PM
  • Joined: 16 Aug 2013

SetControlDelay -1
ControlClick, OK, Video Source,,,, NA
 SendMessage, WM_CAP_DRIVER_CONNECT, %thisInfo%, 0, , ahk_id %capHwnd%

 

I did this but it doesnt seem to do anything... i tried alsorts of delay values (-1, 0 , 500) nothing...

 

But im guessing this method will be almost identical to the winkill method. the window will flash open and press ok immediately... shame we cant just never have this window pop up.



matt4068
  • Members
  • 6 posts
  • Last active: Aug 22 2013 06:21 PM
  • Joined: 16 Aug 2013

-