Sign in to follow this  
Followers 0
Michel Claveau

Capture x times a region, then re-play

1 post in this topic

#1 ·  Posted (edited)

Hi!

It is only an exemple (for POC).

3 scripts, for :

- capture a region, in several files (.BMP) (warning: need big size on disk)

- Optimize the result (delete dedondant .BMP ; convert the rest in .JPG)

- (re)-Play the capture.

The example is in french, but easy to understand.

#NoTrayIcon
#include-once
#include <WinAPI.au3>
#include <GDIPlus.au3>
#Include <Array.au3>
#include <GuiConstantsEx.au3>
#include <GuiEdit.au3>

Global Const $__SCREENCAPTURECONSTANT_SRCCOPY = 0x00CC0020
Global $giBMPFormat = $GDIP_PXF24RGB

Global $lfichiers[9999]
Global $icudir = IniRead(@ScriptDir & "\icu.ini", "Param", "folder", "L:\temp\")
Global $icufilename = $icudir & "c"
Global $icunum=0
Global $icuX = IniRead(@ScriptDir & "\icu.ini", "Param", "x",100)
Global $icuY = IniRead(@ScriptDir & "\icu.ini", "Param", "y",20)
Global $icuLarg = IniRead(@ScriptDir & "\icu.ini", "Param", "w",800)
Global $icuHaut = IniRead(@ScriptDir & "\icu.ini", "Param", "h",500)
Global $intervalle = IniRead(@ScriptDir & "\icu.ini", "Param", "intervalle",250)
Global $duree = IniRead(@ScriptDir & "\icu.ini", "Param", "dureeMaxi",60)

$gui=GUICreate("ICU - Paramètres",400,390)
GUISetFont(10,600,-1,"Arial")
$pas=30
$v=15
$v+=$pas
$h=170
$l1=GUICtrlCreateLabel("Dossier (folder) de travail:",10,$v+3,155)
$chdoss=GUICtrlCreateInput($icudir,$h,$v,200)  
$v+=$pas
$l2=GUICtrlCreateLabel("Région, début X:",10,$v+3,135)
$chx=GUICtrlCreateInput($icuX,$h,$v,200)
$v+=$pas
$l3=GUICtrlCreateLabel("Région, début Y:",10,$v+3,135)
$chy=GUICtrlCreateInput($icuY,$h,$v,200)
$v+=$pas
$l4=GUICtrlCreateLabel("Région, Largeur:",10,$v+3,135)
$chw=GUICtrlCreateInput($icuLarg,$h,$v,200)
$v+=$pas
$l5=GUICtrlCreateLabel("Région, Hauteur:",10,$v+3,135)
$chh=GUICtrlCreateInput($icuHaut,$h,$v,200)
$v+=$pas
$l6=GUICtrlCreateLabel("Intervalle de capture (ms):",10,$v+3,155)
$chi=GUICtrlCreateInput($intervalle,$h,$v,200)
$v+=$pas
$l7=GUICtrlCreateLabel("Durée Maximale (s):",10,$v+3,135)
$chdm=GUICtrlCreateInput($duree,$h,$v,200)

$l8=GUICtrlCreateLabel("Rappel :    {Ctrl} ²   pour terminer la capture",50,300,380)
GUICtrlSetColor(-1,0xAA0000)

GUICtrlSetFont($l1,10,400,-1,"Arial")
GUICtrlSetFont($l2,10,400,-1,"Arial")
GUICtrlSetFont($l3,10,400,-1,"Arial")
GUICtrlSetFont($l4,10,400,-1,"Arial")
GUICtrlSetFont($l5,10,400,-1,"Arial")
GUICtrlSetFont($l6,10,400,-1,"Arial")
GUICtrlSetFont($l7,10,400,-1,"Arial")


$btok=GUICtrlCreateButton("OK",50,350,100,30)
$btesc=GUICtrlCreateButton("Abandon",250,350,100,30)

GUISetState()

While 1
$csmsg = GUIGetMsg()
If ($csmsg = $GUI_EVENT_CLOSE) Or ($csmsg = $btesc) Then
  ExitLoop
EndIf

If $csmsg = $btok Then
  $icudir=GUICtrlRead($chdoss)
  $icuX=GUICtrlRead($chx)
  $icuY=GUICtrlRead($chy)
  $icuLarg=GUICtrlRead($chw)
  $icuHaut=GUICtrlRead($chh)
  $intervalle=GUICtrlRead($chi)
  $duree=GUICtrlRead($chdm)
  ExitLoop
EndIf
WEnd
GUIDelete($gui)
If ($csmsg = $GUI_EVENT_CLOSE) Or ($csmsg = $btesc) Then
Exit
EndIf


$duree=$duree*1000

If Not FileExists($icudir) Then
DirCreate($icudir)
EndIf

ecrit_ini()

Func ecrit_ini()
IniWrite(@ScriptDir & "\icu.ini", "Param", "folder", $icudir)
IniWrite(@ScriptDir & "\icu.ini", "Param", "x", $icuX)
IniWrite(@ScriptDir & "\icu.ini", "Param", "y", $icuY)
IniWrite(@ScriptDir & "\icu.ini", "Param", "w", $icuLarg)
IniWrite(@ScriptDir & "\icu.ini", "Param", "h", $icuHaut)
IniWrite(@ScriptDir & "\icu.ini", "Param", "intervalle", $intervalle)
If $duree>60000 Then
  $duree=60000
EndIf
IniWrite(@ScriptDir & "\icu.ini", "Param", "dureeMaxi", ($duree/1000))
EndFunc ;==>ecrit_ini


Global $icudesktop,$icuDDC,$icuCDC,$icuBMP,$icuCLSID,$icuhImage,$icupGUID, $icutGUID,$icubufferimage,$icubufferimage2
Global $BitmapData0,$BitmapData1,$icuScan0,$icuScan1,$icusize0,$ptr0,$ptr1
Global $postbat

$postbat="CD /D"& $icudir & @CRLF

HotKeySet("^²","fin")

fastcaptprepar()
$init=TimerInit()
$debut=TimerInit()
$temps=$intervalle
While 1
If TimerDiff($debut)>=$temps Then
  fastcapt()
  $temps += $intervalle
Else
  sleep(10)
EndIf
If TimerDiff($init)>$duree Then ExitLoop
WEnd
$fin=TimerDiff($debut)
fastcaptclose()
_GDIPlus_Shutdown()
MsgBox(0,"Nb fichiers",$icunum,2)

$f=FileOpen($icudir &"nbfichiers.txt",2)
FileWrite($f,String($icunum))
FileClose($f)

Exit




Func fin()
$duree=1
HotKeySet("^²")
EndFunc




Func fastcaptprepar()
$icudesktop = _WinAPI_GetDesktopWindow()
$icuDDC = _WinAPI_GetDC($icudesktop)
$icuCDC = _WinAPI_CreateCompatibleDC($icuDDC)
$icuBMP = _WinAPI_CreateCompatibleBitmap($icuDDC, $icuLarg, $icuHaut)
_GDIPlus_Startup()
$icuCLSID = _GDIPlus_EncodersGetCLSID("BMP")
$icutGUID = _WinAPI_GUIDFromString($icuCLSID)
$icupGUID = DllStructGetPtr($icutGUID)
$icubufferimage=""
EndFunc



Func fastcaptclose()
_GDIPlus_BitmapUnlockBits($icubufferimage, $BitmapData0)
_GDIPlus_ImageDispose($icuhImage)
_WinAPI_DeleteObject($icuBMP)
_WinAPI_ReleaseDC($icudesktop, $icuDDC)
_WinAPI_DeleteDC($icuCDC)
_WinAPI_DeleteObject($icuBMP)
_GDIPlus_Shutdown()
sleep(250)
$postbat &= "exit" & @CRLF
$f=FileOpen($icudir & "deldoublon.bat",2)
FileWrite($f,$postbat)
FileClose($f)
EndFunc



Func fastcapt()
_WinAPI_SelectObject($icuCDC, $icuBMP)
_WinAPI_BitBlt($icuCDC, 0, 0, $icuLarg, $icuHaut, $icuDDC, $icuX, $icuY, $__SCREENCAPTURECONSTANT_SRCCOPY)
#cs
$aCursor = _WinAPI_GetCursorInfo()
If $aCursor[1] Then
  $hIcon = _WinAPI_CopyIcon($aCursor[2])
  $aIcon = _WinAPI_GetIconInfo($hIcon)
EndIf
#ce
$icunum+=1
$lfichiers[$icunum]=$icufilename & (10000+$icunum) &'.bmp'
$aResult = DllCall($ghGDIPDll, "int", "GdipCreateBitmapFromHBITMAP", "hwnd", $icuBMP, "hwnd", 0, "int*", 0)
$icuhImage = $aResult[3]

DllCall($ghGDIPDll, "int", "GdipSaveImageToFile", "hwnd", $icuhImage, "wstr", $lfichiers[$icunum], "ptr", $icupGUID, "ptr", 0) 
EndFunc

#NoTrayIcon
#include-once
#include <WinAPI.au3>
#include <GDIPlus.au3>
#Include <Array.au3>

Global $icufilename
Global Const $__SCREENCAPTURECONSTANT_SRCCOPY = 0x00CC0020
Global $giBMPFormat = $GDIP_PXF24RGB

Global $lfichiers[9999]

Global $icudir = IniRead(@ScriptDir & "\icu.ini", "Param", "folder", "L:\temp\")
Global $icufilename = $icudir & "c"
Global $icunum=0
Global $icuX = IniRead(@ScriptDir & "\icu.ini", "Param", "x",100)
Global $icuY = IniRead(@ScriptDir & "\icu.ini", "Param", "y",20)
Global $icuLarg = IniRead(@ScriptDir & "\icu.ini", "Param", "w",800)
Global $icuHaut = IniRead(@ScriptDir & "\icu.ini", "Param", "h",500)
Global $intervalle = IniRead(@ScriptDir & "\icu.ini", "Param", "intervalle",100)
Global $duree = IniRead(@ScriptDir & "\icu.ini", "Param", "dureeMaxi",60)


Global $icudesktop,$icuDDC,$icuCDC,$icuBMP,$icuCLSID,$icuhImage,$icupGUID, $icutGUID,$icubufferimage,$icubufferimage2
Global $BitmapData0,$BitmapData1,$icuScan0,$icuScan1,$icusize0,$ptr0,$ptr1
Global $postbat

$postbat="CD /D"& $icudir & @CRLF


$f=FileOpen($icudir &"nbfichiers.txt",0)
$icunum=FileRead($f)
FileClose($f)


SplashTextOn("Optimisation","En cours; veuillez patienter...",260,80)

;----------Partie optimisation 1---------------------------------
_GDIPlus_Startup()
$ibuffer = _GDIPlus_ImageLoadFromFile($icufilename & '10001.bmp')
$numbuffer=1
sleep(12)
$ibuffer2 = _GDIPlus_ImageLoadFromFile($icufilename & '10002.bmp')
$numbuffer2=2
sleep(12)
$batch="CD /D "& $icudir & @CRLF
For $i=3 To $icunum
$fichier = $icufilename & (10000+$i) &'.bmp'
$image = _GDIPlus_ImageLoadFromFile($fichier)
sleep(12)
$tmp=CompareBitmaps($image, $ibuffer)
If $tmp Then
  $batch &= "del /Q "& $fichier & @CRLF
  $lfichiers[$i]="-1"
Else
  $tmp=CompareBitmaps($image, $ibuffer2)
  If $tmp Then
   $batch &= "del /Q "& $fichier & @CRLF
   $lfichiers[$i]="-2"
  Else
   $jpgimage = _GDIPlus_ImageLoadFromFile($fichier)
   $gdiCLSID = _GDIPlus_EncodersGetCLSID("JPG")
   _GDIPlus_ImageSaveToFile($jpgimage, $icufilename & (10000+$i) &'.jpg')
   Sleep(12)
   _GDIPlus_ImageDispose($jpgimage)

   If $numbuffer<$numbuffer2 Then
    $ibuffer = _GDIPlus_ImageLoadFromFile($fichier)
    $numbuffer=$i
    $lfichiers[$i]=1
   Else
    $ibuffer2 = _GDIPlus_ImageLoadFromFile($fichier)
    $numbuffer2=$i
    $lfichiers[$i]=2
   EndIf
  EndIf
EndIf
Next
_GDIPlus_ImageDispose($image)
_GDIPlus_ImageDispose($ibuffer)
_GDIPlus_ImageDispose($ibuffer2)
_GDIPlus_Shutdown()
$lfichiers[1]=1
$lfichiers[2]=2

$f=FileOpen($icudir & "deldoublon.bat",2)
FileWrite($f,"CD /D "& $icudir & @CRLF &"tasklist"& @CRLF &"del /Q *.bmp"& @CRLF)
FileClose($f)
sleep(250)
Run($icudir &"deldoublon.bat")
sleep(250)
$f=FileOpen($icudir &"listefichiers.txt",2)
FileWrite($f,_ArrayToString($lfichiers,'|'))
FileClose($f)
SplashOff()
Sleep(250)
Run("cmd /c" & $icudir &"deldoublon.bat",$icudir)
Exit





Func CompareBitmaps($bm1, $bm2)
    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")
    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4

    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")

    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4

    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)

    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)

    Return ($call[0]=0)
