CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
Apache License 2.0
1.28k stars 263 forks source link

CanHub Image Cropper not taking up full crop image container #582

Open razer56 opened 1 year ago

razer56 commented 1 year ago

The problem I am facing is that an image that's 4000x6000 won't fill the cropImageView container fully. While an image that's 1440x3088 will. All I want to do is when a user uploads an image, it always fills the entire cropImageView container. If that image is smaller than the container size, then it should zoom all the way up until it takes up the full container size. How do I achieve this?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.canhub.cropper.CropImageView
            android:id="@+id/cropImageView"
            android:layout_width="match_parent"
            app:cropFixAspectRatio="false"
            android:layout_height="300dp" />

    </LinearLayout>

and here's the code of when I load the image into the cropImageView after I select an image from my camera roll:

private val cropActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val resultUri = result.data?.data
        if (resultUri != null) {
            // Set the cropped image to the CropImageView
            binding.cropImageView.setImageUriAsync(resultUri)

            // Apply the scale to the CropImageView
            binding.cropImageView.scaleType = CropImageView.ScaleType.CENTER_CROP
        }
    }
}
vanniktech commented 1 year ago

The aspect ratio probably? There are a few options to configure this thing for you though:

https://github.com/CanHub/Android-Image-Cropper/blob/main/cropper/src/main/kotlin/com/canhub/cropper/CropImageOptions.kt

razer56 commented 1 year ago

The aspect ratio probably? There are a few options to configure this thing for you though:

https://github.com/CanHub/Android-Image-Cropper/blob/main/cropper/src/main/kotlin/com/canhub/cropper/CropImageOptions.kt

I think an easier way to ask my question would be if I had a very small image. Say 50x50 pixels. It's much smaller than the cropImageView, so it would not take the full width or height. Is there a way to automatically zoom in so that it does take up the full width and height of the cropImageView

vanniktech commented 1 year ago

I think you might need to tweak maxZoom but it should be somehow supported yes.

razer56 commented 1 year ago

I think you might need to tweak maxZoom but it should be somehow supported yes.

I think tweaking scaleX and scaleY will do the trick, but as for my first issue I have an image that's 4000x6000....the aspect ratio is 0.66 and another image that's 1440x3088, its aspect ratio is 0.466. The CropImageView is set to the width of the parent and a height of 300dp. The 1440x3088 takes up the full width and height, while the 4000x6000 does not. I'm not sure why. An image that's 679x452 and has an aspect ratio of 1.50 will also fit the whole width and height of the CropImageView. Any images that are 0.66, 0.75, etc. do not fit the whole width and height of the CropImageView

vanniktech commented 3 months ago

@razer56 did you come back to this problem and found a solution? I still think for now the best solution is to calculcate the zoom factors yourself. You have all of the data (how big is your view/how big is the image).