android - 从库 Kotlin Android 旋转图像方向
问题描述
当我从我的画廊上传一张图片到我的 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)
推荐阅读
- swift - 如何使用 UIBezierPath 以圆弧为中心绘制自定义形状
- javascript - 反应 prevstate 用法
- android - Android : 使用 sendgrid 发送电子邮件
- javascript - 用于 iframe 的 Google Chrome 中的错误
- c# - 有没有办法从编译的 EXE(使用 Quest PowerGUI)正确访问 isnetworkdeployed 属性?
- list - Ocaml:必要时展平列表
- c - 为什么第二次调用 memset 比第一次要快得多?
- php - 我如何从这个 php api 的响应中得到结果?
- reactjs - React Native 包安装中 JSON 错误的意外结束
- mysql - MySQL 5:使用没有 LAG / LEAD 功能的 LAG / LEAD 请求