diff --git a/.changelog/lang_zh-Hans.json b/.changelog/lang_zh-Hans.json index f09a437f..809a93d9 100644 --- a/.changelog/lang_zh-Hans.json +++ b/.changelog/lang_zh-Hans.json @@ -14,6 +14,7 @@ "ScriptRuntime 使用 require 引用内置模块时可能出现的同步状态异常 (试修) _[`issue #298`](http://issues.autojs6.com/298)_", "notice 模块缺失 getBuilder 等扩展方法的问题 _[`issue #301`](http://issues.autojs6.com/301)_", "shizuku/shell 等方法无法接受字符串参数的问题 _[`issue #310`](http://issues.autojs6.com/310)_", + "colors.pixel 无法接受单通道图像参数的问题 _[`issue #350`](http://issues.autojs6.com/350)_", "engines.execScript/execScriptFile 等方法执行脚本时默认工作路径异常", "floaty.window/floaty.rawWindow 无法在子线程执行的问题", "ui.inflate 返回值丢失 attr/on/click 等原型方法的问题", diff --git a/app/src/main/java/org/autojs/autojs/core/image/ImageWrapper.kt b/app/src/main/java/org/autojs/autojs/core/image/ImageWrapper.kt index 988b6e46..c5c5b1cc 100644 --- a/app/src/main/java/org/autojs/autojs/core/image/ImageWrapper.kt +++ b/app/src/main/java/org/autojs/autojs/core/image/ImageWrapper.kt @@ -194,8 +194,45 @@ open class ImageWrapper : Recyclable, MonitorResource { // ! 注意 `org.opencv.core.Mat.get(row, col)` 方法参数, "row" 对应 "y", "col" 对应 "x". val pixelList = oMat.get(/* row = */ y, /* col = */ x) ?: throw Exception("Channel list is null at ($x, $y) of $this") - val (r, g, b, a) = pixelList.map { pixelValue -> pixelValue.toInt() } - return Color.argb(a, r, g, b) + // @Hint by SuperMonster003 on Apr 9, 2025. + // ! Here we assume that pixelList contains ARGB channels. + // ! If mMat does not have these channels, an IndexOutOfBoundsException will be triggered: + // # java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 + // ! Therefore, it is necessary to consider cases with different numbers of channels. + // ! + // ! Refer to: http://issues.autojs6.com/350 + // ! + // ! zh-CN: + // ! 此处默认 pixelList 为 ARGB 通道, 当 mMat 不是上述通道时, 将触发索引越界异常: + // # java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 + // ! 因此需要考虑不同通道数量的情况. + // ! + // ! 参阅: http://issues.autojs6.com/350 + // ! + // # val (r, g, b, a) = pixelList.map { pixelValue -> pixelValue.toInt() } + // # return Color.argb(a, r, g, b) + + return when (pixelList.size) { + 1 -> { + // Single-channel image (grayscale/binary/...) + // zh-CN: 单通道图像 (灰度图/二值图/...) + val value = pixelList[0].toInt() + Color.argb(255, value, value, value) + } + 3 -> { + // RGB + val (r, g, b) = pixelList.map { it.toInt() } + Color.argb(255, r, g, b) + } + in 4..Int.MAX_VALUE -> { + // RGBA + val (r, g, b, a) = pixelList.map { it.toInt() } + Color.argb(a, r, g, b) + } + else -> { + throw Exception("Unsupported pixel channel count (${pixelList.size}) at ($x, $y) of $this") + } + } } plane?.let { oPlane -> val buffer = oPlane.buffer.apply { position(0) }