A F
A F

Reputation: 63

Java: How do I write a custom tag (programmers note) in a jpeg file

I'm wondering to write some new user defined tags in a jpeg file. I use "org.apache.commons.imaging" libraries to work with jpeg meta data in java. I just do not know how to define a new custom tag; write it and then read the value of the tag later.

Here is my code which reads a jpeg file and make an output.

public void setExifCustomTag(final File jpegImageFile, final File dst) throws IOException,
        ImageReadException, ImageWriteException {
    OutputStream os = null;
    boolean canThrow = false;
    try {
        TiffOutputSet outputSet = null;

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
        if (null != jpegMetadata) {
            // note that exif might be null if no Exif metadata is found.
            final TiffImageMetadata exif = jpegMetadata.getExif();

            if (null != exif) {
                outputSet = exif.getOutputSet();
            }
        }
        if (null == outputSet) {
            outputSet = new TiffOutputSet();
        }



        TagInfoAscii tag1 = new TagInfoAscii("custom tag", 1, 20, TiffDirectoryType.EXIF_DIRECTORY_MAKER_NOTES);
        outputSet.getExifDirectory().add(tag1, "custom value");

        TiffOutputField to = outputSet.findField(tag1);
        //How should I read the value ???????????????

        os = new FileOutputStream(dst);
        os = new BufferedOutputStream(os);

        new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                outputSet);
        canThrow = true;
    } finally {
        IoUtils.closeQuietly(canThrow, os);
    }
}

Upvotes: 2

Views: 1082

Answers (1)

user3344003
user3344003

Reputation: 21637

I could not do this as a comment, so I have to go here:

Your question needs some clarification. There are a variety of JPEG file formats. The process of writing custom tags [kind of] varies among those formats.

You can write a COM marker with text in any JPEG file. You can write an APPn marketing with whatever you want in an JPEG file as long as you do not use one of the APPn markers used by the particular file format.

In Exif, there is a UserComment tag defined that you can use for your own data.

Your example presumes EXIF is the file format.

Upvotes: 2

Related Questions