noli
noli

Reputation: 3745

Lossless image rotation

I'm currently trying to implement a rotate feature in my app wich is playing with images.

The feature would allow the user to rotate an image -90 and +90 degree (switching landscape/portrait mode)

Here is my code :

public Bitmap rotateRight(Bitmap bm) {
    Matrix matrix = new Matrix();
    matrix.postRotate((float)90);

    Bitmap nbm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    nbm.setDensity(bm.getDensity());
    return nbm; 
}

After that, i save the image to the filesystem.

This work perfectly but the problem appears if i try to rotate 5 or 6 times the same image. The image quality will decrease and I will finally have a very ugly image to show...

Could you help me ? Thank you in advance !

Upvotes: 1

Views: 921

Answers (1)

Jave
Jave

Reputation: 31846

First: the last parameter in createBitmap() is whether to filter the resulting bitmap. As it is set to true, every time you perform this action your bitmap will become slightly more "blurred". As you do not change the size of the Bitmap, you might want to turn this off.

Secondly: make sure you save the image as a lossless format, such as PNG. If you save as JPG or another lossy format you will always reduce quality with each save, even if you set quality parameters to highest.

Upvotes: 2

Related Questions