1. P_W999
  2. J-Exiftool

Wiki

Clone wiki

J-Exiftool / Examples

Examples

Read a tag

The following code is a simple example of how to read the ISO tag from a file "read01.JPG".

JExifTool tool = new JExifTool(); //starts exiftool process
JExifInfo info1 = tool.getInfo(new File("test-resources/read01.JPG")); // create proxy
LOG.info("ISO value is for read 1 = {}", info1.getTag(ExifIFD.ISO)); //execute read

Write a Tag

Writing a tag is almost as easy as reading a Tag. There are a few limitations to writing tags:

  • A tag may not be unsafe to write !
  • A tag may not be avoided /
  • A tag may not be protected *
  • Data validation is not 100% safe. Dates are not validated.
JExifTool tool = new JExifTool();
JExifInfo write1 = tool.getInfo(new File("tmp.jpg"));
LOG.info("Writing 5 to FOCAL_LENGTH for write 1 -> {}", write1.setTag(ExifIFD.FOCALLENGTH, "5.0"));

Write GPS info

Writing GPS info is fairly easy as long as you know what GPS is and does ;) . Following example will write to location 50.84791165546716N 4.350469368264016E at an altitude of 50m above sea level to the file gps.jpg. The location is given in decimal format and the last parameter "0" indicates that the provided altitude is above sea level.

JExifTool tool = new JExifTool();
JExifInfo gpsInfo = tool.getInfo(new File("GPS.JPG"));
gpsInfo.setGPSInfo("50.84791165546716", "N", "4.350469368264016", "E", "50", "0");
tool.stop();

J-ExifTool also supports DMS format:

JExifTool tool = new JExifTool();
JExifInfo gpsInfo = tool.getInfo(new File("GPS.JPG"));
gpsInfo.setGPSInfo("50 50 52.4832", "N", "4 21 1.6884", "E", "50", "0");
tool.stop();

More info about GPS + Exiftool can be found here and the complete list of supported GPS tags (including the values they support can be found here

Using unsupported Tags

I currently do not have the time to convert the complete list of supported tags into java code. Since v0.0.4 you can "add" support for other Tags. All you have to do is create a new enum that implements the Tag interface. For example:

public enum TestTagClass implements Tag {
		LENSSPEC("LensSpec", false, false, false, DataType.STRING);
		private final boolean avoided;
		private final boolean unsafe;
		private final boolean protectedField;
		private final String name;
		private final DataType type;

		private TestTagClass(final String name, final boolean unsafe, final boolean avoided, final boolean protectedField, final DataType type) {
			this.avoided = avoided;
			this.unsafe = unsafe;
			this.protectedField = protectedField;
			this.name = name;
			this.type = type;
		}

//some getters come here

You can then register your new set of tags:

TagUtil.register(TestTagClass.class);

Change the language of the error messages

Since v0.0.4 you can change the language of the error messages with the following snippet:

Cal10nUtil.changeLocale(new Locale("nl"));

If the language isn't supported, J-ExifTool will fallback to english. Currently, following languages are supported:

  • English
  • Dutch

Extract thumbnail image

JExifInfo write1 = tool.getInfo(new File("read01.JPG"));
// Important: you'll get an error if the thumnail already exists, so check whether it exists prior to calling this (or live on the dangerous side of live and don't check it ;) )
info1.extractThumbnail(null);	// create thumbnail read01_tn.JPG next to read01.JPG
info1.extractThumbnail("_tn.jpg");	// create thumbnail read01_tn.jpg next to read01.JPG
info1.extractThumbnail("/tmp/cache/%f_thumbnail.jpg");	//create thumnbail read01_thumbnail.jpg in /tmp/cache folder

Deleting all exif tags

JExifInfo deletAll = tool.getInfo(file);
deletAll.deleteAllExifTags();	// no return value

Updated