Jump to content

Compare Images and alert the user when matched


Recommended Posts

I currently have this script which scans and records the screen and compares images2 to image1(Original image). If image2 don't match it'll say false and if they're exactly the same it'll say true. This is intended for a software gas modeling tool at work on a laptop( while it's processing for hours). Would like to have it loop until it matches original image and alert the engineer with some sort of sound. Does windows have default sounds to use for this?

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPIFiles.au3>

_GDIPlus_Startup()

scanImage2()

$fname1 = @ScriptDir & "/Image1.jpg"
$fname2 = @ScriptDir & "/Image2.jpg"

$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
$bm2 = _GDIPlus_ImageLoadFromFile($fname2)
MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2),2)

_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()

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  ;==>CompareBitmaps

Func scanImage2()
$hWnd1 = WinGetHandle("")
WinActivate($hWnd1)
$hWnd = WinGetHandle($hWnd1)
Sleep(500)
$SsTake = _ScreenCapture_CaptureWnd("", $hWnd)
_ScreenCapture_SaveImage(@ScriptDir & "/Image2.jpg", $SsTake)
EndFunc

 

Link to comment
Share on other sites

Thanks alepha01. The alarm part seems to work pretty good. Now how would I setup a while loop to repeat scanImage2 function until image2 is completely the same image1. Image1 is a pre-stored image inside a file that reflects the popup handler.

#include <GDIPlus.au3>
#include <ScreenCapture.au3>
#include <WinAPIFiles.au3>

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

_GDIPlus_Startup()

scanImage2()
Sleep(500)

$fname1 = @ScriptDir & "/Image1.jpg"
$fname2 = @ScriptDir & "/Image2.jpg"

$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
$bm2 = _GDIPlus_ImageLoadFromFile($fname2)
MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2), 2)

$msgBox = MsgBox(4, "Update Status", "Model Completed",10)
Do
    SoundPlay("C:\WINDOWS\media\notify.wav", 1)
Until $msgBox = 6


_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()


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   ;==>CompareBitmaps

Func scanImage1()
    $hWnd1 = WinGetHandle("")
    WinActivate($hWnd1)
    $hWnd = WinGetHandle($hWnd1)
    Sleep(500)
    $SsTake = _ScreenCapture_CaptureWnd("", $hWnd)
    _ScreenCapture_SaveImage(@ScriptDir & "/Image1.jpg", $SsTake)
EndFunc   ;==>scanImage1

Func scanImage2()
    $hWnd1 = WinGetHandle("")
    WinActivate($hWnd1)
    $hWnd = WinGetHandle($hWnd1)
    Sleep(500)
    $SsTake = _ScreenCapture_CaptureWnd("", $hWnd)
    _ScreenCapture_SaveImage(@ScriptDir & "/Image2.jpg", $SsTake)
EndFunc   ;==>scanImage2

Func Terminate()
    Exit
EndFunc   ;==>Terminate

 

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