From cb3e58c0916f1de5b1ce03915618734eccdc7258 Mon Sep 17 00:00:00 2001 From: Jonas Thuresson Date: Fri, 22 Mar 2024 10:37:51 +0100 Subject: [PATCH] Code formatting --- .../imageeditor/ImageEditorModuleImpl.kt | 132 ++++++++++-------- 1 file changed, 71 insertions(+), 61 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt index e1209cf..8fd6014 100644 --- a/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt @@ -262,69 +262,79 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) { ): Bitmap? { Assertions.assertNotNull(outOptions) - return openBitmapInputStream(uri, headers)?.use { - // Efficiently crops image without loading full resolution into memory - // https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html - val decoder = - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - BitmapRegionDecoder.newInstance(it) - } else { - @Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false) - } ?: throw Error("Could not create bitmap decoder. Uri: $uri") - - val orientation = getOrientation(reactContext, Uri.parse(uri)) - val (x, y) = - when (orientation) { - 90 -> yPos to decoder.height - rectWidth - xPos - 270 -> decoder.width - rectHeight - yPos to xPos - 180 -> decoder.width - rectWidth - xPos to decoder.height - rectHeight - yPos - else -> xPos to yPos - } + return openBitmapInputStream(uri, headers)?.use { + // Efficiently crops image without loading full resolution into memory + // https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html + val decoder = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + BitmapRegionDecoder.newInstance(it) + } else { + @Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false) + } ?: throw Error("Could not create bitmap decoder. Uri: $uri") - val (width, height) = - when (orientation) { - 90, - 270 -> rectHeight to rectWidth - else -> rectWidth to rectHeight - } - val (targetWidth, targetHeight) = - when (orientation) { - 90, - 270 -> outputHeight to outputWidth - else -> outputWidth to outputHeight - } + val orientation = getOrientation(reactContext, Uri.parse(uri)) + val (x, y) = + when (orientation) { + 90 -> yPos to decoder.height - rectWidth - xPos + 270 -> decoder.width - rectHeight - yPos to xPos + 180 -> decoder.width - rectWidth - xPos to decoder.height - rectHeight - yPos + else -> xPos to yPos + } + + val (width, height) = + when (orientation) { + 90, + 270 -> rectHeight to rectWidth + else -> rectWidth to rectHeight + } + val (targetWidth, targetHeight) = + when (orientation) { + 90, + 270 -> outputHeight to outputWidth + else -> outputWidth to outputHeight + } - val cropRectRatio = width / height.toFloat() - val targetRatio = targetWidth / targetHeight.toFloat() - val isCropRatioLargerThanTargetRatio = cropRectRatio > targetRatio - val newWidth = - if (isCropRatioLargerThanTargetRatio) height * targetRatio else width.toFloat() - val newHeight = - if (isCropRatioLargerThanTargetRatio) height.toFloat() else width / targetRatio - val newX = if (isCropRatioLargerThanTargetRatio) x + (width - newWidth) / 2 else x.toFloat() - val newY = - if (isCropRatioLargerThanTargetRatio) y.toFloat() else y + (height - newHeight) / 2 - val scale = - if (isCropRatioLargerThanTargetRatio) targetHeight / height.toFloat() - else targetWidth / width.toFloat() - - // Decode the bitmap. We have to open the stream again, like in the example linked above. - // Is there a way to just continue reading from the stream? - outOptions.inSampleSize = getDecodeSampleSize(width, height, targetWidth, targetHeight) - - val cropX = (newX / outOptions.inSampleSize.toFloat()).roundToInt() - val cropY = (newY / outOptions.inSampleSize.toFloat()).roundToInt() - val cropWidth = (newWidth / outOptions.inSampleSize.toFloat()).roundToInt() - val cropHeight = (newHeight / outOptions.inSampleSize.toFloat()).roundToInt() - val cropScale = scale * outOptions.inSampleSize - val scaleMatrix = Matrix().apply { setScale(cropScale, cropScale) } - val filter = true - - val rect = Rect(0, 0, decoder.width, decoder.height) - val bitmap =decoder.decodeRegion(rect, outOptions) - - return Bitmap.createBitmap(bitmap, cropX, cropY, cropWidth, cropHeight, scaleMatrix, filter) - } + val cropRectRatio = width / height.toFloat() + val targetRatio = targetWidth / targetHeight.toFloat() + val isCropRatioLargerThanTargetRatio = cropRectRatio > targetRatio + val newWidth = + if (isCropRatioLargerThanTargetRatio) height * targetRatio else width.toFloat() + val newHeight = + if (isCropRatioLargerThanTargetRatio) height.toFloat() else width / targetRatio + val newX = + if (isCropRatioLargerThanTargetRatio) x + (width - newWidth) / 2 else x.toFloat() + val newY = + if (isCropRatioLargerThanTargetRatio) y.toFloat() else y + (height - newHeight) / 2 + val scale = + if (isCropRatioLargerThanTargetRatio) targetHeight / height.toFloat() + else targetWidth / width.toFloat() + + // Decode the bitmap. We have to open the stream again, like in the example linked + // above. + // Is there a way to just continue reading from the stream? + outOptions.inSampleSize = getDecodeSampleSize(width, height, targetWidth, targetHeight) + + val cropX = (newX / outOptions.inSampleSize.toFloat()).roundToInt() + val cropY = (newY / outOptions.inSampleSize.toFloat()).roundToInt() + val cropWidth = (newWidth / outOptions.inSampleSize.toFloat()).roundToInt() + val cropHeight = (newHeight / outOptions.inSampleSize.toFloat()).roundToInt() + val cropScale = scale * outOptions.inSampleSize + val scaleMatrix = Matrix().apply { setScale(cropScale, cropScale) } + val filter = true + + val rect = Rect(0, 0, decoder.width, decoder.height) + val bitmap = decoder.decodeRegion(rect, outOptions) + + return Bitmap.createBitmap( + bitmap, + cropX, + cropY, + cropWidth, + cropHeight, + scaleMatrix, + filter + ) + } } private fun openBitmapInputStream(uri: String, headers: HashMap?): InputStream? {