Convert Figma logo to code with AI

Yalantis logouCrop

Image Cropping Library for Android

11,849
2,152
11,849
287

Top Related Projects

📸Image Picker for Android, Pick an image from Gallery or Capture a new image with Camera

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

Android library project for cropping images

Android's TextView that can expand/collapse like the Google Play's app description

Quick Overview

uCrop is an Android library for image cropping. It provides a customizable UI for selecting and cropping portions of images, with features like rotation, scaling, and aspect ratio control. The library is designed to be easy to integrate into Android applications and offers a smooth user experience.

Pros

  • Highly customizable UI with various options for crop frame, grid overlay, and gestures
  • Supports both URI and Bitmap input for image sources
  • Offers precise control over aspect ratio and output image size
  • Provides smooth animations and gestures for an enhanced user experience

Cons

  • Limited to Android platform only
  • May require additional setup for handling large images to avoid OutOfMemoryError
  • Documentation could be more comprehensive, especially for advanced use cases
  • Some users report issues with specific image formats or orientations

Code Examples

  1. Basic usage with default settings:
UCrop.of(sourceUri, destinationUri)
    .start(activity)
  1. Customizing crop options:
UCrop.of(sourceUri, destinationUri)
    .withAspectRatio(16f, 9f)
    .withMaxResultSize(maxWidth, maxHeight)
    .start(activity)
  1. Applying custom UI options:
val options = UCrop.Options()
options.setCircleDimmedLayer(true)
options.setFreeStyleCropEnabled(true)

UCrop.of(sourceUri, destinationUri)
    .withOptions(options)
    .start(activity)

Getting Started

  1. Add the dependency to your app's build.gradle:
dependencies {
    implementation 'com.github.yalantis:ucrop:2.2.8'
}
  1. Add UCropActivity to your AndroidManifest.xml:
<activity
    android:name="com.yalantis.ucrop.UCropActivity"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
  1. Implement UCrop in your activity:
private fun startCrop(sourceUri: Uri) {
    val destinationUri = Uri.fromFile(File(cacheDir, "cropped_image.jpg"))
    UCrop.of(sourceUri, destinationUri)
        .start(this)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
        val resultUri = UCrop.getOutput(data!!)
        // Use the cropped image
    } else if (resultCode == UCrop.RESULT_ERROR) {
        val cropError = UCrop.getError(data!!)
        // Handle crop error
    }
}

Competitor Comparisons

📸Image Picker for Android, Pick an image from Gallery or Capture a new image with Camera

Pros of ImagePicker

  • Offers a more comprehensive solution, including image selection, cropping, and compression
  • Provides an easier-to-use API with fewer configuration steps
  • Supports both gallery and camera image capture out of the box

Cons of ImagePicker

  • Less customizable cropping options compared to uCrop
  • May have a larger footprint due to additional features
  • Potentially slower performance for specific cropping tasks

Code Comparison

ImagePicker:

ImagePicker.with(this)
    .crop()
    .compress(1024)
    .maxResultSize(1080, 1080)
    .start()

uCrop:

UCrop.of(sourceUri, destinationUri)
    .withAspectRatio(1f, 1f)
    .withMaxResultSize(1080, 1080)
    .start(this)

ImagePicker provides a more concise API for common use cases, while uCrop offers more granular control over the cropping process. ImagePicker includes compression and selection features in a single call, whereas uCrop focuses solely on image cropping with advanced customization options.

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

Pros of Android-Image-Cropper

  • More customizable UI options, including overlay shapes and guidelines
  • Supports both bitmap and URI image sources
  • Includes built-in image rotation and flipping features

Cons of Android-Image-Cropper

  • Less optimized for performance, especially with large images
  • Limited animation options compared to uCrop
  • Fewer options for aspect ratio presets

Code Comparison

uCrop:

UCrop.of(sourceUri, destinationUri)
    .withAspectRatio(16f, 9f)
    .withMaxResultSize(maxWidth, maxHeight)
    .start(this)

