Camposha
  • Android
    • Introduction
    • Components
    • Communication
    • DateTime
    • FileSystem
    • HTTP
    • Local Database
    • Permissions
    • Scheduler
    • System
    • Widgets
    • Threading
    • Animation and Motion
    • Hardware and Sensors
  • C#
  • Flutter
  • Java
  • Javascript
  • Python
Sunday, January 17, 2021
No Result
View All Result
  • Login
  • Register
Buy Projects
  • Android
    • Introduction
    • Components
    • Communication
    • DateTime
    • FileSystem
    • HTTP
    • Local Database
    • Permissions
    • Scheduler
    • System
    • Widgets
    • Threading
    • Animation and Motion
    • Hardware and Sensors
  • C#
  • Flutter
  • Java
  • Javascript
  • Python
No Result
View All Result
Camposha
No Result
View All Result
Home Android Hardware and Sensors

Android: How to crop images

Oclemy by Oclemy
2 months ago
in Hardware and Sensors
A A
4k
VIEWS
Share on FacebookShare on Twitter

When you are capturing images from the Camera, you sometimes need to crop those images before saving them in external storage or uploading them to the server. Luckily there are already great and modern solutions to allow us integrate cropping capability in our android apps, be it via Kotlin or Java.

In this piece we will look at these options, how to install them and code snippets of how to use them.

(a). Croppy

This is a modern Image Cropping Library for Android with several cool features. Here are it’s features:

Double tap focus point_up_2point_up_2

It zooms-in to the touch points on double tap. Restores default state when user double taps on max scale state.

Pinch Zoom ok_hand

Zoom in and out with two finger.

Free Mode love_you_gesture

You can crop your image in free mode. In any size.

Aspect Ratio Mode iphone

Enhanced aspect ratio mode will help you while cropping. Aspect ratio will be fixed while you play with cropper. So for your social media apps, it will help you to crop in fixed size (instagram, facebook, twitter, 16:9, 1:2, 3:2 and more..)

Size Displayer keycap_ten

While you scale your image, size displayer indicates the bitmap size reactively. It will provide a smoother experience to the user.

Auto Centered heart_eyes

What ever you do while cropping, we center the bitmap with animation. Smoother experience for user.

Animations star2

We use animation everywhere in this cropper. User zoomed-out too much? We zoom back in with animation. User scrolled image out of borders? We scroll it back with animation.

 

Steps for using Croppy

Step 1: Installation

First install Croppy from jitpack:

allprojects {
     repositories {
	...
	maven { url 'https://jitpack.io' }
     }
}
dependencies {
      implementation 'com.github.lyrebirdstudio:Croppy:0.2'
}

Step 2: Basic usage

Croppy is very simple to use. You just need to start it:

//Start croppy (source uri is the original image.)
val cropRequest = CropRequest.Auto(sourceUri = uri, requestCode = 101)
Croppy.start(this, cropRequest)

Then listen to results:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      if (requestCode == 101) {
           imageview.setImageURI(data.data)
     }
}

Step 3: Custom Usage

For more advanced usage you can for example save the cropped result in the external storage:

val externalCropRequest = CropRequest.Auto(sourceUri = uri, requestCode = RC_CROP_IMAGE)

You can also save it in the cache:

val cacheCropRequest = CropRequest.Auto(
     sourceUri = uri,
     requestCode = RC_CROP_IMAGE,
     storageType = StorageType.CACHE
)

You can also specify a custom destination for the cropped image:

val destinationUri = ...
val manuelCropRequest = CropRequest.Manual(
    sourceUri = uri,
    destinationUri = destinationUri,
    requestCode = RC_CROP_IMAGE
)

If you want to exclude some specific aspect ratio from bottom aspect ratio list view.

val excludeAspectRatiosCropRequest = CropRequest.Manual(
    sourceUri = uri,     
    destinationUri = destinationUri,
    requestCode = RC_CROP_IMAGE,
    excludedAspectRatios = arrayListOf(AspectRatio.ASPECT_FREE)
)

If you want to give specific theme as accent color.

val themeCropRequest = CropRequest.Manual(
    sourceUri = uri,
    destinationUri = destinationUri,
    requestCode = RC_CROP_IMAGE,
    croppyTheme = CroppyTheme(R.color.blue)
)

Ultimately you have to start croppy:

//Start croppy with your custom request.
Croppy.start(this, cropRequest)

Demo

Here is the demo:

 

Links

  1. Download the example here.
  2. Follow the author here.
Tags: Android Camera Examples
Share16Tweet10Send
Oclemy

Oclemy

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

Get Free Projects

