Home » Android » Android auto crop camera captured images

Android auto crop camera captured images

Posted by: admin May 14, 2020 Leave a comment


I am looking for some kind of auto trim/crop functionality in android.
Which detects a object in captured image and creates a square box around object for
cropping. I have found face detection apis in android, but my problem is captured images are documents/pages not human faces so how can I detected documents or any other object from captured picture.

I am thinking of any algorithms for object detection or some color detection. Is there any apis or libraries available for it.

I have tried following link but not found any desired output.

Find and Crop relevant image area automatically (Java / Android)


Any small hint would also help me alot. Please help. Thanks in advance

How to&Answers:

That depends on what you intend to capture and crop, but there are many ways to achieve this. Like littleimp suggested, you should use OpenCv for the effect.

I suggest you use edge-detection algorithms, such as Sobel, and perform image transformation on it with, for example, a Threshold function that will turn the image into a binary one (only black and white). Afterwards, you can search the image for the geometric shape you want, using what’s suggested here. Filter the object you want by calculating the detected geometric figure’s area and ratio.

It would help a lot to know what you’re trying to detect in an image. Those methods I described were the ones I used for my specific case, which was developing an algorithm to detect and crop the license plate from a given vehicle image. It works close to perfect and it was all done by using OpenCV.

If you have anything else you’d like to know, don’t hesitate to ask. I’m watching this post 🙂


Use OpenCV for android.

You can use the Watershed (Imgproc.watershed) function to segment the image into foreground and background. Then you can crop around the foreground (which will be the document).

The watershed algorithm needs some markers pre-defining the regions. You can for example assume the document to be in the middle of the image, so create a marked region in the middle of the image to get the watershed algorithm started.