我有一个 ExifUtil,它基于 EXIF 旋转位图,如果设备上本地图像的路径,这个类可以正常工作。
如果图像路径来自 AWS,我想使用相同的类或相同的功能。请问我怎样才能做到这一点。
在此先感谢 R
网址是https://xxx-2.amazonaws.com/YRINWoUKBOW97VXvpnR ...。
class ExifUtil {
companion object {
fun rotateBitmap(src: String, bitmap: Bitmap): Bitmap {
try {
val orientation: Int = getExifOrientation(src)
if (orientation == 1) {
return bitmap
}
val matrix = Matrix()
when (orientation) {
2 -> matrix.setScale(-1f, 1f)
3 -> matrix.setRotate(180f)
4 -> {
matrix.setRotate(180f)
matrix.postScale(-1f, 1f)
}
5 -> {
matrix.setRotate(90f)
matrix.postScale(-1f, 1f)
}
6 -> matrix.setRotate(90f)
7 -> {
matrix.setRotate(-90f)
matrix.postScale(-1f, 1f)
}
8 -> matrix.setRotate(-90f)
else -> return bitmap
}
return try {
val oriented =
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
bitmap.recycle()
oriented
} catch (e: OutOfMemoryError) {
e.printStackTrace()
bitmap
}
} catch (e: IOException) {
e.printStackTrace()
}
return bitmap
}
@Throws(IOException::class)
private fun getExifOrientation(src: String): Int {
var orientation = 1
try {
val exif = ExifInterface(src);
orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
1
);
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: SecurityException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
} catch (e: InstantiationException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: NoSuchFieldException) {
e.printStackTrace()
}
return orientation
}
}
}