Cache images to improve detection speed and reduce memory consumption
// First we need to explain how the detection works under the hood
// Take this code for example (the same applies to UI)
var region = Region(100, 100, 200, 500) //create the region
var result1 = region.find("button_1") //find button_1 in the region
var result2 = region.find("button_2") //find button_2 in the region
if (result1) { } else { } //check the result 1
if (result2) { } else { } //check the result 2
// When the 2nd line is executed
// The screen is first captured, you get a full screen image
// Then the full screen image will be cropped to the region dimension and scale depend on compareWidth
// Then the button_1 image will be read from disk and cached (default behavior), it is then searched in the cropped region image
// When the 3rd line is executed, the same process is applied again
// This is not efficient. The time between the 2nd and 3rd line is very small. We don't need to capture a new image for the 3rd
// Imagine if we have 10 of those find instead of just 2 then it's even worse.
// We can improve this by using Cache
Cache.screen() //turn on full screen cache
var region = Region(100, 100, 200, 500) //create the region
var result1 = region.find("button_1") //find button_1 in the region, full screen image is now cached
var result2 = region.find("button_2") //find button_2 in the region, this will use the cached version instead of capturing new image
if (result1) { } else { } //check the result 1
if (result2) { } else { } //check the result 2
Cache.screenOff() //turn off full screen cache as soon as you're done
// Notice that we have the same region for two find call
// We can cached the cropped region result instead of cropping it every time
Cache.screen() //turn on full screen cache
Cache.region() //turn on region cache
var region = Region(100, 100, 200, 500) //create the region
var result1 = region.find("button_1") //find button_1 in the region, full screen image is now cached, cropped region also cached
var result2 = region.find("button_2") //find button_2 in the region, this will use the cached version and skip the cropping process
if (result1) { } else { } //check the result 1
if (result2) { } else { } //check the result 2
Cache.screenOff() //turn off full screen cache as soon as you're done
Cache.regionOff() //turn off region cache as soon as you're done
// When the template image is loaded, it's automatically cached because template is reuse a lot.
// Hitting the disk every time is not efficient but this means the cache will build up over time
// You can use clearImage method to clear a specific image cache to free memory when you know you don't need it anymore
Cache.screen() //turn on full screen cache
Cache.region() //turn on region cache
var region = Region(100, 100, 200, 500) //create the region
var result1 = region.find("button_1") //find button_1 in the region, full screen image is now cached, cropped region also cached
var result2 = region.find("button_2") //find button_2 in the region, this will use the cached version and skip the cropping process
Cache.clearImage("button_2") //clear button_2 cache only, assuming we don't need to find button_2 anymore
if (result1) { } else { } //check the result 1
if (result2) { } else { } //check the result 2
Cache.screenOff() //turn off full screen cache as soon as you're done
Cache.regionOff() //turn off region cache as soon as you're done
// The same applies to the Color.get and Color.getAll methods as well
// Cache properly will increase performance a lot especially with native service where the cost of getting new image is higher than media projection
// We're trading memory for performance here so you should clear all full screen cache and region cache as soon as possible
// Clear template cache properly will also reduce the memory consumption while running
static void screen()
Cache the next full screen capture. All image detection will now use the cached version and will not capture new image
static void screenOff()
Disable full screen cache and clear the previous cached image if any. You should call this as soon as you are done with the detection. Leaving a full screen image data in memory is not good
static void screenRefresh()
Convenient method that call screen() after screenOff()
static void region()
Cache all cropped region image. Region with the same dimension will use the previous cached image of the same dimension
static void screenOff()
Disable region cache and clear all previous cached region images if any
static void regionRefresh()
Convenient method that call region() after regionOff()
static void clearImage(imageName: string)
Clear the cached template image by name. Pass null or no argument to clear all cached images
Parameters | |
imageName | The template imageName name |
static void clearRegion(region: Region)
Clear the cached region image. The dimension of the region is used so you don't need to use the same reference. Pass null or no argument to clear all cached images
Parameters | |
region | The Region |
static void loadColor(color: number | string | Color, name: string = null)
Load a color into the cache as a 1px image that you can use with any image detection methods
Parameters | |
color | The color as number, hex string or Color object instance |
name |
Custom name to put into the Cache. If null then it'll use the return string from color.valueString() method. Careful not to use the same name as other template cause this will override them.
|
static void loadVars()
Reload all the cached variables in the UI (point, region, etc...). Use this after you changed anything related to the macro's dimension.
© 2024 - Macrorify by KoK-CODE