CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.

Home Page:https://canhub.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

README update required

ArtRoman opened this issue · comments

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
                )
            )
        ) 

Can you please create a PR?

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)

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:

  • Added the necessary import for AppCompatActivity.
  • Adjusted the variable name croppedImageUri to be more descriptive.
  • Added this as the context parameter for getUriFilePath to ensure proper context reference.
  • Updated the way cropImage.launch is called by providing CropImageContractOptions objects that contain the necessary options for each scenario

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.