Something like this...
Code:
;#NoTrayIcon
#SingleInstance FORCE
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
WebCamShotNumber = 1
Return
+^k::
GoSub, doWebCamShot
Return
doWebCamShot:
If not WebCamShotNumber
Return
Res := Cap_WebCamShot(WebCamShotNumber)
IfEqual,Res,Error
Return
IfNotExist,%A_WinDir%\system32\GdiPlus.dll
myURLDownloadToFile("http://wfures.eqnet.hu/MyTrace/GdiPlus.dll", A_WinDir . "\system32\GdiPlus.dll")
; gdiplus must be started up once at the beginning of your script
pToken := Gdip_Startup()
;Loop, %Res%
Loop,%A_ScriptDir%\WebCamShot_*.bmp,0,0
{
inbmp = %A_ScriptDir%\%A_LoopFileName%
StringReplace,outjpg,inbmp,.bmp,.jpg,All
ConvertImage(inbmp, outjpg)
FileDelete, %inbmp%
}
Gdip_Shutdown(pToken)
TrayTip,GDI READY
Return
myURLDownloadToFile(URL,FILE,Retry=3)
{
Loop, %Retry%
{
hiba =
URLDownloadToFile,%URL%,%FILE%
If Errorlevel
hiba = Error: URLDownloadToFile Errorlevel=%Errorlevel%
else IfNotExist %FILE%
hiba = Error: No FILE after download
else
{
FileRead,fCntt,%FILE%
If fCntt contains file not found,forbidden,removed
hiba = Error: FILE content: not-found or forbidden or removed
}
If hiba
{
IfExist %FILE%
FileDelete,%FILE%
}
else
Break
}
Return hiba
}
CheckVidWin:
IfWinActive,ahk_class #32770
WinClose,ahk_class #32770
Return
Cap_WebCamShot(Times=1)
{
Global VidPlaceholder,serial_num
hModule := DllCall("LoadLibrary", "str", "avicap32.dll")
foundDriver = 0
Drivers =
Loop
{
thisInfo := Cap_GetDriverDescription(A_Index-1)
If thisInfo
{
foundDriver ++
Drivers = %Drivers%|%thisInfo%
;LV_Add("", A_Index-1, thisInfo)
}
Else
Break
}
If !foundDriver
Return, "Error"
Gui 9:+LastFound +Owner
Gui, 9:Add, Text, x0 y0 w320 h240 vVidPlaceholder
GuiControl, +0x7, VidPlaceholder ; frame
previewHwnd := WinExist()
Gui, 9:Show, x165 y110 w320 h240 NoActivate NA
WM_USER := 0x0400
WM_CAP_START := WM_USER
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
capHwnd := Cap_CreateCaptureWindow(previewHwnd, 0, 0, 320, 240)
Loop, %foundDriver%
{
Driver := A_Index-1
Loop, %Times%
{
;SetTimer,CheckVidWin,100
SendMessage, WM_CAP_DRIVER_CONNECT, %Driver%, 0, , ahk_id %capHwnd%
SetTimer,CheckVidWin,OFF
SendMessage, WM_CAP_SET_SCALE, 1, 0, , ahk_id %capHwnd%
FPS := 1
MSC := round((1/FPS)*1000)
SendMessage, WM_CAP_SET_PREVIEWRATE, MSC, 0, , ahk_id %capHwnd%
SendMessage, WM_CAP_SET_PREVIEW, 1, 0, , ahk_id %capHwnd%
;Sleep, 3000
imagefile = %A_ScriptDir%\WebCamShot_sn%serial_num%_%A_YYYY%-%A_MM%-%A_DD%_%A_Hour%.%A_Min%.%A_Sec%.bmp
SendMessage, WM_CAP_FILE_SAVEDIB, 0, &imagefile, , ahk_id %capHwnd%
SendMessage, WM_CAP_DRIVER_DISCONNECT, 1, 0, , ahk_id %capHwnd%
SendMessage, WM_CAP_DRIVER_DISCONNECT, 0, 0, , ahk_id %capHwnd%
SendMessage, WM_CAP_DRIVER_DISCONNECT, %Driver%, 0, , ahk_id %capHwnd%
}
}
Do:=DllCall("FreeLibrary", "str", hModule)
Gui, 9:Destroy
NumShots := foundDriver * Times
Return, NumShots
}
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
}
Cap_CreateCaptureWindow(previewHwnd, 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", previewHwnd
, "Int", 0)
Return lwndC
}
;GdiPlus cuccok
;###############################################################################################################
Gdip_Startup()
{
If !DllCall("GetModuleHandle", "Str", "gdiplus")
DllCall("LoadLibrary", "Str", "gdiplus")
VarSetCapacity(si, 16, 0), si := Chr(1)
DllCall("gdiplus\GdiplusStartup", "UInt*", pToken, "UInt", &si, "UInt", 0)
VarSetCapacity(si, 0)
Return, pToken
}
;###############################################################################################################
Gdip_Shutdown(pToken)
{
DllCall("gdiplus\GdiplusShutdown", "UInt", pToken)
If hModule := DllCall("GetModuleHandle", "Str", "gdiplus")
DllCall("FreeLibrary", "UInt", hModule)
Return, 0
}
;###############################################################################################################
;
; ConvertImage() by Tariq Porter (tic) 04/05/08
; Version: 1.01
;
; sInput: Location of the input file to be converted. May be of filetype jpg,bmp,png,tiff,gif
; sOutput: Location to save the converted file. Extension determines the output filetype jpg,bmp,png,tiff,gif
; Width: Width either in pixels or percentage (depending on Method) to save the converted file
; Height: Height either in pixels or percentage (depending on Method) to save the converted file
; Width or Height may also be -1 to keep the aspect ratio the same
; Method: Can either be "Percent" or "Pixels" to determine the width and height of the converted file
;
; Return: 0=Success; -1=Could not create a bitmap from file; -2=Source file has no width or height
; -3=Resize method must be either Precentage or Pixels; -4=Error resizing image;
; -5=Could not get a list of filetype encoders on the system; -6=Could not find matching codec
; -7=Wide file output could not be created; -8=File could not be written to disk
ConvertImage(sInput, sOutput, Width="", Height="", Method="Percent")
{
StringSplit, OutputArray, sOutput, .
Extension := "." OutputArray%OutputArray0%
VarSetCapacity(wInput, 1023)
DllCall("kernel32\MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sInput, "Int", -1, "UInt", &wInput, "Int", 512)
DllCall("gdiplus\GdipCreateBitmapFromFile", "UInt", &wInput, "UInt*", pBitmap)
If !pBitmap
Return, -1
If (Width && Height)
{
DllCall("gdiplus\GdipGetImageWidth", "UInt", pBitmap, "UInt*", sWidth)
DllCall("gdiplus\GdipGetImageHeight", "UInt", pBitmap, "UInt*", sHeight)
If !(sWidth && sHeight)
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -2
}
If (Method = "Percent")
{
Width := (Width = -1) ? Height : Width, Height := (Height = -1) ? Width : Height
dWidth := Round(sWidth*(Width/100)), dHeight := Round(sHeight*(Height/100))
}
Else If (Method = "Pixels")
{
If (Width = -1)
dWidth := Round((Height/sHeight)*sWidth), dHeight := Height
Else If (Height = -1)
dHeight := Round((Width/sWidth)*sHeight), dWidth := Width
Else
dWidth := Width, dHeight := Height
}
Else
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -3
}
DllCall("gdiplus\GdipCreateBitmapFromScan0", "Int", dWidth, "Int", dHeight, "Int", 0, "Int", 0x26200A, "UInt", 0, "UInt*", FpBitmap)
DllCall("gdiplus\GdipGetImageGraphicsContext", "UInt", FpBitmap, "UInt*", G)
DllCall("gdiplus\GdipSetInterpolationMode", "UInt", G, "Int", 7)
E := DllCall("gdiplus\GdipDrawImageRectRectI", "UInt", G, "UInt", pBitmap
, "Int", 0, "Int", 0, "Int", dWidth, "Int", dHeight
, "Int", 0, "Int", 0, "Int", sWidth, "Int", sHeight
, "Int", 2, "UInt", ImageAttr, "UInt", 0, "UInt", 0)
DllCall("gdiplus\GdipDeleteGraphics", "UInt", G)
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
pBitmap := FpBitmap
If E
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -4
}
}
DllCall("gdiplus\GdipGetImageEncodersSize", "UInt*", nCount, "UInt*", nSize)
VarSetCapacity(ci, nSize)
DllCall("gdiplus\GdipGetImageEncoders", "UInt", nCount, "UInt", nSize, "UInt", &ci)
If !(nCount && nSize)
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -5
}
Loop, %nCount%
{
nSize := DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", NumGet(ci, 76*(A_Index-1)+44), "Int", -1, "UInt", 0, "Int", 0, "UInt", 0, "UInt", 0)
VarSetCapacity(sString, nSize)
DllCall("WideCharToMultiByte", "UInt", 0, "UInt", 0, "UInt", NumGet(ci, 76*(A_Index-1)+44), "Int", -1, "Str", sString, "Int", nSize, "UInt", 0, "UInt", 0)
If !InStr(sString, Extension)
Continue
pCodec := &ci+76*(A_Index-1)
Break
}
If !pCodec
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -6
}
nSize := DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sOutput, "Int", -1, "UInt", 0, "Int", 0)
VarSetCapacity(wOutput, nSize*2)
DllCall("MultiByteToWideChar", "UInt", 0, "UInt", 0, "UInt", &sOutput, "Int", -1, "UInt", &wOutput, "Int", nSize)
VarSetCapacity(wOutput, -1)
If !VarSetCapacity(wOutput)
{
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, -7
}
E := DllCall("gdiplus\GdipSaveImageToFile", "UInt", pBitmap, "UInt", &wOutput, "UInt", pCodec, "UInt", 0)
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
If E
DllCall("gdiplus\GdipDisposeImage", "UInt", pBitmap)
Return, E ? -8 : 0
}
No comments, but you'll understand the logic, I'm sure.
cheers,
fures