简体   繁体   English

在 Kotlin 中使用 setRGB() 需要 IntArray

[英]Using setRGB() required IntArray in Kotlin

I want to change color of a pixel in Kotlin.我想在 Kotlin 中更改像素的颜色。 I get a pixel, want to set new values for it but I get an error in setRGB method that it requires IntArray!我得到一个像素,想为它设置新值,但我在 setRGB 方法中得到一个错误,它需要 IntArray! but found Array:但发现数组:

val c: Int = bufferedImage.getRGB(x, y)
val color = Color(c)

val newColor = Array<Int>(3) { color.red; 0; 0 }
bufferedImage.setRGB(x, y, 0, 0, newColor, 0, 0)

Also, I am writing code in Kotlin/Java and cannot find detailed explanation how does setRGB() method work.另外,我正在用 Kotlin/Java 编写代码,但找不到 setRGB() 方法如何工作的详细说明。 I know from Intelij IDE that parameters are: setRGB(x, y, width, height, IntArray for rgb color, offset, scansize).我从 Intelij IDE 知道参数是:setRGB(x, y, width, height, IntArray for rgb color, offset, scansize)。

But what is width, height?但什么是宽度、高度? Is it dimensions of a picture?是图片的尺寸吗? Why do they matter if I only change one pixel?如果我只更改一个像素,它们为什么重要?

And how do I pass new color to setRGB() method correctly as IntArray?以及如何将新颜色作为 IntArray 正确传递给 setRGB() 方法?

setRGB expect a primitive int[] setRGB期望原始int[]

In kotlin the java equivalent of int[] is IntArray (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/ )在 kotlin 中, int[]的 java 等价物是IntArray (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int-array/ )

So you should change the creation of val newColor to:因此,您应该将val newColor的创建更改为:

val newColor = intArrayOf(color.red, 0, 0)

Full example:完整示例:

val c: Int = bufferedImage.getRGB(x, y)
val color = Color(c)

val newColor = intArrayOf(color.red, 0, 0)
bufferedImage.setRGB(x, y, 0, 0, newColor, 0, 0)

For more info regarding the function you can refer to the javadoc: https://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#setRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)有关该功能的更多信息,您可以参考 javadoc: https ://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferedImage.html#setRGB(int,%20int,%20int ,%20int,%20int[],%20int,%20int)

If you are only changing one pixel (at a time), you should use the setRGB(int x, int y, int aRGB) method.如果你只改变一个像素(一次),你应该使用setRGB(int x, int y, int aRGB)方法。 Don't bother with arrays at all.根本不用理会数组。

I don't normally program Kotlin, so the syntax may not be 100% correct, but anyway:我通常不会对 Kotlin 进行编程,因此语法可能不是 100% 正确,但无论如何:

val c: Int = bufferedImage.getRGB(x, y)
val color = Color(c)

val newColor = Color(color.red, 0, 0)
bufferedImage.setRGB(x, y, newColor.getRGB())

That said, the width and height parameters of the setRGB method that takes an int array, is the height and with of the region you want to set (and usually offset == 0 and scansize == width ) .也就是说,采用int数组的setRGB方法的widthheight参数是您要设置的区域的高度和scansize == width (通常offset == 0scansize == width )。 You typically use this only to set multiple pixels.您通常仅使用它来设置多个像素。 To set a single pixel using it, the values should be simply 1, 1 (I borrowed the intArrayOf part from @Tom's answer):要使用它设置单个像素,值应该只是1, 1 (我从intArrayOf的回答中借用了intArrayOf部分):

val newColor = intArrayOf(color.red, 0, 0)
bufferedImage.setRGB(x, y, 1, 1, newColor, 0, 1)

This should also work.这也应该有效。 But I think it's less elegant and probably slower due to array bounds checking and copying.但我认为由于数组边界检查和复制,它不太优雅并且可能更慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM