3

My use case is pretty simple which I think is shared with many other developers.

I would like to:

 1. Load an image
 2. Read EXIF Orientation details (If available)
 3. Rotate (90, 180, 270) degrees
 4. Resize and store images.

I tried going through the forums and then tried:

Sanselan: Reads EXIF, but not JPG images. Also, commons-imaging download links are all broken.

Java ImageIO/Graphics2D: Can rotate/re-size(not one liner, but is understandable) although as suggested you do lose quality when rotating.

*BUT does not read all JPG images (Throws CMMException for some jpg file)

The rest are either too old and not maintained anymore, have no documentation at all or I missed the 'good' one.

Can anyone suggest a library that supports these few 'simple' use cases?

*Using Sanselan to read EXIF is fine. Read/Rotate/Re-size (JPGs) is my main problem

pinkpanther
  • 4,770
  • 2
  • 38
  • 62
Ioannis Deligiannis
  • 2,679
  • 5
  • 25
  • 48
  • 1
    AFAIK, JPG is not lossless format, every time you save it, you loose something. – user1516873 Jun 13 '13 at 12:21
  • @user1516873 Does this mean that when a camera takes (JPG) picture in landscape orientation the only way to view the image is to tilt the camera? – Ioannis Deligiannis Jun 13 '13 at 12:27
  • no, loss in quality happens only when you read JPG image in some internal format (not important, what) and save it again. Your camera not rewrite image every time, so it is safe to rotate it as you want ;) – user1516873 Jun 13 '13 at 12:35
  • 1
    Interesting read regarding the Orientation/rotate of JPGs: http://www.impulseadventure.com/photo/lossless-rotation.html – Ioannis Deligiannis Jun 13 '13 at 12:51

3 Answers3

4

Thumbnailator is a simple Java library which has no external dependencies which can (a) load an image, (b) read the Exif metadata and automatically rotate the image, (c) resize and (d) store the image in one single statement:

Thumbnails.of("path/to/image")
    .size(320, 240)
    .toFile("path/to/thumbnail");

The above will:

  • open the image
  • resize it so that it will fit in a 320 pixel x 240 pixel region, while
  • maintaining the aspect ratio of the original image, and
  • properly orient the image according to the Exif metadata, and
  • store it to a new image

If one desires to perform additional rotations, a rotate method is also available.

Disclaimer: I am the maintainer of Thumbnailator.

coobird
  • 159,216
  • 35
  • 211
  • 226
  • Seems exactly what I am looking for and I can see it is on maven as well. I will try it and mark as answer. Note that I expect it to break as well if it uses out-of-the-box ImageIO.read(file) to read a JPG – Ioannis Deligiannis Jun 13 '13 at 14:01
  • Didn´t know about it. Sounds cool ! – jlengrand Jun 13 '13 at 19:37
  • @johnd Thumbnailator does use the Image I/O API to read JPEGs, but it doesn't use `ImageIO.read` as is -- it performs additional actions to retrieve the Exif metadata (which _can_ be done with extra work!) from the JPEG along with the image. – coobird Jun 14 '13 at 01:20
  • @coobird I tried Thumbnailator with the bad image and it breaks as well :( It is very easy to use though as needed quite a lot of code to do the same. Can you explain how do you process the EXIF data in terms of IO operations? Currently I use Sanselan, which means that I need to a. Read EXIF, rotate and write to file with ImageIO/Graphics2D and the write/copy updated EXIF. This is very heavy on IO. Does your library do the same on 1 IO write? i.e. Read Image, process and write (with updated EXIF) in one file operation? – Ioannis Deligiannis Jun 14 '13 at 07:47
  • @johnd Thumbnailator does do the Exif read and image read operation in a single file opening operation. (It uses Image I/O as the backend, so it's probably not a single read, but probably doesn't do anything too inefficient.) One thing Thumbnailator does not do is to modify the Exif and write it into the thumbnail image. – coobird Jun 15 '13 at 07:09
  • @coobird can you tell me please,if Thumbnailator can JUST read exif data?(w/o resize and rotate). Interesting if i can extract value of exif data(orientation). – XTL Nov 19 '15 at 14:17
2

Might be much of an overkill if this is the only thing you will have to do, but OpenCV is an excellent image processing library. It comes with Java bindings, and is under BSD licence.

I say this is an overkill for a simple use case like that, simply because it can do so much more. It is always my first weapon of choice for years though, simply because it has bindings with lots of languages, is portable pretty much everywhere and even now has GPU support :).

There are already SO questions that answer exactly what you want I think. Look here for an example

Hope this helps

Community
  • 1
  • 1
jlengrand
  • 12,152
  • 14
  • 57
  • 87
0

You just better use java 2D library itself.

Like :

paint(Graphics g)
Graphics g2= (Graphics)g
g2.draw();
g2.rotate(give parameters)
Will Mcavoy
  • 485
  • 5
  • 24
  • I am using Graphics2D (in one of my implementations) to rotate the image. As many posts suggests, even by reading the JPG file (ImageIO) you lose quality. – Ioannis Deligiannis Jun 13 '13 at 12:09
  • 1
    @johnd a suggestion for you, it woudld be better if you mention all these kind of details that why you won't use `Graphics2D` and all in the question... you will get more good answers....:) by the way have you tried `ImageReader` instead of `ImageIO`?. see http://stackoverflow.com/questions/17015197/quality-loss-using-imageio-write – pinkpanther Jun 13 '13 at 12:12
  • @pinkpanther I thought this was implied by saying that I use ImageIO and that it loses on quality. I will update my question ;) I haven't tried ImageReader because I assumed that since it is part of ImageIO package, it will use the same code. Am I missing somthing? – Ioannis Deligiannis Jun 13 '13 at 12:24
  • @johnd in [this link](http://stackoverflow.com/questions/17015197/quality-loss-using-imageio-write), using `ImageWriter` instead of `ImageIO` solved the problem, that's the reason I suggested...:) I don't know anything else... – pinkpanther Jun 13 '13 at 12:31
  • Yes, with ImageWriter one can set a high quality 1.0F. – Joop Eggen Jun 13 '13 at 13:47