Java api to exiftool

…衆ロ難τιáo~ 提交于 2019-12-05 07:17:40

ExifTool (for Java) is designed to be a simple-to-use and robust Java abstraction of Phil Harvey's ExifTool. I just made the first public release this past week after incubating the project under the imgscalr project umbrella for a while.

The project is under the commercial-friendly Apache 2 license.

My goal for the library isn't just to abstract out the external process execution code from the caller (like most of the other abstraction layers are seeming to do) but to actually design a wrapper so tightly integration and resilient (I will clarify what I mean here later), that you treat instances of your ExifTool class exactly as you would if ExifTool itself was written in Java.

In this initial release I support reading tag data (will add writing in a future release) and it is as simple as this:

File image = // path to some image
ExifTool tool = new ExifTool();

Map<Tag, String> valueMap =
    tool.getImageMeta(image, Tag.GPS_LATITUDE, Tag.GPS_LONGITUDE);

System.out.println("Lat: " + valueMap.get(Tag.GPS_LATITUDE) +
    "\tLong: " + valueMap.get(Tag.GPS_LONGITUDE));

Using ExifTool in the new "daemon mode" (-stay_open True cmd line) is also supported and turning on support for it is as easy as creating your ExifTool instance like so:

ExifTool tool = new ExifTool(Feature.STAY_OPEN);

The documentation on how to use the ExifTool class is extensive, covering everything from design to performance to thread safety.

In addition to making use of ExifTool simple from Java, the class employs a considerable amount of precautions to minimize runtime issues as well as correctly catching and reporting any and all errors that can arise in well-documented ways (instead of letting surprise exceptions bubble up from core Java classes).

I was so pedantic with this exception handling and error recovery because the class is designed to allow you to utilize ExifTool in a high-availability environment like a busy web application. I didn't just want to wrap simple Process objects then throw my hands up in the air if something exploded. I knew myself (and anyone else using the class) would need a well designed API to allow for easy error recovery.

For example, attempting to use ExifTool in daemon mode will cause the class to actually check the underlying install of ExifTool for support for that feature and throw an UnsupportedFeatureException with recommendations on how to work around the problem if it isn't supported.

In addition to the pre-condition checking, to ensure that usage of the class (namely in daemon mode) doesn't leak native OS processes as well as Input/OutputStreams used to communicate with them, the class provides an auto-cleanup thread that after a specified interval of inactivity (default is 10 mins) will shut down the external process cleanly and the read/write streams, making the idle instance of ExifTool lightweight and easy to keep around for re-use.

All the resources are re-initialized on the next call to the class to parse more metadata so there is no need to throw out and re-create new instances. Also the cleanup thread only executes after extended periods of inactivity, not on a set schedule. You can set the interval to anything you want or shut off the cleanup thread completely and manage cleanup yourself (just call close()).

These designs are all part of my ultimate goal of making ExifTool integration into a Java application seamless, performant and easy.

You can check out the main project page for more info on the project, usage, source, download links, etc. or you can jump right to GitHub and look through the code if you prefer.

I don't know exiftool, but I have previously used MediaUtil to read and write exif tags in java (I used it for automated jpeg image rotation)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!