Search code examples
androidexif

Writing XPKeywords in Exif


I want to write XPKeywords in a Jpeg Image. Till now I am using Sansaleen java api for writing Exif tags in Jpeg images. I am able to write most of the tags like subject, comment, author, rating but I am not able to write Windows XP Keywords. I am using below code:

public static TiffOutputField getTiffOutputFieldKeyword(
        TiffOutputSet outputSet, String metaDataToChange) {
    TiffOutputField imageHistoryPre = outputSet
            .findField(TiffConstants.EXIF_TAG_XPKEYWORDS);
    if (imageHistoryPre != null) {
        outputSet.removeField(TiffConstants.EXIF_TAG_XPKEYWORDS);
    }

    TiffOutputField tiffOutputField = new TiffOutputField(
            TiffConstants.EXIF_TAG_XPKEYWORDS,
            TiffFieldTypeConstants.FIELD_TYPE_BYTE,
            metaDataToChange.length(), metaDataToChange.getBytes("UTF-16"));

    return tiffOutputField;
}

I have googled this issue and came to know that XP_Keyword accept special encoded in UCS2 so I have updated my code. But still not able to write complete tags. Tags are semi-colon separated.

Please let me know if there present any resolution for the above issue or is there any other java/android lib which can write Tags in Jpeg files.


Solution

  • Got it working:

    public static TiffOutputField getTiffOutputFieldKeyword(
        TiffOutputSet outputSet, String metaDataToChange) {
    TiffOutputField imageHistoryPre = outputSet
            .findField(TiffConstants.EXIF_TAG_XPKEYWORDS);
    if (imageHistoryPre != null) {
        outputSet.removeField(TiffConstants.EXIF_TAG_XPKEYWORDS);
    }
    
    TiffOutputField tiffOutputField = new TiffOutputField(
            TiffConstants.EXIF_TAG_XPKEYWORDS,
            TiffFieldTypeConstants.FIELD_TYPE_BYTE,
            metaDataToChange.getBytes("UTF-16").length, metaDataToChange.getBytes("UTF-16"));
    
    return tiffOutputField;
    }
    

    Just use length of Bytes in "UTF-16" and then write. Also, Make sure you trim the characters to not include any spaces. Also, please give a try by separating string with Semicolon(;) as By default Windows take semicolon separated keywords.