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
To fix the EXIF orientation, open the image in your image editing program. Rotate the image to the correct orientation, then save the file and reupload your image. As an alternative, you can simply remove all EXIF data from images in Windows and macOS.
Photos taken with a smartphone or digital camera contain “Exif data,” all sorts of information about where the photo was taken, when it was taken, and even how the camera was oriented. When uploaded to File Manager, this data is preserved, and that can often cause the orientation of the picture to be rotated.
In the Edit Pictures task pane, under Edit using these tools, click Rotate and Flip. Do one of the following: Click Rotate left or Rotate right. If you click the option more than once, the picture will continue to rotate in the same direction.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With