Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么访问ByteBuffer的单个元素比使用额外副本的ByteArray慢?_Java_Android_Performance_Kotlin_Jvm - Fatal编程技术网

Java 为什么访问ByteBuffer的单个元素比使用额外副本的ByteArray慢?

Java 为什么访问ByteBuffer的单个元素比使用额外副本的ByteArray慢?,java,android,performance,kotlin,jvm,Java,Android,Performance,Kotlin,Jvm,这是camerax分析仪的一部分,用于旋转图像: y缓冲区被传递给set方法,单个字节通过get方法或[]运算符读取 private data class IImage(var byteArray: ByteArray= ByteArray(0), var width: Int=0, var height: Int=0) { //set rotationDegrees to 0 to disable rotation fun Set(rotationDegrees:Int,buf

这是camerax分析仪的一部分,用于旋转图像: y缓冲区被传递给set方法,单个字节通过get方法或[]运算符读取

private data class IImage(var byteArray: ByteArray= ByteArray(0), var width: Int=0, var height: Int=0)
{
    //set rotationDegrees to 0 to disable rotation
    fun Set(rotationDegrees:Int,buffer:ByteBuffer,w:Int,h:Int){
        if (byteArray.size != buffer.capacity()) byteArray =ByteArray(buffer.capacity()) //resize buffer
        if(rotationDegrees == 0 || rotationDegrees % 90 != 0){ buffer.get(byteArray); width=w; height=h}
        else
        {
            for (y in 0 until h) // we scan the array by rows
                for (x in 0 until w)
                    when (rotationDegrees) {
                        90 -> byteArray[x * h + h - y - 1] = buffer.get(x + y * w) // Fill from top-right toward left (CW)
                        180 -> byteArray[w * (h - y - 1) + w - x - 1] = buffer.get(x + y * w) // Fill from bottom-right toward up (CW)
                        270 -> byteArray[y + x * h] = buffer.get(y * w + w - x - 1) // The opposite (CCW) of 90 degrees
                    }
            if (rotationDegrees != 180) { height = w; width = h }
        }
    }
}
这是相同的代码,但使用了额外的字节数组

 private data class IImage2(var byteArray: ByteArray= ByteArray(0), var width: Int=0, var height: Int=0)
{
    //set rotationDegrees to 0 to disable rotation
    fun Set(rotationDegrees:Int,buffer:ByteBuffer,w:Int,h:Int){
        if (byteArray.size != buffer.capacity()) byteArray =ByteArray(buffer.capacity()) //resize buffer
        buffer.get(byteArray); width=w; height=h
        if(rotationDegrees == 0 || rotationDegrees % 90 != 0) return
        else
        {
            val tByteArray=ByteArray(byteArray.size)
            for (y in 0 until h) // we scan the array by rows
                for (x in 0 until w)
                    when (rotationDegrees) {
                        90 -> tByteArray[x * h + h - y - 1] = byteArray[x + y * w] // Fill from top-right toward left (CW)
                        180 -> tByteArray[w * (h - y - 1) + w - x - 1] = byteArray[x + y * w] // Fill from bottom-right toward up (CW)
                        270 -> tByteArray[y + x * h] = byteArray[y * w + w - x - 1] // The opposite (CCW) of 90 degrees
                    }
            byteArray=tByteArray
            if (rotationDegrees != 180) { height = w; width = h }
        }
    }
}

analysis类存储IImage或IImage2的单个实例,IImage代码在模拟器上以5:7 fps的速度生成,而IImage在模拟器上以9/10:12 fps的速度生成,为什么要生成另一个ByteArray并复制数据比访问ByteBuffer的单个字节快2倍?您需要查找仿射变换。它们比使用switch语句更快。@user207421我同意OP应该使用仿射变换工具,而不是重新发明轮子。但是内置设备也有特殊处理90°倍数的理由(避免舍入误差和更好的性能)…@Holger我建议他寻找正确的数学方法来实现这一点。20年前,当一位负责图形的同事在毫无基础知识的情况下从头开始做这件事时,我就被这件事咬了一口。他犯了几个重大错误,23年后这些错误还在系统中。@user207421,@Holger,很可能是我误解了什么,因为我不知道有什么内置的,我在使用zxing的条形码阅读器,所以我只需要通过yplane作为缓冲,如果将图像转换为位图比将图像转换为位图更快,那么使用仿射变换,然后将其转换回并将yBuffer提取为字节数组,这难道不是循环旋转它吗?或者我缺少了一条更短的路径?你需要查找仿射变换。它们比使用switch语句更快。@user207421我同意OP应该使用仿射变换工具,而不是重新发明轮子。但是内置设备也有特殊处理90°倍数的理由(避免舍入误差和更好的性能)…@Holger我建议他寻找正确的数学方法来实现这一点。20年前,当一位负责图形的同事在毫无基础知识的情况下从头开始做这件事时,我就被这件事咬了一口。他犯了几个重大错误,23年后这些错误还在系统中。@user207421,@Holger,很可能是我误解了什么,因为我不知道有什么内置的,我在使用zxing的条形码阅读器,所以我只需要通过yplane作为缓冲,如果将图像转换为位图比将图像转换为位图更快,那么使用仿射变换,然后将其转换回并将yBuffer提取为字节数组,这难道不是循环旋转它吗?还是我错过了一条更短的路?