我正在创建一个程序,它可以使用Kotlin读取图像文件中最不重要的一点。我有一个函数可以读取文件中的字节,但我不知道如何实际地打印函数consumeArray中的字节。
我的目标是打印图像中最不重要的部分。
override fun run() {
val buff = ByteArray(1230)
File("src\\main\\kotlin\\day01_least_significant_bit_steganography\\eksempel_bakgrunnsbilde.png").inputStream().buffered().use { input ->
while(true) {
val sz = input.read(buff)
if (sz <= 0) break
///at that point we have a sz bytes in the buff to process
consumeArray(buff, 0, sz)
}
}
} // run
private fun consumeArray(buff: ByteArray, i: Int, sz: Int) {
println("??")
} // consumeArray发布于 2021-12-01 22:59:59
在Kotlin 1.4+中,可以使用.takeLowestOneBit()方法获得任何字节中最小的有效位。
可能发生的情况是,它等于零,所以您需要迭代byteArray,直到满足任何非零最不重要的位(我相信这就是“最小有效位byteArray”下的意思):
var lowestBit: Byte = 0
for (index in sz - 1 downTo 0) {
val currentLowestBit = buff[index].takeLowestOneBit()
if (currentLowestBit != 0.toByte()) {
lowestBit = currentLowestBit
break
}
}请注意,它将打印缓冲区中最不重要的部分,而不是整个图像(如果大于缓冲区)
https://stackoverflow.com/questions/70190133
复制相似问题