Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.findFragment
import androidx.lifecycle.MutableLiveData
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.signature.ObjectKey
import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.fhir.datacapture.tryUnwrapContext
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
Expand Down Expand Up @@ -225,14 +227,28 @@ object PhotoCaptureViewHolderFactory :

private fun loadPhotoPreview(byteArray: ByteArray, title: String) {
photoPreview.visibility = View.VISIBLE
Glide.with(context).load(byteArray).into(photoThumbnail)
photoTitle.text = title
Glide.with(context)
.asBitmap()
.load(byteArray)
.signature(ObjectKey(byteArray.toString())) // Add a signature to force Glide to reload the image
.transform(ViewHolderFactoryUtil.Companion.RotateTransformation(context, null, byteArray))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(photoThumbnail)
}

private fun loadPhotoPreview(uri: Uri, title: String) {
photoPreview.visibility = View.VISIBLE
Glide.with(context).load(uri).into(photoThumbnail)
photoTitle.text = title
Glide.with(context)
.asBitmap()
.load(uri)
.signature(ObjectKey(uri.toString())) // Add a signature to force Glide to reload the image
.transform(ViewHolderFactoryUtil.Companion.RotateTransformation(context, uri, null))
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(photoThumbnail)
}

fun clearPhotoPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@

package com.google.android.sensory.fhir_data

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Matrix
import androidx.exifinterface.media.ExifInterface
import android.net.Uri
import android.view.View
import androidx.core.net.toUri
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import java.io.ByteArrayInputStream
import java.io.File
import java.io.IOException
import java.security.MessageDigest

class ViewHolderFactoryUtil {
companion object {
Expand All @@ -45,5 +54,71 @@ class ViewHolderFactoryUtil {
itemView.findViewById<View>(com.google.android.fhir.datacapture.R.id.error).visibility =
View.GONE
}

// Function to rotate the image bitmap based on its Exif information
fun rotateImageIfRequired(context: Context, bitmap: Bitmap, uri: Uri): Bitmap {
return try {
val exif = context.contentResolver.openInputStream(uri)?.let { ExifInterface(it) }

val rotationInDegrees = when (exif?.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 90
}

val matrix = Matrix()
matrix.postRotate(rotationInDegrees.toFloat())
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)

} catch (e: IOException) {
e.printStackTrace()
bitmap
}
}

// Function to rotate the image byte array based on its Exif information
fun rotateImageIfRequired(bitmap: Bitmap, imageByteArray: ByteArray): Bitmap {
return try {
val exif = ExifInterface(ByteArrayInputStream(imageByteArray))

val rotationInDegrees = when (exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL)) {
ExifInterface.ORIENTATION_ROTATE_90 -> 90
ExifInterface.ORIENTATION_ROTATE_180 -> 180
ExifInterface.ORIENTATION_ROTATE_270 -> 270
else -> 90
}

val matrix = Matrix()
matrix.postRotate(rotationInDegrees.toFloat())
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)

} catch (e: IOException) {
e.printStackTrace()
bitmap
}
}

class RotateTransformation(private val context: Context, private val uri: Uri?, private val imageByteArray: ByteArray?) : BitmapTransformation() {
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
if(imageByteArray != null){
return rotateImageIfRequired(toTransform, imageByteArray)
}
return rotateImageIfRequired(context, toTransform, uri!!)
}

override fun updateDiskCacheKey(messageDigest: MessageDigest) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are not caching the image, do we need to override this ?
If this is not mandatory to override, then we can skip it.

if(imageByteArray != null){
messageDigest.update(imageByteArray)
}else{
messageDigest.update((context.packageName + uri.toString()).toByteArray())
}
}
}
}
}