Consider EXIF orientation manually for APIs < 28

Co-authored-by: Matthieu <24-artectrex@users.noreply.shinice.net>
This commit is contained in:
Frédéric Gerber 2021-09-23 22:35:20 +02:00 committed by Matthieu
parent 73fc84ad75
commit d64728d8ed
1 changed files with 34 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import android.content.*
import android.content.res.Resources import android.content.res.Resources
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.ImageDecoder import android.graphics.ImageDecoder
import android.graphics.Matrix
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
@ -12,13 +13,14 @@ import android.util.DisplayMetrics
import android.view.WindowManager import android.view.WindowManager
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.browser.customtabs.CustomTabsIntent import androidx.browser.customtabs.CustomTabsIntent
import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import org.pixeldroid.app.R
import okhttp3.HttpUrl import okhttp3.HttpUrl
import org.pixeldroid.app.R
import kotlin.properties.ReadWriteProperty import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty import kotlin.reflect.KProperty
@ -71,10 +73,39 @@ fun bitmapFromUri(contentResolver: ContentResolver, uri: Uri?): Bitmap =
) )
{ decoder, _, _ -> decoder.isMutableRequired = true } { decoder, _, _ -> decoder.isMutableRequired = true }
} else { } else {
//FIXME EXIF orientation is ignored by getBitmap, respect it manually? val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri)
MediaStore.Images.Media.getBitmap(contentResolver, uri) modifyOrientation(bitmap!!, contentResolver, uri!!)
} }
fun modifyOrientation(
bitmap: Bitmap,
contentResolver: ContentResolver,
uri: Uri
): Bitmap {
val inputStream = contentResolver.openInputStream(uri)!!
val ei = ExifInterface(inputStream)
return when (ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED)) {
ExifInterface.ORIENTATION_ROTATE_90 -> bitmap.rotate(90f)
ExifInterface.ORIENTATION_ROTATE_180 -> bitmap.rotate(180f)
ExifInterface.ORIENTATION_ROTATE_270 -> bitmap.rotate(270f)
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> bitmap.flip(horizontal = true, vertical = false)
ExifInterface.ORIENTATION_FLIP_VERTICAL -> bitmap.flip(horizontal = false, vertical = true)
else -> bitmap
}
}
fun Bitmap.rotate(degrees: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(degrees)
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
fun Bitmap.flip(horizontal: Boolean, vertical: Boolean): Bitmap {
val matrix = Matrix()
matrix.preScale(if (horizontal) -1f else 1f, if (vertical) -1f else 1f)
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
}
fun BaseActivity.openUrl(url: String): Boolean{ fun BaseActivity.openUrl(url: String): Boolean{
val intent = CustomTabsIntent.Builder().build() val intent = CustomTabsIntent.Builder().build()