From 1eea3aea48142705bf314cf8e8d64501225d5bde Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sat, 23 Jan 2021 11:17:21 +0200 Subject: [PATCH] Use correct rotation for contact avatar. --- app/build.gradle | 1 + .../com/tutpro/baresip/ContactActivity.kt | 77 ++++++++++++++++--- build.gradle | 2 +- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 74e34d0d..62aa65f7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -53,6 +53,7 @@ dependencies { implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0" implementation "androidx.preference:preference-ktx:1.1.1" + implementation "androidx.exifinterface:exifinterface:1.3.2" } repositories { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt index 98492a92..413e82c5 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt @@ -1,20 +1,24 @@ package com.tutpro.baresip +import android.Manifest import android.app.Activity +import android.content.Context import android.content.Intent import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.graphics.Matrix +import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.GradientDrawable import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity +import android.provider.ContactsContract import android.view.Menu import android.view.MenuItem -import android.widget.* -import android.graphics.BitmapFactory -import android.graphics.drawable.BitmapDrawable -import androidx.cardview.widget.CardView import android.view.View +import android.widget.* +import androidx.appcompat.app.AppCompatActivity +import androidx.cardview.widget.CardView +import androidx.exifinterface.media.ExifInterface import com.tutpro.baresip.databinding.ActivityContactBinding - import java.io.File class ContactActivity : AppCompatActivity() { @@ -133,12 +137,18 @@ class ContactActivity : AppCompatActivity() { resultData?.data?.also { uri -> try { val inputStream = baseContext.contentResolver.openInputStream(uri) - val avatarImage = BitmapFactory.decodeStream(inputStream) - showImageAvatar(avatarImage) - if (Utils.saveBitmap(avatarImage, File(BaresipService.filesPath, "tmp.png"))) - newAvatar = "image" + val avatarBitmap = BitmapFactory.decodeStream(inputStream) + inputStream?.close() + val scaledBitmap = Bitmap.createScaledBitmap(avatarBitmap, 192, 192, true) + val exif = ExifInterface(baseContext.contentResolver.openInputStream(uri)!!) + val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, + ExifInterface.ORIENTATION_NORMAL) + val rotatedBitmap = rotateBitmap(scaledBitmap, orientation) + showImageAvatar(rotatedBitmap) + if (Utils.saveBitmap(rotatedBitmap, File(BaresipService.filesPath, "tmp.png"))) + newAvatar = "image" } catch (e: Exception) { - Log.e("Baresip", "Could not read avatar image") + Log.e("Baresip", "Could not read avatar image: $e") } } } @@ -224,7 +234,7 @@ class ContactActivity : AppCompatActivity() { Utils.deleteFile(File(BaresipService.filesPath, "${contact.id}.png")) } } - "image" -> { + "image" -> { contact.avatarImage = (cardImageAvatarView.drawable as BitmapDrawable).bitmap Utils.deleteFile(File(BaresipService.filesPath, "${contact.id}.png")) File(BaresipService.filesPath, "tmp.png") @@ -279,4 +289,47 @@ class ContactActivity : AppCompatActivity() { cardAvatarView.visibility = View.VISIBLE cardImageAvatarView.setImageBitmap(image) } + + private fun getThumbnailSize(ctx: Context): Int { + var thumbnailSize = 96 + if (Utils.checkPermission(this, Manifest.permission.READ_CONTACTS)) { + val c = ctx.contentResolver.query(ContactsContract.DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI, + arrayOf(ContactsContract.DisplayPhoto.THUMBNAIL_MAX_DIM), + null, null, null) + if (c != null && c.moveToFirst()) + thumbnailSize = c.getInt(0) + else + Log.e("Baresip", "Could not get THUMBNAIL_MAX_DIM") + c?.close() + } + return thumbnailSize + } + + private fun rotateBitmap(bitmap: Bitmap, orientation: Int): Bitmap { + val matrix = Matrix() + when (orientation) { + ExifInterface.ORIENTATION_NORMAL -> return bitmap + ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> matrix.setScale(-1f, 1f) + ExifInterface.ORIENTATION_ROTATE_180 -> matrix.setRotate(180f) + ExifInterface.ORIENTATION_FLIP_VERTICAL -> { + matrix.setRotate(180f) + matrix.postScale(-1f, 1f) + } + ExifInterface.ORIENTATION_TRANSPOSE -> { + matrix.setRotate(90f) + matrix.postScale(-1f, 1f) + } + ExifInterface.ORIENTATION_ROTATE_90 -> matrix.setRotate(90f) + ExifInterface.ORIENTATION_TRANSVERSE -> { + matrix.setRotate(-90f) + matrix.postScale(-1f, 1f) + } + ExifInterface.ORIENTATION_ROTATE_270 -> matrix.setRotate(-90f) + else -> return bitmap + } + val rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, + bitmap.width, bitmap.height, matrix, true) + bitmap.recycle() + return rotatedBitmap + } } diff --git a/build.gradle b/build.gradle index 65aa408c..1139f871 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.1.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files