From 97011d3794763958b2539640dda2c682813b0136 Mon Sep 17 00:00:00 2001 From: SuperMonster003 Date: Thu, 10 Apr 2025 00:33:45 +0800 Subject: [PATCH] =?UTF-8?q?6.6.2=20-=20Alpha5=20-=20=E4=BF=AE=E5=A4=8D=20c?= =?UTF-8?q?olors.pixel=20=E6=97=A0=E6=B3=95=E6=8E=A5=E5=8F=97=E5=8D=95?= =?UTF-8?q?=E9=80=9A=E9=81=93=E5=9B=BE=E5=83=8F=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20(issue=20#350)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changelog/lang_zh-Hans.json | 1 + .../autojs/autojs/core/image/ImageWrapper.kt | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) 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) }