fix: images.detectsColor() and images.pixel() cause "x > image.width" exception when image is landscape
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user