无法从EXIF获取图像方向

时间:2019-05-16 18:30:50

标签: android image kotlin orientation exif

当前,我正在Kotlin中为我们公司的微库编写一个函数,该函数可以读取图像的字节并返回以度为单位的方向 我知道,在API 24中,我们具有ExifInterface并能够从InputStream实例化它,但是问题在于我们需要支持没有此类构造函数的API 21。

传递给getOrientation函数的字节数组始终看起来像这样:

-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 72, 0, 72, 0, 0, -1, -31, 8, 82, 69, 120, 105, 102, 0, 0, 77, 77, 0, 42, 0, 0, 0, 8, 0, 12, 1, 15, 0, 2, 0, 0, 0, 6, 0, 0, 0, -98, 1, 16, 0, 2, 0, 0, 0, 9, 0, 0, 0, -92, 1, 18, 0, 3, 0, 0, 0, 1, 0, 6, 0, 0, 1, 26, 0, 5, 0, 0, 0, 1, 0, 0, 0, -82, 1, 27, 0, 5, 0, 0, 0, 1, 0, 0, 0, -74, 1, 40, 0 and so on

它看起来像移位了,这就是为什么我在第一行中将它向右移位256个原因

这是我现在停留的代码:

object Exif {
    fun getOrientation(_bytes: ByteArray): Int {
        val bytes = _bytes.map { b -> b.toInt() + 256 }

        if (bytes[0] != 0xff && bytes[1] != 0xd8) {
            return 0
        }

        val length = bytes.size
        var offset = 2

        while (offset < length) {
            // TODO: extract all operations like the following
            // into separate function
            val marker = (bytes[offset] shl 8) or bytes[offset + 1]
            offset += 2

            if (marker == 0xffe1) {
                offset += 2
                val exifStr = (bytes[offset] shl 24) or (bytes[offset + 1] shl 16) or (bytes[offset + 2] shl 8) or bytes[offset + 3]

                if (exifStr != 0x45786966) {
                    return 0
                }

                offset += 6
                val little = (bytes[offset] shl 8) or bytes[offset + 1] == 0x4949
                offset += 4

                val inc = (bytes[offset] shl 24) or (bytes[offset + 1] shl 16) or (bytes[offset + 2] shl 8) or bytes[offset + 3]
                offset += if (little) inc.reverseBytes() else inc

                val tagsWOEndian = (bytes[offset] shl 8) or bytes[offset + 1]
                val tags = if (little) tagsWOEndian.reverseBytes() else tagsWOEndian
                offset += 2

                for (idx in 0..tags) {
                    val off = offset + idx * 12
                    val orientWOEndian = (bytes[off] shl 8) or bytes[off + 1]
                    val orient = if (little) orientWOEndian.reverseBytes() else orientWOEndian

                    if (orient == 0x0112) {
                        when ((bytes[off + 8] shl 8) or bytes[off + 8 + 1]) {
                            1 -> return 0
                            3 -> return 180
                            6 -> return 90
                            8 -> return 270
                        }
                    }
                }
            } else if (marker and 0xff00 != 0xff00) {
                break
            } else {
                offset += (bytes[offset] shl 8) or bytes[offset + 1]
            }
        }

        return 0
    }
}

fun Int.reverseBytes(): Int {
    val v0 = ((this ushr 0) and 0xFF)
    val v1 = ((this ushr 8) and 0xFF)
    val v2 = ((this ushr 16) and 0xFF)
    val v3 = ((this ushr 24) and 0xFF)
    return (v0 shl 24) or (v1 shl 16) or (v2 shl 8) or (v3 shl 0)
}

1 个答案:

答案 0 :(得分:0)

解决了! 我得到了以下解决方案:

fun getUint16(bytes: List<Int>, offset: Int, littleEndian: Boolean): Int {
    val value = (bytes[offset] shl 8) or bytes[offset + 1]
    return if (littleEndian) value.reverseBytes() else value
}

fun getUint32(bytes: List<Int>, offset: Int, littleEndian: Boolean): Int {
    val value = (bytes[offset] shl 24) or (bytes[offset + 1] shl 16) or (bytes[offset + 2] shl 8) or bytes[offset + 3]
    return if (littleEndian) value.reverseBytes() else value
}

fun Int.reverseBytes(): Int {
    val v0 = ((this ushr 0) and 0xFF)
    val v1 = ((this ushr 8) and 0xFF)
    val v2 = ((this ushr 16) and 0xFF)
    val v3 = ((this ushr 24) and 0xFF)
    return (v0 shl 24) or (v1 shl 16) or (v2 shl 8) or (v3 shl 0)
}

object Exif {
    fun getRotationDegrees(_bytes: ByteArray): Int {
        val bytes = _bytes.take(64 * 1024).map { b -> b.toInt() and 0xff }

        if (getUint16(bytes, 0, false) != 0xffd8) {
            return 0
        }

        val length = bytes.size
        var offset = 2

        while (offset < length) {
            val marker = getUint16(bytes, offset, false)
            offset += 2

            if (marker == 0xffe1) {
                if (getUint32(bytes, offset + 2, false) != 0x45786966) {
                    return 0
                }
                offset += 2

                val little = getUint16(bytes, offset + 6, false) == 0x4949
                offset += 6

                offset += getUint32(bytes, offset + 4, little)

                val tags = getUint16(bytes, offset, little)
                offset += 2

                for (idx in 0..tags) {
                    if (getUint16(bytes, offset + (idx * 12), little) == 0x0112) {
                        when (getUint16(bytes, offset + (idx * 12) + 8, little)) {
                            1 -> return 0
                            3 -> return 180
                            6 -> return 90
                            8 -> return 270
                        }
                    }
                }
            } else if (marker and 0xff00 != 0xff00) {
                break
            } else {
                offset += getUint16(bytes, offset, false)
            }
        }

        return 0
    }
}

与问题中发布的代码相比的更改:

  1. 已签名的_bytes与未签名的bytes之间的转换执行错误。现在,我首先获取前65536个字节,然后通过对每个字节应用按位and而不是简单地添加256
  2. ,将它们转换为无符号
  3. 所有用于从字节数组中获取UInt16和UInt32值的操作都移到了单独的函数中
  4. 修复了offset循环内错误的while增量