Android-Image-Cropper:

CropImage.activity(imageUri)
    .setGuidelines(CropImageView.Guidelines.ON)
    .setAspectRatio(1, 1)
    .start(this)

Both libraries offer simple implementation, but uCrop provides more granular control over the result size, while Android-Image-Cropper focuses on UI customization options.

Android library project for cropping images

Pros of android-crop

  • Simpler implementation, easier to integrate for basic cropping needs
  • Lightweight library with minimal dependencies
  • Supports older Android versions (API 10+)

Cons of android-crop

  • Limited customization options compared to uCrop
  • Less active development and community support
  • Lacks advanced features like rotation, scaling, and aspect ratio presets

Code Comparison

android-crop:

new Crop(inputUri)
    .output(outputUri)
    .asSquare()
    .start(activity);

uCrop:

UCrop.of(inputUri, outputUri)
    .withAspectRatio(1, 1)
    .withMaxResultSize(maxWidth, maxHeight)
    .start(activity);

Both libraries offer simple API calls for basic cropping, but uCrop provides more options for customization and advanced features. uCrop's method chaining allows for easier configuration of multiple parameters, while android-crop focuses on simplicity with fewer options.

uCrop offers a more modern and feature-rich approach to image cropping on Android, with active development and a larger community. It provides extensive customization options, gesture controls, and a polished UI. However, android-crop may be suitable for projects requiring a lightweight solution or compatibility with older Android versions.

Android's TextView that can expand/collapse like the Google Play's app description

Pros of ExpandableTextView

  • Specifically designed for text expansion, providing a seamless reading experience
  • Lightweight and focused on a single functionality
  • Easy to implement and customize for text-heavy applications

Cons of ExpandableTextView

  • Limited to text manipulation, lacking image processing capabilities
  • May require additional libraries for more complex UI interactions
  • Less actively maintained compared to uCrop

Code Comparison

ExpandableTextView implementation:

ExpandableTextView expTv1 = (ExpandableTextView) findViewById(R.id.expand_text_view);
expTv1.setText(getString(R.string.dummy_text1));

uCrop implementation:

UCrop.of(sourceUri, destinationUri)
    .withAspectRatio(16, 9)
    .withMaxResultSize(maxWidth, maxHeight)
    .start(this);

ExpandableTextView focuses on text manipulation, offering a simple API for expanding and collapsing text. uCrop, on the other hand, provides a comprehensive image cropping solution with various customization options. While ExpandableTextView is ideal for text-centric applications, uCrop excels in image processing tasks, making it more suitable for photo editing and manipulation scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

uCrop - Image Cropping Library for Android

This project aims to provide an ultimate and flexible image cropping experience. Made in Yalantis

How We Created uCrop

Check this project on Dribbble

Usage

For a working implementation, please have a look at the Sample Project - sample

Get it on Google Play

  1. Include the library as a local library project.

    allprojects {
       repositories {
          jcenter()
          maven { url "https://jitpack.io" }
       }
    }
    

    implementation 'com.github.yalantis:ucrop:2.2.8' - lightweight general solution

    implementation 'com.github.yalantis:ucrop:2.2.8-native' - get power of the native code to preserve image quality (+ about 1.5 MB to an apk size)

  2. Add UCropActivity into your AndroidManifest.xml

    <activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
    
  3. The uCrop configuration is created using the builder pattern.

    UCrop.of(sourceUri, destinationUri)
        .withAspectRatio(16, 9)
        .withMaxResultSize(maxWidth, maxHeight)
        .start(context);
    
  4. Override onActivityResult method and handle uCrop result.

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
            final Uri resultUri = UCrop.getOutput(data);
        } else if (resultCode == UCrop.RESULT_ERROR) {
            final Throwable cropError = UCrop.getError(data);
        }
    }
    
  5. You may want to add this to your PROGUARD config:

    -dontwarn com.yalantis.ucrop**
    -keep class com.yalantis.ucrop** { *; }
    -keep interface com.yalantis.ucrop** { *; }
    

