In my application I’m using the ACTION_IMAGE_CAPTURE Intent to get a picture taken. When the camera returns, the file is checked and if the rotation is portrait a the bitmap is rotated and saved out to disk with the following code:
BitmapFactory.Options options = new Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath(), options);
if (bmp != null) {
Matrix m = new Matrix();
m.postRotate(90);
Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), m,
true);
rotated = rotated.copy(Bitmap.Config.RGB_565, false); // added based on comment
f.delete();
FileOutputStream fos = new FileOutputStream(f);
rotated.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
This works as it should, but the file size is twice that of non-rotated picture. I’ve tried setting the densities on the BitmapFactory.Options to 0 and the scale to false, but neither is having the desired effect. I want the image I transform to be the same size as the image I load from disk. Is there something in my code that is preventing this from happening?
Your original JPEG uses RGB565, which uses 2 bytes per pixel. Your in-memory bitmap derived from this file uses the “normal” format with 4 bytes per pixel; when this is saved to a new JPEG, it’s saved with the denser format and thus is twice the size (this has nothing to do with its being rotated).