Tuesday, March 20, 2012

How to Attach EXIF metadata to a serialized Bitmap in Android?


After uploading image to server from Android mobile, I also needed exif information of the image. EXIF information are metadata about image file. It contains information like, Camera name, date taken, Longitude and latitude etc.
After 2.0 SDK of Android, we have exifinterface available to read exif information of the file stored in SD card.
I read a file and get Bitmap object and then I compress it to get byte array. But while compressing it, I loose this information. Thanks to Sanselan, Apache project, By adding this library, I am now able to add exif information to byte array.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos); //Bitmap object is your image
byte[] data = bos.toByteArray();

TiffOutputSet outputSet = null;

IImageMetadata metadata = Sanselan.getMetadata(new File(filepath)); // filepath is the path to your image file stored in SD card (which contains exif info)
JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
if (null != jpegMetadata)
{
    TiffImageMetadata exif = jpegMetadata.getExif();
    if (null != exif)
    {
        outputSet = exif.getOutputSet();
    }
}
if (null != outputSet)
{
    bos.flush();
    bos.close();
    bos = new ByteArrayOutputStream();
    ExifRewriter ER = new ExifRewriter();
    ER.updateExifMetadataLossless(data, bos, outputSet);
    data = bos.toByteArray(); //Update you Byte array, Now it contains exif information!
}

No comments:

Post a Comment