mirror of
https://github.com/TonyJiangWJ/Auto.js.git
synced 2026-06-21 21:01:43 +08:00
fix: images.detectsColor() and images.pixel() cause "x > image.width" exception when image is landscape
This commit is contained in:
parent
1e8183f756
commit
771511e939
@ -29,19 +29,25 @@ import java.util.concurrent.TimeUnit;
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
||||
public class ColorFinder {
|
||||
|
||||
public static Point findColorEquals(ImageWrapper imageWrapper, int color) {
|
||||
private ScreenMetrics mScreenMetrics;
|
||||
|
||||
public ColorFinder(ScreenMetrics screenMetrics) {
|
||||
mScreenMetrics = screenMetrics;
|
||||
}
|
||||
|
||||
public Point findColorEquals(ImageWrapper imageWrapper, int color) {
|
||||
return findColorEquals(imageWrapper, color, null);
|
||||
}
|
||||
|
||||
public static Point findColorEquals(ImageWrapper imageWrapper, int color, Rect region) {
|
||||
public Point findColorEquals(ImageWrapper imageWrapper, int color, Rect region) {
|
||||
return findColor(imageWrapper, color, 0, region);
|
||||
}
|
||||
|
||||
public static Point findColor(ImageWrapper imageWrapper, int color, int threshold) {
|
||||
public Point findColor(ImageWrapper imageWrapper, int color, int threshold) {
|
||||
return findColor(imageWrapper, color, threshold, null);
|
||||
}
|
||||
|
||||
public static Point findColor(ImageWrapper imageWrapper, int color, int threshold, Rect region) {
|
||||
public Point findColor(ImageWrapper imageWrapper, int color, int threshold, Rect region) {
|
||||
Point[] points = findAllColors(imageWrapper, color, threshold, region);
|
||||
if (points.length == 0) {
|
||||
return null;
|
||||
@ -49,7 +55,7 @@ public class ColorFinder {
|
||||
return points[0];
|
||||
}
|
||||
|
||||
public static Point[] findAllColors(ImageWrapper image, int color, int threshold, Rect rect) {
|
||||
public Point[] findAllColors(ImageWrapper image, int color, int threshold, Rect rect) {
|
||||
Mat bi = new Mat();
|
||||
Scalar lowerBound = new Scalar(Color.red(color) - threshold, Color.green(color) - threshold,
|
||||
Color.blue(color) - threshold, 255);
|
||||
@ -68,8 +74,8 @@ public class ColorFinder {
|
||||
Point[] points = new MatOfPoint(nonZeroPos).toArray();
|
||||
if (rect != null) {
|
||||
for (int i = 0; i < points.length; i++) {
|
||||
points[i].x += rect.x;
|
||||
points[i].y += rect.y;
|
||||
points[i].x = mScreenMetrics.scaleX((int) (points[i].x + rect.x));
|
||||
points[i].y = mScreenMetrics.scaleX((int) (points[i].y + rect.y));
|
||||
}
|
||||
}
|
||||
return points;
|
||||
|
||||
@ -53,6 +53,7 @@ public class Images {
|
||||
private Display mDisplay;
|
||||
private Image mPreCapture;
|
||||
private ImageWrapper mPreCaptureImage;
|
||||
private ScreenMetrics mScreenMetrics;
|
||||
|
||||
@ScriptVariable
|
||||
public final ColorFinder colorFinder;
|
||||
@ -62,7 +63,8 @@ public class Images {
|
||||
mScreenCaptureRequester = screenCaptureRequester;
|
||||
mContext = context;
|
||||
mDisplay = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
||||
colorFinder = new ColorFinder();
|
||||
mScreenMetrics = mScriptRuntime.getScreenMetrics();
|
||||
colorFinder = new ColorFinder(mScreenMetrics);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
@ -135,8 +137,6 @@ public class Images {
|
||||
if (image == null) {
|
||||
throw new NullPointerException("image = null");
|
||||
}
|
||||
x = ScreenMetrics.rescaleX(x, image.getWidth());
|
||||
y = ScreenMetrics.rescaleY(y, image.getHeight());
|
||||
return image.pixel(x, y);
|
||||
}
|
||||
|
||||
@ -212,8 +212,8 @@ public class Images {
|
||||
org.opencv.core.Point point = TemplateMatching.fastTemplateMatching(src, template.getMat(), TemplateMatching.MATCHING_METHOD_DEFAULT,
|
||||
weakThreshold, threshold, maxLevel);
|
||||
if (point != null && rect != null) {
|
||||
point.x += rect.x;
|
||||
point.y += rect.y;
|
||||
point.x = mScreenMetrics.scaleX((int) (point.x + rect.x));
|
||||
point.y = mScreenMetrics.scaleX((int) (point.y + rect.y));
|
||||
}
|
||||
return point;
|
||||
}
|
||||
|
||||
@ -18,7 +18,7 @@ public class ScreenMetrics {
|
||||
private static Display display;
|
||||
|
||||
public static void initIfNeeded(Activity activity) {
|
||||
if(initialized)
|
||||
if (initialized)
|
||||
return;
|
||||
DisplayMetrics metrics = new DisplayMetrics();
|
||||
activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
|
||||
@ -86,17 +86,11 @@ public class ScreenMetrics {
|
||||
}
|
||||
|
||||
public int scaleX(int x) {
|
||||
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180)
|
||||
return scaleX(x, mDesignWidth);
|
||||
else
|
||||
return scaleY(x, mDesignWidth);
|
||||
return scaleX(x, mDesignWidth);
|
||||
}
|
||||
|
||||
public int scaleY(int y) {
|
||||
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180)
|
||||
return scaleY(y, mDesignHeight);
|
||||
else
|
||||
return scaleX(y, mDesignHeight);
|
||||
return scaleY(y, mDesignHeight);
|
||||
}
|
||||
|
||||
|
||||
@ -106,17 +100,11 @@ public class ScreenMetrics {
|
||||
}
|
||||
|
||||
public int rescaleX(int x) {
|
||||
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180)
|
||||
return rescaleX(x, mDesignWidth);
|
||||
else
|
||||
return rescaleY(x, mDesignWidth);
|
||||
return rescaleX(x, mDesignWidth);
|
||||
}
|
||||
|
||||
|
||||
public int rescaleY(int y) {
|
||||
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180)
|
||||
return rescaleY(y, mDesignHeight);
|
||||
else
|
||||
return rescaleX(y, mDesignHeight);
|
||||
return rescaleY(y, mDesignHeight);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user