Resize Bitmap by Keeping the Same Aspect Ratio in Kotlin – Android

In Android, we use the Bitmap class to store images. Following is a utility method that would help you to resize Bitmap by keeping the same aspect ratio in Kotlin.

Following is the Kotlin solution. Those who are looking for the Java solution can visit How to Resize a Bitmap by Keeping the Same Aspect Ratio in Java.

Utility Method to Resize a Bitmap by Keeping the Same Aspect Ratio

fun resizeBitmap(source: Bitmap, maxLength: Int): Bitmap {
        try {
            if (source.height >= source.width) {
                if (source.height <= maxLength) { // if image height already smaller than the required height
                    return source
                }

                val aspectRatio = source.width.toDouble() / source.height.toDouble()
                val targetWidth = (maxLength * aspectRatio).toInt()
                val result = Bitmap.createScaledBitmap(source, targetWidth, maxLength, false)
                return result
            } else {
                if (source.width <= maxLength) { // if image width already smaller than the required width
                    return source
                }

                val aspectRatio = source.height.toDouble() / source.width.toDouble()
                val targetHeight = (maxLength * aspectRatio).toInt()

                val result = Bitmap.createScaledBitmap(source, maxLength, targetHeight, false)
                return result
            }
        } catch (e: Exception) {
            return source
        }
    }

How to Use This Method?

This method takes 2 parameters. The first parameter is the Bitmap you want to resize. The second parameter is the maximum length (either width or height) you want in the final Bitmap. This method resizes the Bitmap by keeping the same aspect ratio. If the image height is greater than the width, it resizes the image to match height with the parameter maxLength. If width is greater than the height, it resizes the image to match width with the parameter maxLength. This method doesn’t scale up the image. It means if the Image dimensions are under the required dimensions it will return the same Bitmap.

That’s it. This is how to resize Bitmap in Kotlin. Enjoy 🙂

For other helping code snippets you can visit our Coding Articles & Tutorials Knowledge Base.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

4 thoughts on “Resize Bitmap by Keeping the Same Aspect Ratio in Kotlin – Android

  1. What i don’t realize is in reality how you’re now not actually much more neatly-appreciated than you may be now. You’re very intelligent. You know therefore significantly relating to this matter, made me personally consider it from a lot of various angles. Its like women and men don’t seem to be involved unless it is something to do with Woman gaga! Your own stuffs outstanding. At all times deal with it up!

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: