chirag
chirag

Reputation: 57

Splitting a bitmap into two equal halves in Android

How do I split a bitmap into two equal halves in Android. I already have the image captured and saved in internal storage. The goal is to split the captured face into two and calculate brightness distribution of two halves of the image. I already did search for this but all I found was splitting the layout. I want to split the image based on width if it is in landscape and based on height if it is in portrait. Please help. Thanks!

Upvotes: 1

Views: 1169

Answers (1)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

You can draw it on a Canvas half it size each and save it again.

Bitmap bmp = //load your bmp from system
int half = bmp.getWidth() / 2;
Bitmap half1 = new Bitmap(half, bmp.getHeight(), bmp.getConfig());
Canvas c1 = new Canvas(half1);
c1.drawBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight());
//Save half1
Bitmap half2 = new Bitmap(half, bmp.getHeight(), bmp.getConfig());
Canvas c2 = new Canvas(half2);
c2.drawBitmap(bmp, -half, 0, bmp.getWidth() - half, bmp.getHeight());
//Save half2

Upvotes: 1

Related Questions