Skip to content

Commit cb3e58c

Browse files
author
Jonas Thuresson
committed
Code formatting
1 parent 52a78aa commit cb3e58c

File tree

1 file changed

+71
-61
lines changed

1 file changed

+71
-61
lines changed

android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -262,69 +262,79 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
262262
): Bitmap? {
263263
Assertions.assertNotNull(outOptions)
264264

265-
return openBitmapInputStream(uri, headers)?.use {
266-
// Efficiently crops image without loading full resolution into memory
267-
// https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
268-
val decoder =
269-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
270-
BitmapRegionDecoder.newInstance(it)
271-
} else {
272-
@Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false)
273-
} ?: throw Error("Could not create bitmap decoder. Uri: $uri")
274-
275-
val orientation = getOrientation(reactContext, Uri.parse(uri))
276-
val (x, y) =
277-
when (orientation) {
278-
90 -> yPos to decoder.height - rectWidth - xPos
279-
270 -> decoder.width - rectHeight - yPos to xPos
280-
180 -> decoder.width - rectWidth - xPos to decoder.height - rectHeight - yPos
281-
else -> xPos to yPos
282-
}
265+
return openBitmapInputStream(uri, headers)?.use {
266+
// Efficiently crops image without loading full resolution into memory
267+
// https://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
268+
val decoder =
269+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
270+
BitmapRegionDecoder.newInstance(it)
271+
} else {
272+
@Suppress("DEPRECATION") BitmapRegionDecoder.newInstance(it, false)
273+
} ?: throw Error("Could not create bitmap decoder. Uri: $uri")
283274

284-
val (width, height) =
285-
when (orientation) {
286-
90,
287-
270 -> rectHeight to rectWidth
288-
else -> rectWidth to rectHeight
289-
}
290-
val (targetWidth, targetHeight) =
291-
when (orientation) {
292-
90,
293-
270 -> outputHeight to outputWidth
294-
else -> outputWidth to outputHeight
295-
}
275+
val orientation = getOrientation(reactContext, Uri.parse(uri))
276+
val (x, y) =
277+
when (orientation) {
278+
90 -> yPos to decoder.height - rectWidth - xPos
279+
270 -> decoder.width - rectHeight - yPos to xPos
280+
180 -> decoder.width - rectWidth - xPos to decoder.height - rectHeight - yPos
281+
else -> xPos to yPos
282+
}
283+
284+
val (width, height) =
285+
when (orientation) {
286+
90,
287+
270 -> rectHeight to rectWidth
288+
else -> rectWidth to rectHeight
289+
}
290+
val (targetWidth, targetHeight) =
291+
when (orientation) {
292+
90,
293+
270 -> outputHeight to outputWidth
294+
else -> outputWidth to outputHeight
295+
}
296296

297-
val cropRectRatio = width / height.toFloat()
298-
val targetRatio = targetWidth / targetHeight.toFloat()
299-
val isCropRatioLargerThanTargetRatio = cropRectRatio > targetRatio
300-
val newWidth =
301-
if (isCropRatioLargerThanTargetRatio) height * targetRatio else width.toFloat()
302-
val newHeight =
303-
if (isCropRatioLargerThanTargetRatio) height.toFloat() else width / targetRatio
304-
val newX = if (isCropRatioLargerThanTargetRatio) x + (width - newWidth) / 2 else x.toFloat()
305-
val newY =
306-
if (isCropRatioLargerThanTargetRatio) y.toFloat() else y + (height - newHeight) / 2
307-
val scale =
308-
if (isCropRatioLargerThanTargetRatio) targetHeight / height.toFloat()
309-
else targetWidth / width.toFloat()
310-
311-
// Decode the bitmap. We have to open the stream again, like in the example linked above.
312-
// Is there a way to just continue reading from the stream?
313-
outOptions.inSampleSize = getDecodeSampleSize(width, height, targetWidth, targetHeight)
314-
315-
val cropX = (newX / outOptions.inSampleSize.toFloat()).roundToInt()
316-
val cropY = (newY / outOptions.inSampleSize.toFloat()).roundToInt()
317-
val cropWidth = (newWidth / outOptions.inSampleSize.toFloat()).roundToInt()
318-
val cropHeight = (newHeight / outOptions.inSampleSize.toFloat()).roundToInt()
319-
val cropScale = scale * outOptions.inSampleSize
320-
val scaleMatrix = Matrix().apply { setScale(cropScale, cropScale) }
321-
val filter = true
322-
323-
val rect = Rect(0, 0, decoder.width, decoder.height)
324-
val bitmap =decoder.decodeRegion(rect, outOptions)
325-
326-
return Bitmap.createBitmap(bitmap, cropX, cropY, cropWidth, cropHeight, scaleMatrix, filter)
327-
}
297+
val cropRectRatio = width / height.toFloat()
298+
val targetRatio = targetWidth / targetHeight.toFloat()
299+
val isCropRatioLargerThanTargetRatio = cropRectRatio > targetRatio
300+
val newWidth =
301+
if (isCropRatioLargerThanTargetRatio) height * targetRatio else width.toFloat()
302+
val newHeight =
303+
if (isCropRatioLargerThanTargetRatio) height.toFloat() else width / targetRatio
304+
val newX =
305+
if (isCropRatioLargerThanTargetRatio) x + (width - newWidth) / 2 else x.toFloat()
306+
val newY =
307+
if (isCropRatioLargerThanTargetRatio) y.toFloat() else y + (height - newHeight) / 2
308+
val scale =
309+
if (isCropRatioLargerThanTargetRatio) targetHeight / height.toFloat()
310+
else targetWidth / width.toFloat()
311+
312+
// Decode the bitmap. We have to open the stream again, like in the example linked
313+
// above.
314+
// Is there a way to just continue reading from the stream?
315+
outOptions.inSampleSize = getDecodeSampleSize(width, height, targetWidth, targetHeight)
316+
317+
val cropX = (newX / outOptions.inSampleSize.toFloat()).roundToInt()
318+
val cropY = (newY / outOptions.inSampleSize.toFloat()).roundToInt()
319+
val cropWidth = (newWidth / outOptions.inSampleSize.toFloat()).roundToInt()
320+
val cropHeight = (newHeight / outOptions.inSampleSize.toFloat()).roundToInt()
321+
val cropScale = scale * outOptions.inSampleSize
322+
val scaleMatrix = Matrix().apply { setScale(cropScale, cropScale) }
323+
val filter = true
324+
325+
val rect = Rect(0, 0, decoder.width, decoder.height)
326+
val bitmap = decoder.decodeRegion(rect, outOptions)
327+
328+
return Bitmap.createBitmap(
329+
bitmap,
330+
cropX,
331+
cropY,
332+
cropWidth,
333+
cropHeight,
334+
scaleMatrix,
335+
filter
336+
)
337+
}
328338
}
329339

330340
private fun openBitmapInputStream(uri: String, headers: HashMap<String, Any?>?): InputStream? {

0 commit comments

Comments
 (0)