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

后端 未结 1 585
一生所求
一生所求 2021-02-02 14:27

In Android, when decoding a Bitmap from a photo on the phone, the EXIF data in the original gets lost. I am sending this Bitmap to my server via a socket and would

相关标签:
1条回答
  • 2021-02-02 15:07

    Thanks to @Nick Campion and Sanselan.

    Working code:

    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!
    }
    
    0 讨论(0)
提交回复
热议问题