Thursday, June 23, 2011

[android-developers] croping, scaling and rotating a Bitmap make my app crash

Hi,

I am developing an app that uses the camera.. when i get the image i
wan to crop, scale and rotate the Bitmap but my app crashes because
memory usage.. i am testing on a HTC magic.. (XDD i know..), this is
my code but maybe ther is a best way to do it..


public Bitmap cropBitmap(Bitmap original) {

int porwidth = original.getWidth();
if (original.getHeight() < original.getWidth()){
porwidth = original.getHeight();
}

Bitmap croppedImage = Bitmap.createBitmap(porwidth, porwidth,
Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(croppedImage);

Rect srcRect = new Rect(0, 0, original.getWidth(),
original.getHeight());
Rect dstRect = new Rect(0, 0, porwidth, porwidth);

int dx = (srcRect.width() - dstRect.width()) / 2;
int dy = (srcRect.height() - dstRect.height()) / 2;

// If the srcRect is too big, use the center part of it.
srcRect.inset(Math.max(0, dx), Math.max(0, dy));

// If the dstRect is too big, use the center part of it.
dstRect.inset(Math.max(0, -dx), Math.max(0, -dy));

// Draw the cropped bitmap in the center
canvas.drawBitmap(original, srcRect, dstRect, null);
original.recycle();

int newporwidth = porwidth;
if(newporwidth > 640){

newporwidth = 640;
}

float scaleWidth = ((float) newporwidth) / porwidth;
float scaleHeight = ((float) newporwidth) / porwidth;

// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(90);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(croppedImage, 0,
0, porwidth, porwidth, matrix, true);

croppedImage.recycle();
return resizedBitmap;
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate