Reputation: 11408
Is there a way to resize a JPEG image file (from filesystem to filesystem) without removing meta data (just like ImageMagicks convert in.jpg -resize 50% out.jpg
)?
The only way I found to resize .jpg files I found is BitmapFactory.decodeStream
and Bitmap.compress
, which looks like a lot of overhead if I manually need to transfer image meta data.
Upvotes: 10
Views: 1161
Reputation: 12735
It may be possible to read the metadata with android.media.ExifInterface, compress the image via Bitmap.compress
and then save the metadata back:
String filename = "yourfile.jpg";
// this reads all meta data
ExifInterface exif = new ExifInterface(filename);
// read and compress file
Bitmap bitmap = BitmapFactory.decodeFile(filename);
FileOutputStream fos = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
// write meta data back
exif.saveAttributes();
I didn't test it, but looking at the source code of ExifInterface, it may work. If you don't want to rely on the implementation details, you could of course loop through all attributes and copy them.
Upvotes: 3