Customization

If you want to let your users choose crop ratio dynamically, just do not call withAspectRatio(x, y).

uCrop builder class has method withOptions(UCrop.Options options) which extends library configurations.

Currently, you can change:

  • image compression format (e.g. PNG, JPEG), compression
  • image compression quality [0 - 100]. PNG which is lossless, will ignore the quality setting.
  • whether all gestures are enabled simultaneously
  • maximum size for Bitmap that is decoded from source Uri and used within crop view. If you want to override the default behaviour.
  • toggle whether to show crop frame/guidelines
  • setup color/width/count of crop frame/rows/columns
  • choose whether you want rectangle or oval(options.setCircleDimmedLayer(true)) crop area
  • the UI colors (Toolbar, StatusBar, active widget state)
  • and more...

Compatibility

  • Library - Android ICS 4.0+ (API 14) (Android GINGERBREAD 2.3+ (API 10) for versions <= 1.3.2)
  • Sample - Android ICS 4.0+ (API 14)
  • CPU - armeabi armeabi-v7a x86 x86_64 arm64-v8a (for versions >= 2.1.2)

Changelog

Version: 2.2.9

  • Update compileSdk and targetSdk versions up to 33
  • Fixed #867
  • Fixed #873
  • And other improvements

Version: 2.2.8

  • Merged pending pull requests with improvements and bugfixes
  • Update compileSdk and targetSdk versions up to 31
  • Add localizations
  • Fixed #609
  • Fixed #794

Version: 2.2.5

Version: 2.2.4

  • AndroidX migration
  • Redesign
  • Several fixes including #550

Version: 2.2.3

  • Several fixes including #445, #465 and more!
  • Material design support
  • uCrop fragment as child fragment
  • Added the Italian language

Version: 2.2.2

  • uCrop fragment added
  • bugfix

Version: 2.2.1

  • Fix including #285

Version: 2.2

Version: 2.1

  • Fixes issue with EXIF data (images taken on front camera with Samsung devices mostly) #130 #111
  • Added API to set custom set of aspect ratio options for the user. #131
  • Added API to set all configs via UCrop.Options class. #126
  • Added ABI x86_64 support. #105

Version: 2.0

  • Native image crop (able to crop high-resolution images, e.g. 16MP & 32MP images on Nexus 5X).
  • WebP compression format is not supported at the moment (choose JPEG or PNG).
  • Now library copies EXIF data to cropped image (size and orientation are updated).

Version: 1.5

  • Introduced "Freestyle" crop (you can resize crop rectangle by dragging it corners) #32
  • Now image & crop view paddings are not associated #68
  • Updated API

Version: 1.4

  • Introduced HTTP(s) Uri support!
  • Image is cropped in a background thread.
  • Showing loader while Bitmap is processed (both loading and cropping).
  • Several bug fixes.
  • Couple new things to configure.
  • Updated minSdkVersion to Android ICS 4.0 (no reason to support couple percents of old phones).

Version: 1.3

  • Image is loaded in a background thread. Better error-handling for image decoding.

  • Improved EXIF data support (rotation and mirror).

  • Small UI updates.

  • Couple new things to configure.

  • Sample updated with the possibility to choose custom aspect ratio.

Version: 1.2

  • Updated core logic so an image corrects its position smoothly and obviously.

Version: 1.1

  • UCrop builder was updated and now UCrop.Options class has even more values to setup.

Version: 1.0

  • Initial Build

Let us know!

We’d be really happy if you sent us links to your projects where you use our component. Just send an email to github@yalantis.com And do let us know if you have any questions or suggestion regarding the library.

Apps using uCrop

License

Copyright 2017, Yalantis

Software doesn't collect, store or transfer data to Yalantis or third parties.
Emplacement of this Software is carried out locally at device.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.