Skip to content
Snippets Groups Projects
Commit 9d0d509f authored by Taras's avatar Taras
Browse files

Handle orientation for images and videos

parent dc209f7f
No related branches found
No related tags found
No related merge requests found
......@@ -9,6 +9,7 @@ import androidx.core.database.getStringOrNull
import org.futo.circles.core.blurhash.ThumbHash
import org.futo.circles.core.model.MediaAttachmentInfo
import org.futo.circles.core.utils.ImageUtils
import org.futo.circles.core.utils.MediaUtils.getOrientation
import org.futo.circles.core.utils.VideoUtils
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
......@@ -35,7 +36,7 @@ fun Uri.getFilename(context: Context): String? {
suspend fun Uri.toImageContentAttachmentData(context: Context): ContentAttachmentData? {
val attachmentInfo = getMediaAttachmentInfo(context) ?: return null
val resolution = ImageUtils.getImageResolution(context, this)
val orientation = ImageUtils.getOrientation(context, this)
val orientation = getOrientation(context, this)
return ContentAttachmentData(
mimeType = attachmentInfo.mimeType,
type = ContentAttachmentData.Type.IMAGE,
......
......@@ -5,6 +5,7 @@ import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Size
import androidx.exifinterface.media.ExifInterface
import org.futo.circles.core.utils.MediaUtils.getSizeBasedOnOrientation
object ImageUtils {
......@@ -16,19 +17,7 @@ object ImageUtils {
null,
options
)
return Size(options.outWidth, options.outHeight)
return getSizeBasedOnOrientation(context, uri, options.outWidth, options.outHeight)
}
fun getOrientation(context: Context, uri: Uri): Int {
var orientation = 0
context.contentResolver.openInputStream(uri)?.use { inputStream ->
try {
ExifInterface(inputStream).let {
orientation = it.rotationDegrees
}
} catch (ignore: Exception) {
}
}
return orientation
}
}
\ No newline at end of file
package org.futo.circles.core.utils
import android.content.Context
import android.net.Uri
import android.util.Size
import androidx.exifinterface.media.ExifInterface
object MediaUtils {
fun getOrientation(context: Context, uri: Uri): Int {
var orientation = 0
context.contentResolver.openInputStream(uri)?.use { inputStream ->
try {
ExifInterface(inputStream).let {
orientation = it.rotationDegrees
}
} catch (ignore: Exception) {
}
}
return orientation
}
fun getSizeBasedOnOrientation(context: Context, uri: Uri, width: Int, height: Int): Size {
val orientation = getOrientation(context, uri)
return if (orientation == 90 || orientation == 270) Size(height, width)
else Size(width, height)
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ import android.content.Context
import android.media.MediaMetadataRetriever
import android.net.Uri
import android.util.Size
import org.futo.circles.core.utils.MediaUtils.getSizeBasedOnOrientation
import java.util.concurrent.TimeUnit
......@@ -25,7 +26,11 @@ object VideoUtils {
val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
retriever.release()
Size(width?.toInt() ?: 0, height?.toInt() ?: 0)
getSizeBasedOnOrientation(
context, uri,
width?.toInt() ?: 0,
height?.toInt() ?: 0
)
} catch (e: Exception) {
Size(0, 0)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment