Skip to main content

Write JPEG with Exif-Data

5 replies [Last post]
blackfox86
Offline
Joined: 2007-10-08

Hi,
I'm relative new to JAI and ImageIO. The last days I tryed hard to find a solution for reading and writing exif-tags from JPEGs.

I achieved the reading using the following reader:
com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader
and then use imageio-tiff-format to get the tags. I even managed to change the value of an tag by creating a new one and replace the current. The problem occurs when trying to write the image back to disk: then all the exif-data is lost.

I achieved as well to read the JPEG with the reader
com.sun.imageio.plugins.jpeg.JPEGImageReader
and write it back to disk with the appropriate writer right away as is.
But with this Reader I can not use the tiff-format, so I can not read out exif-tags not to mention changing them.

I actually tried to read with the com.sun.media...-reader and pass the metadata-object to an com.sun.imageio....-writer, but this way the exif-data is getting lost as well.

Searching this forum I found the following bugreport:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6194442
Thought maybe this ist the reason for the com.sun.media...-writer to not write Metadata, but this bug was fixed in 1.1beta and I'm using the current (1.1-stable) release.

Now I don't have any more idea - can please someone help me? If there would be a completely other way to read and write exif-data I would appreciate that, too.

Below you can find my current sourcecode.
Thanks for reading and trying to help,
blackfox86

Message was edited by: blackfox86

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
marcojacob
Offline
Joined: 2006-07-10

I'm a little confused that a JPEG reader offers TIFF metadata not JPEG Metadata.
If you would use ImageIO API to read not JAI-API you can use native JPEG
metadata format or a common imageio metadata format. Maybe your problem ist
that you use the wrong metadata classes.

However I don't know if the native J2SE JPEG codec can write imageio common
metadata. Maybe you need to use the pure java codec.

Message was edited by: marcojacob

blackfox86
Offline
Joined: 2007-10-08

The thing with TIFF-Metadata is correct since JPEG-Exif-Data is the same format as TIFF-Metadata. This makes it comparatively easy to read selective Values using tag-constants (e.g. EXIFTIFFTagSet.TAG_DATE_TIME_ORIGINAL)

I think I could have expressed the main question more precise:
Is it possible to use the writer "com.sun.media...." for writing JPEG-images inclusive their metadata? I tried already the version with JAI (see above) and the following:
[i]writer.setOutput(new FileImageOutputStream(new File(outputFileName)));
riter.write(null, new IIOImage(image, null, imageMetadata), writeParam);[/i]

I've tried all I could think of, but found no way to get it work - the image itself gets always written correctly but metadata is missing and no exception is thrown I could see - therefore I suppose this writer has a bug but I might be wrong with that. Has anybody tried or even managed this before? What am I doing wrong? Or can someone approve my assumption with the bug?

agirbal
Offline
Joined: 2009-05-04

Any luck writing exif to jpeg with imagewriter?
Im having same issue here - read in an image and its exif with imagereader fine, then when I write back the exif is always the default one.
Im using the clib writer since the other one is way too slow (and doesnt write exif either).
thanks
AG

blackfox86
Offline
Joined: 2007-10-08

I ended up writing exif using the apache sanselan library. I don't like it to use an additional library, especially as I only need a small piece of it - but at the end I had no more time to try on with JAI against long odds.
blackfox86

Message was edited by: blackfox86

blackfox86
Offline
Joined: 2007-10-08

sorry for double-post, but the sourcecode appended was cut down everytime I saved my post...! Trying to post it in this second post...:

edit: didn't work in second post as well - is there a chance to append a *.java file here?

so far, here some important parts:

// choose a reader
Iterator readers = ImageIO.getImageReadersByFormatName("jpeg");
ImageReader reader = null;
while (readers.hasNext()) {
reader = (ImageReader) readers.next();
// com.sun.media or com.sun.imageio - use a specific reader
if (reader.getClass().getName().startsWith("com.sun.media")) {
// Break on finding the core provider.
break;
}
}
// read the image
ParameterBlockJAI pb_r = new ParameterBlockJAI("ImageRead");
pb_r.setParameter("Input", inputFile);
pb_r.setParameter("Reader", reader);
RenderedImage ri = JAI.create("ImageRead", pb_r);

...

// read the metadata (if reader com.sun.media)
Object o = ri.getProperty("JAI.ImageMetadata");
CLibJPEGMetadata md = null;
String[] formats = new String[0];

...

// choose a writer
Iterator writers = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = null;
while (writers.hasNext()) {
writer = (ImageWriter) writers.next();
// com.sun.media or com.sun.imageio - use a specific writer
if (writer.getClass().getName().startsWith("com.sun.media")) {
// Break on finding the core provider.
break;
}
}
// Write Image to disk
ParameterBlockJAI pb = new ParameterBlockJAI("ImageWrite");
pb.addSource(ri);
pb.setParameter("Output", outputFile);
pb.setParameter("UseProperties", true);
pb.setParameter("Transcode", true);
pb.setParameter("Format", "JPEG");
pb.setParameter("ImageMetadata", md);
pb.setParameter("Writer", writer);
RenderedOp ro = JAI.create("ImageWrite", pb);

Message was edited by: blackfox86