CanHub / Android-Image-Cropper

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

README update required #585

Closed ArtRoman closed 2 months ago

ArtRoman commented 1 year ago

Current documentation is misleading, this code in README.md will be reported with an "Unresolved reference" errors:

  cropImage.launch(
      options(uri = imageUri) {
        setGuidelines(Guidelines.ON)
        setOutputCompressFormat(CompressFormat.PNG)
      }
    )
  }

It requires new launch and options objects in 4.3.3 and later:

        cropImage.launch(
            CropImageContractOptions(
                uri = imageUri,
                CropImageOptions(
                    outputCompressFormat = Bitmap.CompressFormat.PNG,
                    // other params
                )
            )
        ) 
vanniktech commented 1 year ago

Can you please create a PR?

harshsingh-io commented 1 year ago

The cropImage.launch() method was changed in version 4.3.3 of the Android Image Cropper library. The old method used a options() block to set the crop options, but this block is no longer supported. In the new method, you need to pass a CropImageContractOptions object to the launch() method. This object contains the crop options, including the output compression format.

However, in newer versions, you will need to use the new launch() method.

Here is an example of how to use the new launch() method:


val cropImageOptions = CropImageOptions.Builder()
    .setOutputCompressFormat(Bitmap.CompressFormat.PNG)
    .build()

val cropImageContractOptions = CropImageContractOptions(
    uri = imageUri,
     cropImageOptions = cropImageOptions
)

cropImage.launch(cropImageContractOptions)
harshsingh-io commented 1 year ago

Here's the modified version of the code:

class MainActivity : AppCompatActivity() {

    private val cropImage = registerForActivityResult(CropImageContract()) { result ->
        if (result.isSuccessful) {
            // Use the cropped image URI.
            val croppedImageUri = result.uriContent
            val croppedImageFilePath = result.getUriFilePath(this) // optional usage
            // Process the cropped image URI as needed.
        } else {
            // An error occurred.
            val exception = result.error
            // Handle the error.
        }
    }

    private fun startCrop() {
        // Start cropping activity with guidelines.
        cropImage.launch(
            CropImageContractOptions(
                cropImageOptions = CropImageOptions(
                    guidelines = Guidelines.ON
                )
            )
        )

        // Start cropping activity with gallery picker only.
        cropImage.launch(
            CropImageContractOptions(
                pickImageContractOptions = PickImageContractOptions(
                    includeGallery = true,
                    includeCamera = false
                )
            )
        )

        // Start cropping activity for a pre-acquired image with custom settings.
        cropImage.launch(
            CropImageContractOptions(
                uri = imageUri,
                cropImageOptions = CropImageOptions(
                    guidelines = Guidelines.ON,
                    outputCompressFormat = Bitmap.CompressFormat.PNG
                )
            )
        )
    }

    // Call the startCrop function when needed.
}

In this code, I've made the following changes:

ArtRoman commented 1 year ago

Here's the modified version of the code:

Thank you, I haven't got any problems with modifying code. I use most of available options, so my changelog is quite larger:

cropImage.launch(
    options(uri) {
        setGuidelines(CropImageView.Guidelines.ON_TOUCH)
        setActivityTitle(getString(R.string.profile_select_image))
        setCropShape(CropImageView.CropShape.RECTANGLE)
        setFixAspectRatio(false)
        setInitialCropWindowPaddingRatio(0f)
        setCropMenuCropButtonTitle(getString(R.string.send))
        setRequestedSize(Constants.MAX_PHOTO_SIZE, Constants.MAX_PHOTO_SIZE, CropImageView.RequestSizeOptions.RESIZE_INSIDE)
        setOutputCompressFormat(Bitmap.CompressFormat.JPEG)
    }
)

to

cropImage.launch(
    CropImageContractOptions(
        uri, CropImageOptions(
            guidelines = CropImageView.Guidelines.ON_TOUCH,
            activityTitle = getString(R.string.profile_select_image),
            cropShape = CropImageView.CropShape.RECTANGLE,
            fixAspectRatio = false,
            initialCropWindowPaddingRatio = 0f,
            cropMenuCropButtonTitle = getString(R.string.send),
            outputRequestSizeOptions = CropImageView.RequestSizeOptions.RESIZE_INSIDE,
            outputRequestWidth = Constants.MAX_PHOTO_SIZE,
            outputRequestHeight = Constants.MAX_PHOTO_SIZE,
            outputCompressFormat = Bitmap.CompressFormat.JPEG,
        )
    )
)

The issue is about misleading documentation in the project's welcome page of this repository, which may disappoint new library users.

vanniktech commented 2 months ago

@harshsingh-io thanks for the corrections. I've created a PR :)

harshsingh-io commented 2 months ago

@vanniktech I hope we can work together. I was not free at that moment. That's why I was not able to create PR of it.