Detect screen pixel change with error threshold Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
william_ahk
Posts: 494
Joined: 03 Dec 2018, 20:02

Detect screen pixel change with error threshold

23 Oct 2020, 19:42

So currently I've got this script found somewhere on the internet and I wonder if it is possible to allow an error threshold for pixel changes. For example only >=30% of pixel change will return true for HasChanged. I want to capture slideshows in videos and there are noises so I need to cancel them.

Code: Select all

#include Gdip_All.ahk
global switch := false

F1::
    switch := !switch
    if (switch) {
        SetTimer, watcher, 500
    } else {
        SetTimer, watcher, off
    }
return 

watcher()
{
    if (switch) {
        WinGetPos, X, Y, W, H, A
        promise := new Snap(0, 0, W, H)
        if (promise.HasChanged) {
            Send {PrintScreen}
        }
        tooltip_fn((promise.HasChanged ? "Something" : "Nothing") " has changed.")
    }
}

class Snap
{
    static pToken := Gdip_Startup()
    static hModule := DllCall("LoadLibrary", "Str", "ucrtbase.dll", "Ptr")
    
    __New(x1, y1, x2, y2)
    {
        this.x := x1
        this.y := y1
        this.w := x2 - x1
        this.h := y2 - y1
        
        this.Original := Bitmap.CreateLocked(this.x, this.y, this.w, this.h)
    }
    
    HasChanged[] {
        get {
            Current := Bitmap.CreateLocked(this.x, this.y, this.w, this.h)
            return DllCall("ucrtbase\memcmp"
            , "Ptr", this.Original.scan0
            , "Ptr", Current.scan0
            , "Ptr", Current.countBytes)
        }
    }
}

class Bitmap
{
    __New(x, y, w, h)
    {
        this.ptr := Gdip_BitmapFromScreen(x "|" y "|" w "|" h)
        this.w := w
        this.h := h
    }
    
    __Delete()
    {
        this.UnlockBits()
        Gdip_DisposeImage(this.ptr)
    }
    
    CreateLocked(x, y, w, h)
    {
        Bmp := new Bitmap(x, y, w, h)
        Bmp.LockBits()
        
        return Bmp
    }
    
    LockBits()
    {
        if this.bmData
            throw Exception("Already locked.", -1)
        
        Gdip_LockBits(this.ptr, 0, 0, this.w, this.h, stride, scan0, bmData)
        this.scan0 := scan0
        this.bmData := bmData
        this.countBytes := this.w * stride
    }
    
    UnlockBits()
    {
        if !this.bmData
            throw Exception("Wasn't locked beforehand.", -1)
        
        Gdip_UnlockBits(this.ptr, this.bmData)
        this.bmData := ""
    }
}

tooltip_fn(input)
{
    ToolTip % input
    SetTimer, rm_tooltip, -1000
}

rm_tooltip()
{
    ToolTip
    return
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Joey5, JPMuir, matt101, Thorlian and 175 guests