android - 从 Gallery Kotlin Android 旋转图像方向

标签 android android-studio kotlin

当我将图像从图库上传到我的 kotlin 应用程序时,图像方向始终旋转到一侧。我怎样才能旋转它们,使它们变直。

最佳答案

正如Commensware建议您使用ExifInterface来旋转图像文件

这是我用于 ImageRotation 的类,

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Matrix
import android.media.ExifInterface
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream

object GetProperImageRotation {

fun getRotatedImageFile(photoFile: File, context: Context?): File? {
    val option = BitmapFactory.Options()
    option.inSampleSize = 4

    val convertedBitmap: Bitmap =
        modifyOrientation(
            BitmapFactory.decodeFile(photoFile.absolutePath, option),
            photoFile.absolutePath
        )

    return saveImage(convertedBitmap, context)

}

private fun saveImage(image: Bitmap, context: Context?): File? {

    val filename = getImageFilePath(context)
    val imageFile = File(filename)

    val os = BufferedOutputStream(FileOutputStream(imageFile))
    image.compress(Bitmap.CompressFormat.JPEG, 100, os)
    os.close()
    return imageFile
}

private fun getImageFilePath(context: Context?): String {
    val filename = "${System.currentTimeMillis()}.jpg"
    val dir = context?.getExternalFilesDir(null)

    return if (dir == null) {
        filename
    } else {
        "${dir.absolutePath}/$filename"
    }
}


private fun modifyOrientation(bitmap: Bitmap, image_absolute_path: String): Bitmap {
    val ei: ExifInterface = ExifInterface(image_absolute_path);
    val orientation: Int =
        ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    when (orientation) {
        ExifInterface.ORIENTATION_ROTATE_90 -> {
            return rotate(bitmap, 90f)
        }
        ExifInterface.ORIENTATION_ROTATE_180 -> {
            return rotate(bitmap, 180f)
        }
        ExifInterface.ORIENTATION_TRANSVERSE -> {
            return rotate(bitmap, 270f)
        }
        ExifInterface.ORIENTATION_ROTATE_270 -> {
            return rotate(bitmap, 270f)
        }
        ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> {
            return flip(bitmap, true, vertical = false)
        }
        ExifInterface.ORIENTATION_FLIP_VERTICAL -> {
            return flip(bitmap, false, vertical = true)
        }
        else -> {
            return bitmap
        }
    }
}

private fun rotate(bitmap: Bitmap, degrees: Float): Bitmap {
    val matrix = Matrix()
    matrix.postRotate(degrees)
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
}

private fun flip(bitmap: Bitmap, horizontal: Boolean, vertical: Boolean): Bitmap {
    val matrix = Matrix()
    matrix.preScale(if (horizontal) (-1f) else 1f, if (vertical) (-1f) else 1f)
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true);
}

}

你可以这样使用

   var rotatedImageFile = GetProperImageRotation.getRotatedImageFile(File("imagefilepath"),context)

关于android - 从 Gallery Kotlin Android 旋转图像方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61943185/

相关文章:

android - 使用 expo kit 在 React Native 中加载自定义原生组件

android - 从 Activity 调用 fragment 方法但 View 为空

android-studio - Genymotion 无效路径 : VBoxManage and Initialize Engine: failed

java - Kotlin/Java-如何识别内部软件包?

android - 在 recyclerview 中使用图像选择器

android - Room 不能选择一个构造函数,因为多个构造函数是合适的错误

Android onActivityResult(来自 MediaStore.ACTION_IMAGE_CAPTURE)无法获取 Galaxy S5 上的数据

android - Kotlin 延迟加载在 Junit 测试中不起作用

java - 断言错误 : Unexpected schema version 0: Unexpected schema version 0

Android Studio 以不同方式呈现相同的布局