Last active
August 29, 2015 14:23
-
-
Save jenslohmann/f19529987b03f411ae94 to your computer and use it in GitHub Desktop.
Preserve EXIF data in jpeg when resizing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.sanselan.ImageReadException; | |
import org.apache.sanselan.ImageWriteException; | |
import org.apache.sanselan.common.IImageMetadata; | |
import org.apache.sanselan.formats.jpeg.JpegImageMetadata; | |
import org.apache.sanselan.formats.jpeg.JpegImageParser; | |
import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter; | |
import org.apache.sanselan.formats.tiff.TiffImageMetadata; | |
public SomeClass { | |
public static TiffImageMetadata readExifMetadata(byte[] jpegData) throws ImageReadException, IOException { | |
IImageMetadata imageMetadata = new JpegImageParser().getMetadata(jpegData, null); | |
if (imageMetadata == null) { | |
return null; | |
} | |
JpegImageMetadata jpegMetadata = (JpegImageMetadata) imageMetadata; | |
TiffImageMetadata exif = jpegMetadata.getExif(); | |
if (exif == null) { | |
return null; | |
} | |
return exif; | |
} | |
public static byte[] writeExifMetadata(TiffImageMetadata metadata, byte[] jpegData) | |
throws ImageReadException, ImageWriteException, IOException { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
new ExifRewriter().updateExifMetadataLossless(jpegData, out, metadata.getOutputSet()); | |
out.close(); | |
return out.toByteArray(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependency> | |
<groupId>org.apache.sanselan</groupId> | |
<artifactId>sanselan</artifactId> | |
<version>0.97-incubator</version> | |
</dependency> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment