Skip to content

Commit ca853b7

Browse files
authored
Refactoring: CropImageView - leverage default parameter to nuke duplicate method. (#516)
1 parent 5802270 commit ca853b7

1 file changed

Lines changed: 43 additions & 54 deletions

File tree

cropper/src/main/kotlin/com/canhub/cropper/CropImageView.kt

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import kotlin.math.pow
3636
import kotlin.math.sqrt
3737

3838
/** Custom view that provides cropping capabilities to an image. */
39-
@Suppress("unused", "MemberVisibilityCanBePrivate")
4039
class CropImageView @JvmOverloads constructor(
4140
context: Context,
4241
attrs: AttributeSet? = null,
@@ -600,70 +599,60 @@ class CropImageView @JvmOverloads constructor(
600599
get() = getCroppedImage(0, 0, RequestSizeOptions.NONE)
601600

602601
/**
603-
* Gets the cropped image based on the current crop window.<br></br>
604-
* Uses [RequestSizeOptions.RESIZE_INSIDE] option.
605-
*
606-
* [reqWidth] the width to resize the cropped image to
607-
* [reqHeight] the height to resize the cropped image to
608-
* @return a new Bitmap representing the cropped image
609-
*/
610-
fun getCroppedImage(reqWidth: Int, reqHeight: Int): Bitmap? {
611-
return getCroppedImage(reqWidth, reqHeight, RequestSizeOptions.RESIZE_INSIDE)
612-
}
613-
614-
/**
615-
* Gets the cropped image based on the current crop window.<br></br>
602+
* Gets the cropped image based on the current crop window.
616603
*
617604
* [reqWidth] the width to resize the cropped image
618605
* [reqHeight] the height to resize the cropped image
619-
* [options] the resize method to use, see its documentation
606+
* [options] the resize method to use
620607
* @return a new Bitmap representing the cropped image
621608
*/
622-
fun getCroppedImage(reqWidth: Int, reqHeight: Int, options: RequestSizeOptions): Bitmap? {
623-
var croppedBitmap: Bitmap? = null
609+
@JvmOverloads
610+
fun getCroppedImage(
611+
reqWidth: Int,
612+
reqHeight: Int,
613+
options: RequestSizeOptions = RequestSizeOptions.RESIZE_INSIDE,
614+
): Bitmap? {
624615
if (originalBitmap != null) {
625616
val newReqWidth = if (options != RequestSizeOptions.NONE) reqWidth else 0
626617
val newReqHeight = if (options != RequestSizeOptions.NONE) reqHeight else 0
627-
croppedBitmap = if (imageUri != null &&
628-
(loadedSampleSize > 1 || options == RequestSizeOptions.SAMPLING)
629-
) {
630-
val orgWidth = originalBitmap!!.width * loadedSampleSize
631-
val orgHeight = originalBitmap!!.height * loadedSampleSize
632-
val bitmapSampled = BitmapUtils
633-
.cropBitmap(
634-
context,
635-
imageUri,
636-
cropPoints,
637-
mDegreesRotated,
638-
orgWidth,
639-
orgHeight,
640-
mCropOverlayView!!.isFixAspectRatio,
641-
mCropOverlayView.aspectRatioX,
642-
mCropOverlayView.aspectRatioY,
643-
newReqWidth,
644-
newReqHeight,
645-
mFlipHorizontally,
646-
mFlipVertically,
647-
)
648-
bitmapSampled.bitmap
618+
val croppedBitmap = if (imageUri != null && (loadedSampleSize > 1 || options == RequestSizeOptions.SAMPLING)) {
619+
BitmapUtils.cropBitmap(
620+
context = context,
621+
loadedImageUri = imageUri,
622+
cropPoints = cropPoints,
623+
degreesRotated = mDegreesRotated,
624+
orgWidth = originalBitmap!!.width * loadedSampleSize,
625+
orgHeight = originalBitmap!!.height * loadedSampleSize,
626+
fixAspectRatio = mCropOverlayView!!.isFixAspectRatio,
627+
aspectRatioX = mCropOverlayView.aspectRatioX,
628+
aspectRatioY = mCropOverlayView.aspectRatioY,
629+
reqWidth = newReqWidth,
630+
reqHeight = newReqHeight,
631+
flipHorizontally = mFlipHorizontally,
632+
flipVertically = mFlipVertically,
633+
).bitmap
649634
} else {
650-
BitmapUtils
651-
.cropBitmapObjectHandleOOM(
652-
originalBitmap,
653-
cropPoints,
654-
mDegreesRotated,
655-
mCropOverlayView!!.isFixAspectRatio,
656-
mCropOverlayView.aspectRatioX,
657-
mCropOverlayView.aspectRatioY,
658-
mFlipHorizontally,
659-
mFlipVertically,
660-
)
661-
.bitmap
635+
BitmapUtils.cropBitmapObjectHandleOOM(
636+
bitmap = originalBitmap,
637+
cropPoints = cropPoints,
638+
degreesRotated = mDegreesRotated,
639+
fixAspectRatio = mCropOverlayView!!.isFixAspectRatio,
640+
aspectRatioX = mCropOverlayView.aspectRatioX,
641+
aspectRatioY = mCropOverlayView.aspectRatioY,
642+
flipHorizontally = mFlipHorizontally,
643+
flipVertically = mFlipVertically,
644+
).bitmap
662645
}
663-
croppedBitmap =
664-
BitmapUtils.resizeBitmap(croppedBitmap, newReqWidth, newReqHeight, options)
646+
647+
return BitmapUtils.resizeBitmap(
648+
bitmap = croppedBitmap,
649+
reqWidth = newReqWidth,
650+
reqHeight = newReqHeight,
651+
options = options,
652+
)
665653
}
666-
return croppedBitmap
654+
655+
return null
667656
}
668657

669658
/**

0 commit comments

Comments
 (0)