From 7094f9850a09882604fa4838ecc14d0c1d2f04f9 Mon Sep 17 00:00:00 2001 From: Ilia Voitcekhovskii Date: Fri, 10 Dec 2021 12:51:01 +0300 Subject: [PATCH] Fix unused inSampleSize and add scaling to decoding step (#359) * Fix bitmap decoding to use inSampleSize * Add scaling (by max side) while decoding bitmap --- .../transformer/BitmapPhotoTransformer.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/result/transformer/BitmapPhotoTransformer.kt b/fotoapparat/src/main/java/io/fotoapparat/result/transformer/BitmapPhotoTransformer.kt index 0e7d6b52..34556751 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/result/transformer/BitmapPhotoTransformer.kt +++ b/fotoapparat/src/main/java/io/fotoapparat/result/transformer/BitmapPhotoTransformer.kt @@ -23,7 +23,8 @@ internal class BitmapPhotoTransformer( desiredResolution = desiredResolution ) - val decodedBitmap = input.decodeBitmap(scaleFactor) ?: throw UnableToDecodeBitmapException() + val decodedBitmap = input.decodeBitmap(scaleFactor, originalResolution, desiredResolution) + ?: throw UnableToDecodeBitmapException() val bitmap = if (decodedBitmap.width == desiredResolution.width && decodedBitmap.height == desiredResolution.height) { decodedBitmap @@ -44,14 +45,28 @@ internal class BitmapPhotoTransformer( } -private fun Photo.decodeBitmap(scaleFactor: Float): Bitmap? { +private fun Photo.decodeBitmap( + scaleFactor: Float, + originalResolution: Resolution, + desiredResolution: Resolution +): Bitmap? { val options = BitmapFactory.Options() options.inSampleSize = scaleFactor.toInt() + options.inScaled = true + + if (desiredResolution.width > desiredResolution.height) { + options.inDensity = originalResolution.width + options.inTargetDensity = desiredResolution.width * options.inSampleSize + } else { + options.inDensity = originalResolution.height + options.inTargetDensity = desiredResolution.height * options.inSampleSize + } return BitmapFactory.decodeByteArray( encodedImage, 0, - encodedImage.size + encodedImage.size, + options ) }