Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize JPEG image in filesystem

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.

like image 635
Simon Warta Avatar asked Nov 21 '16 20:11

Simon Warta


People also ask

How do I reduce the size of a JPEG in a batch file?

To batch-convert the images you press "Batch" button, add your images, choose "Additional tasks" -> "Compress to size" option, set desired filesize threshold and start the process.


1 Answers

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.


Another solution would be to use the Android Exif Extended library. It is a small project to read and write exif data.
like image 102
Manuel Allenspach Avatar answered Oct 07 '22 00:10

Manuel Allenspach