Featured Projects

  • Become a Premium Member $30.00
  • CoronaVirus News App - Kotlin+MVVM+Firebase+Cloud Storage+Authentication+PageViews $12.00 $4.99
  • Largest Stars App - Kotlin+MySQL+MVVM+Retrofit2 Multipart+Data Binding+Disk Caching(2 Apps-Kotlin,Java) $12.00 $4.99
  • Alien Planets App - MVVM+Firebase+Cloud Storage(2 Apps-Kotlin,Java) $10.00 $4.99
  • Retrofit MySQL Multipart Images CRUD - UPLOAD DOWNLOAD UPDATE DELETE Full $10.00 $3.99

My Udemy Courses

Android MySQL Retrofit2 Multipart CRUD,Search,Pagination€19.99€14.99
Beginner Friendly Full Offline Android Apps Creation(Room)€19.99€14.99
Android Firebase MVVM Jetpack - Many Offline-First CRUD Apps€19.99€14.99
Kotlin Firebase CRUD,Cloud Storage,MVVM (Works even Offline)€19.99€14.99

Popular

  • Flutter PHP MySQL – Fill ListView with Images and Text

    45 shares
    Share 18 Tweet 11
  • Kotlin Android SQLite Simple CRUD – INSERT SELECT UPDATE DELETE

    45 shares
    Share 18 Tweet 11
  • Flutter SearchView Examples – ListView Search/Filter

    45 shares
    Share 18 Tweet 11
  • Kotlin Android Capture From Camera or Pick Image

    45 shares
    Share 18 Tweet 11
  • Android MySQL – Insert From EditText,CheckBox,Spinner – Part 4 [PHP Code]

    43 shares
    Share 17 Tweet 11
  • Trending
  • Comments
  • Latest

Flutter PHP MySQL – Fill ListView with Images and Text

December 19, 2020

Kotlin Android SQLite Simple CRUD – INSERT SELECT UPDATE DELETE

December 19, 2020
Flutter AppBar SearchView

Flutter SearchView Examples – ListView Search/Filter

December 19, 2020
Kotlin Android Capture From Camera or Pick Image

Kotlin Android Capture From Camera or Pick Image

December 19, 2020
CoronaVirus News App – Kotlin + MVVM + Firebase + Cloud Storage + Authentication+PageViews Count(Works even Offline)

CoronaVirus News App – Kotlin + MVVM + Firebase + Cloud Storage + Authentication+PageViews Count(Works even Offline)

10

Android Firebase RecyclerView MasterDetail CRUD Images Text – Upload, READ, DELETE

5

Android Retrofit – JSON ListView Images and Text

3

Framework7 Cordova – Compile App to APK and Install in Android Device/Emulator

3

Android Shared Element Transition Examples

December 27, 2020

Best Android Swipe Cards Libraries and Examples

December 27, 2020
Retrofit

Retrofit

December 19, 2020
Fast Networking Library

Fast Networking Library

December 19, 2020
  • Android
  • C#
  • Flutter
  • Java
  • Javascript
  • Python

Copyright © 2020 Camposha All Rights Reserved.

No Result
View All Result
  • Account
  • Activate
  • Activity
  • Become a Teacher
  • Become a Teacher
  • Become a Teacher
  • Become instructor
  • Blog
  • Blog
  • Cancel Payment
  • Cancel Payment
  • Cart
  • Change Password
  • Change Password
  • Checkout
  • Checkout
  • Checkout
  • Contact
  • Contact
  • Contact Us
  • Content restricted
  • Course Checkout
  • Dashboard
  • Edit Profile
  • Edit Profile
  • FAQs
  • Forgot Password
  • Forgot Password
  • Guest
  • Guest
  • Home
  • Home
  • Home Light
  • Instructor Dashboard
  • Instructor Registration
  • IUMP – Account Page
  • IUMP – Default Redirect Page
  • IUMP – Login
  • IUMP – LogOut
  • IUMP – Register
  • IUMP – Reset Password
  • IUMP – TOS Page
  • IUMP – Visitor Inside User Page
  • List courses
  • List wish list
  • Login
  • Login
  • Maintenance
  • Members
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Account
    • Membership Billing
    • Membership Cancel
    • Membership Checkout
    • Membership Confirmation
    • Membership Invoice
    • Membership Levels
  • Membership Plans
  • My Account
  • OnePage Documentation
  • Portfolio Grid
  • Portfolio Masonry
  • Portfolio Multigrid
  • Privacy Policy
  • Products
  • Profile
  • Profile
  • Profile
  • Projects
  • Register
  • Register
  • Register
  • Register
  • Sample Page
  • Shop
  • Sign in
  • Sign up
  • Student profile
  • Student Registration
  • Thank You
  • Thank You

Copyright © 2020 Camposha All Rights Reserved.

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Login

Forgot Password?

Create a new account

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.