Skip to content

Commit c5351ba

Browse files
committed
screen.find(): offer to scale the needle
When capturing a screen region in a recent Windows version on a high-DPI device, the captured image can have a much higher resolution than scale-unaware applications such as `node.exe` might think (compare also nut-tree/libnut-core#52). For convenience, let's support scaling the "needle" image that `nut.js` is told to find. For example, on my laptop, where the scale factor is 200%, I would capture a rectangular region, save it as a `.png` file, then tell `nut.js` to find it thusly: await screen.find('needle.png', 10000, { scaleNeedle: true }) Signed-off-by: Johannes Schindelin <[email protected]>
1 parent dcaed48 commit c5351ba

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

lib/match-request.class.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ export class MatchRequest {
88
public readonly searchRegion: Region,
99
public readonly confidence: number,
1010
public readonly searchMultipleScales: boolean = true,
11+
public readonly scaleNeedle: number = 1.0,
1112
) {}
1213
}

lib/optionalsearchparameters.class.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export class OptionalSearchParameters {
1010
* @param confidence Optional confidence value to configure image match confidence
1111
* @param searchMultipleScales Optional flag to indicate if the search should be conducted at different scales
1212
*/
13-
constructor(public searchRegion?: Region, public confidence?: number, public searchMultipleScales?: boolean) {}
13+
constructor(public searchRegion?: Region, public confidence?: number, public searchMultipleScales?: boolean, public scaleNeedle?: number) {}
1414
}

lib/provider/opencv/template-matching-finder.class.ts

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export default class TemplateMatchingFinder implements FinderInterface {
9797
`Failed to load ${matchRequest.pathToNeedle}, got empty image.`,
9898
);
9999
}
100+
if (matchRequest.scaleNeedle !== 1.0)
101+
needle = await scaleImage(needle, matchRequest.scaleNeedle);
102+
100103
const haystack = await loadHaystack(matchRequest);
101104

102105
if (debug) {

lib/screen.class.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class Screen {
8787
const searchRegion =
8888
(params && params.searchRegion) || await this.vision.screenSize();
8989
const searchMultipleScales = (params && params.searchMultipleScales)
90+
const scaleNeedle = (params && params.scaleNeedle) || 1.0;
9091

9192
const fullPathToNeedle = normalize(join(this.config.resourceDirectory, templateImageFilename));
9293

@@ -97,7 +98,8 @@ export class Screen {
9798
fullPathToNeedle,
9899
searchRegion,
99100
minMatch,
100-
searchMultipleScales
101+
searchMultipleScales,
102+
scaleNeedle
101103
);
102104

103105
return new Promise<Region>(async (resolve, reject) => {

0 commit comments

Comments
 (0)