Arutha
Arutha

Reputation: 26488

Rotate an image file without changing file size

I want to rotate an image file (photo) without changing file size :

but the size of my original file has doubled !!

Upvotes: 2

Views: 4302

Answers (2)

Moss
Moss

Reputation: 6012

Apart from the comppression format try to create the Bitmap with the same config as the src image. I would suggest a RGB_565: http://developer.android.com/reference/android/graphics/Bitmap.Config.html

This would result in a better values as the default one.

mCanvas = new Canvas();
mBitmap = Bitmap.createBitmap(this.width, this.height,
    Bitmap.Config.RGB_565);
mCanvas.setBitmap(mBitmap);

Now if you render any think to mCanvas it will be saved in the bitmap:

mCanvas.save();
mCanvas.translate or roate
mCanvas.drawBitmap(<your real bitmap here>)
mCanvas.restore();

Now mBitmap should have all you need (and the size).

Upvotes: 0

johusman
johusman

Reputation: 3472

The image you load is encoded in a certain format (e.g. JPEG) and with a certain compression quality (where better quality equals larger file size). When you save it after rotating, you are specifying a potentially completely different format and compression quality, hence the size difference.

I don't think there is any way you can guarantee the same size unless both the input and output files are uncompressed bitmaps, but you can probably at least try to figure out what format and quality was used for the original image and use those settings when you save the file again using bitmap.compress . That should give you a file size in the same ballpark at least.

Upvotes: 4

Related Questions