ImageIO.read( ) always rotates my uploaded picture
Asked Answered
L

3

17

I want to create a web application that allow users to upload their image to the server. When they click send, their image will be uploaded to the server (multipart). Before saving, I want to make some operation with the image, so I decided to use ..

ImageIO.read(InputStream)

to get BufferedImage object

here is the code:

public static BufferedImage getBufferedImageFromMultipartFile(MultipartFile file) 
throws APIException
{
    BufferedImage bi = null;

    try
    {
        bi = ImageIO.read(file.getInputStream());
    }
    catch (IOException e)
    {
        throw new APIException(ErrorCode.SERVER_ERROR, e);
    }

    return bi;
}

The problem is when I try to upload a picture that has height more than width such as 3264 x 2448 (height x width), the result always an image that has been rotated (2448 x 3264).

Is there any solution to solve this problem ?

Is this a bug or any defined API specification ?

Thx.

PS. sorry for my english :D

Lacour answered 12/4, 2013 at 18:53 Comment(1)
I have found similar question now, #9453867. So, my problem is not what a bug of ImageIO but it is the API specification that ImageIO can not read image detail (EXIF-orientation). THX.Lacour
F
14

ImageIO.read( ) can't read the orientation of the image if it was taken with mobile device.

I used metadata-extractor to read metadata, i think it's a good solution: github.com/drewnoakes/metadata-extractor/wiki

<dependency> 
  <groupId>com.drewnoakes</groupId> 
  <artifactId>metadata-extractor</artifactId> 
  <version>2.7.2</version> 
</dependency>

Read orientation filed in exif directory:

ExifIFD0Directory exifIFD0 = metadata.getDirectory(ExifIFD0Directory.class);
int orientation = exifIFD0.getInt(ExifIFD0Directory.TAG_ORIENTATION);

switch (orientation) {
  case 1: // [Exif IFD0] Orientation - Top, left side (Horizontal / normal)
    return null;
  case 6: // [Exif IFD0] Orientation - Right side, top (Rotate 90 CW)
    return Rotation.CW_90;
  case 3: // [Exif IFD0] Orientation - Bottom, right side (Rotate 180)
    return Rotation.CW_180;
  case 8: // [Exif IFD0] Orientation - Left side, bottom (Rotate 270 CW)
    return Rotation.CW_270;
}

(Rotation is a class from the org.imgscalr.Scalr framework I use ti rotate image).

Fleawort answered 28/3, 2015 at 16:40 Comment(1)
This was extremely helpful. The code with the Scalr constants was also exactly what I needed.Ebarta
V
0

Quite interesting issue... you can try to fix it by introducing a check on the image width and height to be larger than 2448 and 3264 respectively and then just swap its width and height

Use below piece of code:

BufferedImage oldImage = ImageIO.read(file.getInputStream());

if (oldImage.getWidth() > 2448 || oldImage.getHeight() > 3264) {

    BufferedImage newImage = new BufferedImage(oldImage.getWidth(),
                oldImage.getHeight(), oldImage.getType());

    Graphics2D graphics = (Graphics2D) newImage.getGraphics();

    graphics.drawImage(oldImage, 0, 0, oldImage.getHeight(),
                oldImage.getWidth(), null);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ImageIO.write(newImage, "JPG", bos);
}
Vinavinaceous answered 12/4, 2013 at 19:29 Comment(2)
thx. for participate but I think my word and grammar are making you misunderstanding. Now, I have improved my question.Lacour
ok ...now its clear... gud that SO has the answer and you found it too :)Vinavinaceous
H
0

A little update on Paolo Biavati answer.

I did not find method metadata.getDirectory(ExifIFD0Directory.class); instead what is available is metadata.getFirstDirectoryOfType(ExifIFD0Directory.class). May be it was in old versions. Now the code goes like this:

    Metadata metadata = ImageMetadataReader.readMetadata(file);
    ExifIFD0Directory exifIFD0 = metadata.getFirstDirectoryOfType(ExifIFD0Directory.class);
    int orientation = exifIFD0.getInt(ExifIFD0Directory.TAG_ORIENTATION);
    
    switch (orientation) {
      case 1: // [Exif IFD0] Orientation - Top, left side (Horizontal / normal)
        return null;
      case 6: // [Exif IFD0] Orientation - Right side, top (Rotate 90 CW)
        return Rotation.CW_90;
      case 3: // [Exif IFD0] Orientation - Bottom, right side (Rotate 180)
        return Rotation.CW_180;
      case 8: // [Exif IFD0] Orientation - Left side, bottom (Rotate 270 CW)
        return Rotation.CW_270;
    }
Hhour answered 9/6, 2023 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.