6.6.3 - Alpha3 - 新增 images.detectMultiColors 方法 (issue #374)

This commit is contained in:
SuperMonster003
2025-05-10 11:10:30 +08:00
parent c2b98af9d5
commit 94122476fa
6 changed files with 39 additions and 16 deletions

View File

@@ -135,7 +135,6 @@ public class ColorFinder {
@Deprecated
@ScriptInterface
@SuppressWarnings("deprecation")
@CodeAuthor(name = "LYS86", homepage = "https://github.com/LYS86")
public Point[] findAllMultiColors(ImageWrapper image, int firstColor, int threshold, Rect rect, int[] points) {
Point[] firstPoints = findAllPointsForColor(image, firstColor, threshold, rect);
@@ -151,14 +150,23 @@ public class ColorFinder {
return resultPoints.toArray(new Point[0]);
}
public boolean detectMultiColors(ImageWrapper image, int x, int y, int firstColor, int threshold, Rect region, int[] points) {
int pixel = image.pixel(x, y);
ColorDetector colorDetector = new ColorDetector.DifferenceDetector(firstColor, threshold);
if (!colorDetector.detectColor(Color.red(pixel), Color.green(pixel), Color.blue(pixel))) {
return false;
}
return checksPath(image, new Point(x, y), threshold, points);
}
private boolean checksPath(ImageWrapper image, Point startingPoint, int threshold, int[] points) {
for (int i = 0; i < points.length; i += 3) {
int x = points[i];
int y = points[i + 1];
int color = points[i + 2];
ColorDetector colorDetector = new ColorDetector.DifferenceDetector(color, threshold);
x += startingPoint.x;
y += startingPoint.y;
x += (int) startingPoint.x;
y += (int) startingPoint.y;
if (x >= image.getWidth() || y >= image.getHeight() || x < 0 || y < 0) {
return false;
}
@@ -173,7 +181,6 @@ public class ColorFinder {
@Nullable
@Deprecated
@ScriptInterface
@SuppressWarnings("deprecation")
public Point findColorEquals(ImageWrapper imageWrapper, int color) {
return findColorEquals(imageWrapper, color, null);
}
@@ -181,7 +188,6 @@ public class ColorFinder {
@Nullable
@Deprecated
@ScriptInterface
@SuppressWarnings("deprecation")
public Point findColorEquals(ImageWrapper imageWrapper, int color, Rect region) {
return findColor(imageWrapper, color, 0, region);
}

View File

@@ -40,8 +40,8 @@ public class RhinoColorFinder extends ColorFinder {
return new Rect(x, y, width, height);
}
throw new IllegalArgumentException("out of region: " +
"region = [" + Arrays.toString(new Integer[]{x, y, width, height}) + "], " +
"image.size = [" + imageWrapper.getWidth() + ", " + imageWrapper.getHeight() + ']');
"region = [" + Arrays.toString(new Integer[]{x, y, width, height}) + "], " +
"image.size = [" + imageWrapper.getWidth() + ", " + imageWrapper.getHeight() + ']');
}
private Object getOrNull(NativeArray nativeArray, int index) {

View File

@@ -613,7 +613,7 @@ class Images(scriptRuntime: ScriptRuntime) : Augmentable(scriptRuntime), AsEmitt
initOpenCvIfNeeded()
scriptRuntime.images.flip(image, horizontal, vertical)
}
private fun parseFlipOrientations(o: Any?): Pair<Boolean, Boolean> = when {
o.isJsNullish() -> false to false
o is Boolean -> o to false
@@ -677,13 +677,28 @@ class Images(scriptRuntime: ScriptRuntime) : Augmentable(scriptRuntime), AsEmitt
detectColor(scriptRuntime, it)
}
// @Reference to module __images__.js from Auto.js Pro 9.3.11 by SuperMonster003 on Dec 19. 2023.
// @Reference to module __images__.js from Auto.js Pro 9.3.11 by SuperMonster003 on May 10. 2025.
@JvmStatic
@RhinoRuntimeFunctionInterface
fun detectMultiColors(scriptRuntime: ScriptRuntime, args: Array<out Any?>): Boolean = ensureArgumentsLengthInRange(args, 5..6) {
TODO("detectMultiColors 尚未实现")
val (o, x, y, firstColor, paths, options) = it
val image = if (o is String) read(scriptRuntime, arrayOf<Any>(o, true)) else o
require(image is ImageWrapper) { "Argument image for images.detectMultiColors must be a ImageWrapper" }
require(paths is NativeArray) { "Argument paths for images.detectMultiColors must be a JavaScript Array" }
initOpenCvIfNeeded()
val opt = options as? NativeObject ?: newNativeObject()
scriptRuntime.images.colorFinder.detectMultiColors(
image,
coerceIntNumber(x),
coerceIntNumber(y),
Colors.toIntRhino(firstColor),
parseThreshold(opt).roundToInt(),
opt.inquire("region") { region -> buildRegionInternal(image, region) },
paths.flatMap { path ->
val (px, py, color) = path as NativeArray
listOf(coerceIntNumber(px), coerceIntNumber(py), Colors.toIntRhino(color))
}.toIntArray(),
)
}
@Deprecated("Deprecated in Java", ReplaceWith("detectMultiColors(image, x, y, firstColor, paths, options)"))

View File

@@ -539,6 +539,7 @@ public class BuildActivity extends BaseActivity implements ApkBuilder.ProgressCa
checkBox.setAlpha(0.87f);
checkBox.setText(permission + "\n" + getString(descriptionResId));
checkBox.setButtonDrawable(R.drawable.round_checkbox);
checkBox.setBackground(null);
checkBox.setGravity(Gravity.CENTER_VERTICAL);
checkBox.setTextSize(12);
int marginInPixels = (int) (8 * getResources().getDisplayMetrics().density);