ImageIO.read( ) always rotates my uploaded picture

后端 未结 2 1155
孤独总比滥情好
孤独总比滥情好 2021-02-07 10:36

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 sa

2条回答
  •  抹茶落季
    2021-02-07 11:18

    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

     
      com.drewnoakes 
      metadata-extractor 
      2.7.2 
    
    

    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).

提交回复
热议问题