EndFunc

#include-once
#include <WinAPI.au3>
#include <GDIPlus.au3>
#Include <Array.au3>

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $lfichiers[9999]
Global $icudir = IniRead(@ScriptDir & "\icu.ini", "Param", "folder", "L:\temp\")
Global $icufilename = $icudir & "c"
Global $icunum=0
Global $icuX = IniRead(@ScriptDir & "\icu.ini", "Param", "x",100)
Global $icuY = IniRead(@ScriptDir & "\icu.ini", "Param", "y",20)
Global $icuLarg = IniRead(@ScriptDir & "\icu.ini", "Param", "w",800)
Global $icuHaut = IniRead(@ScriptDir & "\icu.ini", "Param", "h",500)
Global $intervalle = IniRead(@ScriptDir & "\icu.ini", "Param", "intervalle",100)
Global $duree = IniRead(@ScriptDir & "\icu.ini", "Param", "dureeMaxi",60)
$duree=$duree*1000

$f=FileOpen($icudir &"listefichiers.txt",0)
$tmp=FileRead($f)
$lfichiers=StringSplit($tmp,'|')
FileClose($f)
_ArrayDelete($lfichiers,1)

$f=FileOpen($icudir &"nbfichiers.txt",0)
$icunum=FileRead($f)
FileClose($f)

$gui=GUICreate("ICU-Player                {Echap} ou {Esc} pour abandonner. ",$icuLarg,$icuHaut,-1,-1,-1)
$fichier = $icufilename & (10000+1) &'.jpg'
$image=GUICtrlCreatePic($fichier,0,0,$icuLarg,$icuHaut,-1)
GUISetState()

HotKeySet("{ESC}","fin")


_GDIPlus_StartUp()
$gdiCLSID = _GDIPlus_EncodersGetCLSID("JPG")

$init=TimerInit()
$num=0
$debut=TimerInit()
$temps=$intervalle

While 1
$num+=1
If $num>$icunum Then ExitLoop
$fichier = $icufilename & (10000+$num) &'.jpg'
If $lfichiers[$num]=1 Then  $buffer1=$fichier
If $lfichiers[$num]=2 Then  $buffer2=$fichier
If $lfichiers[$num]=-1 Then  $fichier=$buffer1
If $lfichiers[$num]=-2 Then  $fichier=$buffer2

$gdimage=_GDIPlus_ImageLoadFromFile($fichier)
$gdih = _GDIPlus_GraphicsCreateFromHWND($gui)
_WinAPI_RedrawWindow($image, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($gdih, $gdimage, 0, 0)
    _WinAPI_RedrawWindow($image, 0, 0, $RDW_VALIDATE)
_GDIPlus_ImageDispose($gdimage)
If TimerDiff($debut)<$temps Then
  Sleep($temps-TimerDiff($debut))
EndIf 
$temps += $intervalle
WEnd
_GDIPlus_ShutDown ()
MsgBox(0,"Terminé","   Durée :    "& Int(TimerDiff($init)/100)/10 &" s")
Exit





Func fin()
HotKeySet("{ESC}")
$icunum=1
EndFunc

icucapture.au3

icuoptimize.au3

icuplayer.au3

Edited by Michel Claveau

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
Sign in to follow this  
Followers 0