rotate the bitmap received the AWS using EXIF

110 views Asked by At

I have a ExifUtil which rotates the bitmap based on EXIF, this class works fine if the path of the image local on the device.

I want to use the same class or same functionality if the image path is from the AWS. how can I achieve that please.

Thanks in advance R

url is 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
        }
    }
}
0

There are 0 answers