Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop bottom portion of a bitmap programmatically

Tags:

android

I have a bitmap taken from a camera. I want to crop the image so it only leaves the bottom portion of it. The cropped image should be 80% less the height of the original bitmap, so I want only the 20% of the bottom part starting from the left edge.

I'm doing this explicitly in the code without any Android cropping intent whatsoever.

An image to visualize what I want to achieve:

crop this

I've managed to crop the top part of the bitmap by using this code:

final Bitmap toBeCropped = BitmapFactory.decodeFile(mFile.getPath());

final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inTargetDensity = 1;
toBeCropped.setDensity(Bitmap.DENSITY_NONE);

int fromHere = (int) (toBeCropped.getHeight() * 0.2);
Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, 0, toBeCropped.getWidth(), fromHere);
mPreviewHalf.setImageBitmap(croppedBitmap);

But I couldn't find a way to start the cropping 80% from the top. I'm thinking of getting the y-coordinate of the Bitmap, so that I could crop any image sizes and always get the bottom portion only. But can anyone point to me how do I get this coordinate from a bitmap? Or do I have to take it from the layout itself?

like image 465
emen Avatar asked Aug 19 '16 08:08

emen


3 Answers

I am not familiar with operations on Bitmaps but from inspecting your code and looking at the API my guess would be that you need to specify the y coordinates on the following line to match the starting point:

Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, "here", toBeCropped.getWidth(), fromHere);

So my guess would be something like the following:

Bitmap croppedBitmap = Bitmap.createBitmap(toBeCropped, 0, (toBeCropped.getHeight() * 0.8), toBeCropped.getWidth(), fromHere);

in this case fromHere will define the number of rows you want to crop not the starting point (which is 20% of the total as you have pointed out)

like image 140
HarryS Avatar answered Oct 23 '22 19:10

HarryS


This is how I do it:

topcutoff is what you want to cut of on top of the image and buttomcutoff on the buttom (if needed)

height = height - topcutoff;
height = height - bottomcutoff;
croppedBitmap = Bitmap.createBitmap(croppedBitmap, 0, topcutoff, width, height);

Basically you just set a startpoint (topcutoff) from where to begin displaying the bitmap. In your case this would be the position after 80% of your bitmap.

This might also explain some things: Google Bitmap Documentation

"int: The y coordinate of the first pixel in source", so where you want to begin displaying your image.

like image 20
Johannes Muenichsdorfer Avatar answered Oct 23 '22 20:10

Johannes Muenichsdorfer


please use my code according to your need i hope its working

if (srcBmp.getWidth() >= srcBmp.getHeight()){

  dstBmp = Bitmap.createBitmap(
     srcBmp, 
     srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
     0,
     srcBmp.getHeight(), 
     srcBmp.getHeight()
     );

}else{

  dstBmp = Bitmap.createBitmap(
     srcBmp,
     0, 
     srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
     srcBmp.getWidth(),
     srcBmp.getWidth() 
     );
}
like image 29
Riyaz Parasara Avatar answered Oct 23 '22 19:10

Riyaz Parasara