Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate Bitmap without creating a new one?

I'm using this to get rotated Bitmap from existed one :

private Bitmap getRotatedBitmap(Bitmap bitmap, int angle) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix mtx = new Matrix();
        mtx.postRotate(angle);
        return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
    }

Is it possible to do it without creating new bitmap?

I was trying to redraw the same mutable image with Canvas:

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(targetBitmap, matrix, new Paint());

but this approach had just resulted in corrupted bitmap. So is there any possibilities to achieve it?

like image 543
amukhachov Avatar asked Sep 21 '12 11:09

amukhachov


1 Answers

This is the simplest rotation on canvas code, without creating a new bitmap

canvas.save(); //save the position of the canvas
canvas.rotate(angle, X + (imageW / 2), Y + (imageH / 2)); //rotate the canvas
canvas.drawBitmap(imageBmp, X, Y, null); //draw the image on the rotated canvas
canvas.restore();  // restore the canvas position.
like image 119
Ronnie Avatar answered Oct 07 '22 15:10

Ronnie